JTable (javax.swing.JTable) basics explained

UPDATED: 01 June 2013
Importance of Jtable in Desktop application.
If you are beginner then you must know why and how to use JTable. Table really important component of all programming language. Whether you work around web application, desktop application or consider database.

             Jtable helps you to organize your data in proper manner. Let say you want to show all your employee's details. Jtable is your option.


Today's agenda for JTable
  • How to change width of column in JTable?
  • How to change height of cell in JTable?
  • How to disable column in JTable?
  • How to disable row in JTable?

How to change width of column in JTable?
jTable1.getColumnModel().getColumn(2).setMaxWidth(70);
jTable1.getColumnModel().getColumn(2).setMinWidth(70);

How to change height of cell in JTable?
private void updateRowHeights() {
   try {
        for (int row = 0; row < jTable1.getRowCount(); row++) {
            int rowHeight = jTable1.getRowHeight();
            for (int column = 0; column < jTable1.getColumnCount(); column++) {
               Component comp = jTable1.prepareRenderer(jTable1.getCellRenderer(row, column), row, column);
               rowHeight = Math.max(rowHeight, comp.getPreferredSize().height + 20);
               //Change the rowHeight in above line
            }
          jTable1.setRowHeight(row, rowHeight);
       }
   } catch (ClassCastException e) {
       e.printStackTrace();
   }
}

How to disable column in JTable? , How to disable row in JTable?
Object[][] rowData = new Object[10][2];
        Object columnNames[] = {"Column 1", "Column 2"};
        DefaultTableModel tm = new DefaultTableModel(rowData, columnNames) {
            @Override
            public boolean isCellEditable(int row, int column) {
                if (column == 1 || row == 5 || row == 10) {
                    return false;//the nth row, column is not editable  
                }
                return true;
        }
};

Other article related to JTable :
How to update jtable dynamically from Oracle , MySQL (Database)?
http://www.javaquery.com/2010/10/how-to-update-jtable-dynamically-from.html
How to implement JButton in JTable?
http://www.javaquery.com/2013/05/how-to-implement-jbutton-in-jtable.html
How to implement JComboBox in JTable?
http://www.javaquery.com/2013/05/how-to-implement-jcombobox-in-jtable.html

0 comments :