JTable Basics

 

jTableModel = new jTableModel();

jTable = new JTable(jTableModel);

jTable.getColumnModel().getColumn(2).setCellRenderer(new jTableCellRenderer());

jTable.getColumnModel().getColumn(0).setPreferredWidth(170);

jTable.getColumnModel().getColumn(1).setPreferredWidth(500);

jTable.getColumnModel().getColumn(2).setPreferredWidth(250);

jTable.getColumnModel().getColumn(3).setPreferredWidth(80);

jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

//jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

Note: If the table has 4 columns, it will automatically adjust the column width according to the ratio set later. If there are several columns in the table, set the PreferredWidth of these columns, otherwise it will have no effect. If you don't write all 4 columns, and you want the table to be displayed according to your PreferredWidth, you need to turn off the "Auto Adjustment Mode" and it's OK. jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); However, after turning this off, when your page is enlarged, if the PreferredWidth you set does not add up to the size of the page you enlarged, the following columns will not change accordingly. Then it gets ugly. Therefore, it is OK to directly set as many columns as there are and let JTable automatically scale.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326654720&siteId=291194637