[英]JTable not visible inside JScrollPane
我在 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
)
任何幫助表示贊賞...
當您將組件添加到可見 GUI 時,基本邏輯是:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // sometimes needed to make sure panel is repainted
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.