Each column of a JTable component is represented by the TableColumn grade. The method for setting or changing the width of the column includes the setMinWidth(), setMaxWidth() and setPreferredWidth(). These methods are used to fix the minimum, maximum and the preferred width of the column respectively.

When we set merely the preferred width of a tabular array cavalcade and the container get resized the preferred width will exist used to recalculate the new cavalcade width to make full the available space, but the preferred width value itself does not change.

TableColumn object of a table can be obtained by calling table's getColumnModel() method which return an instance of TableColumnModel. Afterwards having the TableColumModel in hand nosotros tin become the tabular array'south column by calling the getColumn(int index) method and passes the index of the column.

              bundle org.kodejava.swing;  import javax.swing.*; import javax.swing.tabular array.TableColumn; import coffee.awt.*;  public class TableColumnWidthDemo extends JPanel {     public TableColumnWidthDemo() {         initializePanel();     }      public static void showFrame() {         JPanel panel = new TableColumnWidthDemo();         panel.setOpaque(true);          // Creates and configures the JFrame component for our         // programme.         JFrame frame = new JFrame();         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);         frame.setContentPane(panel);         frame.setTitle("Premiere League - Flavour 2021-2022");         frame.pack();         frame.setVisible(true);     }      public static void main(String[] args) {         SwingUtilities.invokeLater(TableColumnWidthDemo::showFrame);     }      individual void initializePanel() {         // Defines tabular array'south column names.         String[] columnNames = {                 "Order", "MP", "W", "D", "50", "GF", "GA", "GD", "PTS"         };          // Defines tabular array'south information.         Object[][] information = {                 {"Chelsea", 8, vi, 1, 1, sixteen, 3, thirteen, nineteen},                 {"Liverpool", 8, 5, 3, 0, 22, half dozen, 16, 18},                 {"Manchester City", 8, 5, 2, one, 16, 3, 13, 17},                 {"Brighton", eight, 4, 3, ane, 8, 5, 3, xv},                 {"Tottenham", 8, 5, 0, three, ix, 12, -3, 15}         };          // Defines table'southward column width.         int[] columnsWidth = {                 200, 25, 25, 25, 25, 25, 25, 25, 50         };          // Creates an instance of JTable and fill it with data and         // column names information.         JTable table = new JTable(information, columnNames);          // Configures table'due south column width.         int i = 0;         for (int width : columnsWidth) {             TableColumn column = table.getColumnModel().getColumn(i++);             column.setMinWidth(width);             column.setMaxWidth(width);             column.setPreferredWidth(width);         }          JScrollPane scrollPane = new JScrollPane(tabular array);         table.setFillsViewportHeight(true);         this.setLayout(new BorderLayout());         this.add(scrollPane, BorderLayout.Center);         this.setPreferredSize(new Dimension(500, 500));     } }                          

Here is the tabular array created by the program above:

JTable Column Width Demo

JTable Column Width Demo

  • Author
  • Recent Posts