【发布时间】:2021-08-19 21:41:09
【问题描述】:
我在 JScrollPane 中有一个 JTable。我只想在按下特定按钮时显示表格,否则不应该显示。
为了合并,我在声明时将 ScrollPane 的 setVisible 方法设置为 false,并在 JButton 的 actionPerformed 方法中将其设置为 true。
但是即使按下 JButton 也看不到 JTable。
这是我的代码:
public class TableSample{
private JTable table;
....
private void initialize() {
JScrollPane scrollPane = new JScrollPane();
table = new JTable(new DefaultTableModel(new Object[][] {{null, null}, {null, null}, }, new String[] {"column1", "column2"}));
scrollPane.setViewportView(table);
scrollPane.setVisible(false);
JButton button = new JButton("Show Table");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
....
scrollPane.setVisible(true);
....
}
});
....
}
我使用了组布局,因此 ScrollPane 被添加到具有组布局的框架中。
当我完全不更改setVisible 时,JTable 也是可见的(默认情况下true)
感谢任何帮助...
【问题讨论】:
-
用问题来问你的问题,而不是在 cmets 中。发布适当的minimal reproducible example 来演示问题。我们无法判断您是否真的将滚动窗格添加到框架中。您可能还需要 revalidate() 和 repaint() 面板。
-
@camickr 抱歉,我是这个平台的新手,不知道。但是是的 revaildate() 和 repaint() 对我有用。谢谢。你能把它写在答案中,以便我可以接受它作为正确答案
标签: java swing jtable jscrollpane