英文:
How to restore column widths in a JTable?
问题
My question is clear. Is there any way to restore the width of the columns automatically after the user has resized them?
Every time the button is clicked, I want the column widths to be restored.
public class TableTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JTable table = new JTable(new Object[][] { { "something", "something else" } },
new Object[] { "Col1", "Col2" });
table.getColumnModel().getColumn(0).setCellRenderer(new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
column);
label.setForeground(Color.GREEN);
return label;
}
});
frame.add(new JScrollPane(table), BorderLayout.CENTER);
JButton button = new JButton("Button");
button.addActionListener(e -> {
// Restore column widths here
});
frame.add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
}
I tried:
table.setAutoCreateColumnsFromModel(false);
table.setAutoCreateColumnsFromModel(true);
But it removes the renderer.
I tried:
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
But it does nothing.
I tried:
for (int col = 0; col < table.getColumnCount(); col++) {
table.getColumnModel().getColumn(col).sizeWidthToFit();
}
But it does nothing.
Is there a way? Or do I have to calculate it myself and then getColumnModel.getColumn(..).setPreferredWidth(..)
?
英文:
My question is clear. Is there any way to restore the width of the columns automatically after user has resized them?
Every time the button is clicked, I want the column widths to be restored.
public class TableTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JTable table = new JTable(new Object[][] { { "something", "something else" } },
new Object[] { "Col1", "Col2" });
table.getColumnModel().getColumn(0).setCellRenderer(new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
column);
label.setForeground(Color.GREEN);
return label;
}
});
frame.add(new JScrollPane(table), BorderLayout.CENTER);
JButton button = new JButton("Button");
button.addActionListener(e -> {
// Restore column widths here
});
frame.add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
}
I tried:
table.setAutoCreateColumnsFromModel(false);
table.setAutoCreateColumnsFromModel(true);
But it removes the renderer.
I tried:
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
But does nothing.
I tried:
for (int col = 0; col < table.getColumnCount(); col++) {
table.getColumnModel().getColumn(col).sizeWidthToFit();
}
Bot does nothing.
Is there a way? Or I have to calculate it myself and then getColumnModel.getColumn(..).setPreferredWidth(..)
?
答案1
得分: 2
你需要自行管理这个。
默认情况下,宽度设置为75,因此您可以将宽度重置为75。
或者,您可以在GUI可见后始终查询列的宽度,然后根据此值恢复宽度。
没有默认功能可以显示列的原始宽度。
英文:
You need to manage this yourself.
By default the width is set to 75, so you can just reset the width to 75.
Or you can always query the width of the column after the GUI is visible. Then restore the width based on this value.
There is no default functionality to display the column at its original width
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论