如何恢复JTable中的列宽度?

huangapple go评论75阅读模式
英文:

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(() -&gt; {
			JFrame frame = new JFrame(&quot;&quot;);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setLayout(new BorderLayout());

			JTable table = new JTable(new Object[][] { { &quot;something&quot;, &quot;something else&quot; } },
					new Object[] { &quot;Col1&quot;, &quot;Col2&quot; });
			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(&quot;Button&quot;);
			button.addActionListener(e -&gt; {
				// 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 &lt; 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

huangapple
  • 本文由 发表于 2020年8月11日 03:32:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63346826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定