TableColumn setPreferredWidth not working
I have a JTable with a number of columns. I want a particular column to resize. What I was hoping was by using setPreferredWidth, the column would resize to that size, or the size of the contents such that no truncation occurred and let the rest of the columns take the remaining space, but instead, all of the columns, including the one I resized, equally split all of the space of the table; as if setPreferredWidth did nothing at all. In effect, I want to be able to set the width of a column and have it shrink to that size without truncating content (have I stressed that too much yet?) in such a way th开发者_StackOverflow中文版at all columns that have not been resized fill the remaining space. Using setMaxWidth truncates content (did I mention I didn't like that?) How do I resize/shrink a column without it truncating and without it doing absolutely nothing? Here is the offending 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 a 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; }
}
Include :
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
I had a similar problem despite trying the two other answers here. In my case, some times the width would get set correctly while other times, it would not. I found the problem was caused because I was trying to set the column widths immediately after changing my table model. I found setting the column widths inside of 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);
}
}
}
I know this is a bit late so is mostly for future readers, but I had the same problem and solved it by setting both the column's preferred width and maximum width to the same value.
I had the same issue and reading @Jay Askren answer, the opposite worked for me. I had to set the width in the method where the table was being 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
}
}
Set all three of setMinWidth, setMaxWidth and setPreferredWidth, where minimum_width <= preferred_width <= maximum_width.
int[] minColSizes = { 15, 55, 75, 50, 50, 50 };
int[] preferredColSizes = { 25, 75, 125, 100, 100, 100 };
int[] maxSizes = { 50, 100, 200, 200, 200, 200 };
for ( int index = 0; index < preferredColSizes.length; index++ )
{
myTable.getColumnModel().getColumn(index).setMinWidth ( minColSizes[index] );
myTable.getColumnModel().getColumn(index).setMaxWidth ( preferredColSizes[index] + 100 );
myTable.getColumnModel().getColumn(index).setPreferredWidth ( maxSizes[index] );
}
精彩评论