View RSS Feed

Java Swing

Setting and Changing Column Widths of JTable

Rate this Entry
by , 04-26-2012 at 08:44 PM (998 Views)
All tables which are there in a table by default start with equivalent width and columns are automatically filling up the whole tableís width. When table has become narrow or wide, all widths of column would be changed appropriately.


When a column is resized by dragging the right border then size of the table or the size of the column must change. Size of the table remains similar by default & all columns which are right to the drag point would be resized so that space could be accommodated of removal or addition from column, to drag pointís left.
Initial width of columns could be customized by invoking the setPreferredWidth at columns of your table. In this way preferred column widths and relative widths would be settled. Consider the given example where addition of the given code to SimpleTableDemo would be making 3rd column big as compared to other columns present.
Java Code:
TableColumn column = null;
for (int i = 0; i < 5; i++) {
    column = table.getColumnModel().getColumn(i);
    if (i == 2) {
        column.setPreferredWidth(100); //third column is bigger
    } else {
        column.setPreferredWidth(50);
    }
}

Submit "Setting and Changing Column Widths of JTable" to Facebook Submit "Setting and Changing Column Widths of JTable" to Digg Submit "Setting and Changing Column Widths of JTable" to del.icio.us Submit "Setting and Changing Column Widths of JTable" to StumbleUpon Submit "Setting and Changing Column Widths of JTable" to Google

Categories
JTable

Comments