TableColumn setPreferredWidth not working

I have a JTable with multiple columns. I want a specific column to change. I was hoping that using setPreferredWidth, the column would be resized to the size or size of the content so that there was no truncation , and the remaining columns occupy the remaining space, but instead, all the columns, including the modified ones, share the entire table space equally; as if setPreferredWidth did nothing at all . In fact, I want to be able to set the column width and reduce it to this size without reducing the content (have I emphasized this too much?) So that all columns that have not been changed fill the remaining space. Using setMaxWidth trims the content (did I mention that I didn’t like it?) How do I resize / reduce a column without trimming it and doing absolutely nothing without it? Here is the violation code:

for (int i = 0, x = 0; i < table.getColumnModel().getColumnCount(); i++) if ((x = model.getColumnWidth(i)) > -1) table.getColumnModel().getColumn(i).setPreferredWidth(x); 

The table is in the JPanel (MyListPanel - BorderLayout), which is in another JPanel (GridBagLayout) added with

 new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 0, 0, 2), 0, 0)) 

EDIT: this is the constructor for my subclass of JPanel:

 public MyListPanel(boolean showHeader, String title, ColData...columns) { super(new BorderLayout()); model = new MyListTableModel(columns); table = new JTable(model); table.addFocusListener(this); add(table); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); setTitle(title); if (showHeader) add(table.getTableHeader(), BorderLayout.NORTH); for (int i = 0, x = 0; i < table.getColumnModel().getColumnCount(); i++) if ((x = model.getColumnWidth(i)) > -1) table.getColumnModel().getColumn(i).setPreferredWidth(x); setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)); } 

And MyListTableModel.ColData:

 public static class ColData { private int index; private String title; private int width; public ColData(int _index, String _title, int _width) { index = _index; title = _title; width = _width; } } 
+7
source share
4 answers

Include:

 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
+8
source

I had a similar problem even though we tried two other answers. In my case, several times the width will be set correctly, but in other cases it won't. I found that the problem was caused by the fact that I was trying to set the column width right after changing my table model. I found that setting the column width inside SwingUtilities.invokeLater () did the trick. I.E.

 SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { int width = 100; for (int column = 1; column < table.getColumnCount(); column++) { columnModel.getColumn(column).setPreferredWidth(width); } } } 
+3
source

I know this is a bit late, so mostly for future readers, but I had the same problem and it was solved by setting the preferred width and maximum width for the column to the same value.

+2
source

I had the same problem and I read @Jay Askren's answer, on the contrary it worked for me. I needed to set the width in the method in which the table was created.

 public class Example{ //..stuff private JTable myTable; public Example{ //..stuff myTable = AnotherClass.getTable(); myTable .getColumnModel().getColumn(0).setPreferredWidth(100);//not here } } public class AnotherClass{ public static JTable getTable(){ JTable table= new JTable(); table.setModel(anyModel); table.getColumnModel().getColumn(0).setPreferredWidth(100);//Here! return table } } 
+1
source

All Articles