如何恢复 JTable 中的列宽?
How to restore column widths in a JTable?
我的问题很明确。有什么方法可以在用户调整列宽后自动恢复列宽?
每次单击按钮时,我都希望恢复列宽。
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);
});
}
}
我试过了:
table.setAutoCreateColumnsFromModel(false);
table.setAutoCreateColumnsFromModel(true);
但是它删除了渲染器。
我试过了:
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
但什么都不做。
我试过了:
for (int col = 0; col < table.getColumnCount(); col++) {
table.getColumnModel().getColumn(col).sizeWidthToFit();
}
机器人什么都不做。
有办法吗?或者我必须自己计算然后 getColumnModel.getColumn(..).setPreferredWidth(..)
?
您需要自行管理。
默认情况下,宽度设置为 75,因此您可以将宽度重置为 75。
或者您可以在 GUI 可见后随时查询列的宽度。然后根据这个值恢复宽度。
没有以原始宽度显示列的默认功能
我的问题很明确。有什么方法可以在用户调整列宽后自动恢复列宽?
每次单击按钮时,我都希望恢复列宽。
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);
});
}
}
我试过了:
table.setAutoCreateColumnsFromModel(false);
table.setAutoCreateColumnsFromModel(true);
但是它删除了渲染器。
我试过了:
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
但什么都不做。
我试过了:
for (int col = 0; col < table.getColumnCount(); col++) {
table.getColumnModel().getColumn(col).sizeWidthToFit();
}
机器人什么都不做。
有办法吗?或者我必须自己计算然后 getColumnModel.getColumn(..).setPreferredWidth(..)
?
您需要自行管理。
默认情况下,宽度设置为 75,因此您可以将宽度重置为 75。
或者您可以在 GUI 可见后随时查询列的宽度。然后根据这个值恢复宽度。
没有以原始宽度显示列的默认功能