Java JTable set auto resize last column

Description

Java JTable set auto resize last column

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Main {
   public static void main(String[] args) {
      JFrame f = new JFrame("JTable example");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      Object[][] cellData = { { "1-1", "1-2" },// 
                              { "2-1", "2-2" } //
                            };/* ww  w.  j a  v  a2s  .co  m*/
      String[] columnNames = { "col1", "col2" };

      JTable table = new JTable(cellData, columnNames);

      table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

      f.add(new JScrollPane(table));
      f.pack();

      f.setVisible(true);

   }
}



PreviousNext

Related