Java Swing How to - Resize all columns to the right when the width of a column is changed








Question

We would like to know how to resize all columns to the right when the width of a column is changed.

Answer

 /*from w  w w .  j a  v  a2s.c o  m*/


import javax.swing.JTable;

public class Main {
  public static void main(String[] argv) throws Exception {
    Object[][] cellData = { { "1-1", "1-2" }, { "2-1", "2-2" } };
    String[] columnNames = { "col1", "col2" };

    JTable table = new JTable(cellData, columnNames);
    
    table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
  }
}