JTable column size task

I had a problem setting the width of a JTable column.

The code below works fine:

TableColumn a =shipsAndOwnHitsTable.getColumnModel().getColumn(0); a.setPreferredWidth(800); 

It changes the width of the first column.

However, when posting for some time or in a for loop, nothing happens:

  int index = 0; while (index < columnNum){ TableColumn a =shipsAndOwnHitsTable.getColumnModel().getColumn(index); a.setPreferredWidth(800); index+=1; } 

This code does not work, nothing happens with column sizes, can someone explain why? If not, someone will tell me how to set the width of the row and column the same way, i.e. I want all the cells in my table to be square, regardless of the size of the table (rows and columns).?

thanks

+6
source share
5 answers

The following code is working fine. According to my comments, please note that

  • setAutoResizeMode(JTable.AUTO_RESIZE_OFF) was called
  • table component built into JScrollPane to allow arbitrary size

Compare this to your code and you can quickly find your problem.

 import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.table.TableColumn; public class TableColumnSizeTest { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // prepare table JTable table = new JTable(new String[][] { { "Row 1 Col A", "Row 1 Col B" }, { "Row 2 Col A", "Row 2 Col B" } }, new String[] { "ColA", "ColB" }); // add into scroll pane f.getContentPane().add(new JScrollPane(table)); // turn off auto resize table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // set preferred column widths int index = 0; while (index < 2) { TableColumn a = table.getColumnModel().getColumn(index); a.setPreferredWidth(10); index++; } f.pack(); f.setVisible(true); } }); } } 
+7
source

Try setting the minimum and maximum width to 800 . And other widths if you find them.

0
source

I believe the column width is relative. All 800 should be the same as all 100, but try setting them to different values. To make them bigger, make the table wider.

0
source

Why not use:

  int index = 0; while (index < columnNum){ shipsAndOwnHitsTable.getColumnModel().getColumn(index).setPreferredWidth(800); index+=1; } 

Also, why not change your loop to:

 for(int index = 0; index < columnNum; index++){ shipsAndOwnHitsTable.getColumnModel().getColumn(index).setPreferredWidth(800);// two lines of code less } 

Remember that you use (recommended) while-loop if you do not know the number of iterations in advance ...

-1
source

If jTable is in jScrollPane, you also need to increase the size (width) of jScrollPane ...

sample code

 int index = 0; int columnNum = 2; while (index < columnNum){ TableColumn a=jTable1.getColumnModel().getColumn(index); 
Dimension d = jTable1.getPreferredSize(); d.width = d.width + 500; jTable1.setPreferredSize(d); jScrollPane1.setPreferredSize(d); index+=1; }
I used a existing table size and increased the size of the pane ...

Hope this helps ...

-1
source

Source: https://habr.com/ru/post/886602/


All Articles