我想问一下点击按钮(JTable
)时如何制作新的actionPerformed
?
我想以这种方式添加几个JTable
。
答案 0 :(得分:5)
然后没有理由recreate
或remove then add
JComponents
JTable
及其Model
的所有更新必须在Event Dispatch Thread上完成
答案 1 :(得分:4)
以下是如何创建一个的方法,自己弄清楚如何添加更多内容,因为您没有提供任何SSCCE:
编辑:
import java.awt.*;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Stack extends JFrame implements ActionListener{
JTable table;
JPanel panel = new JPanel();
JButton button = new JButton("Add");
String data[][]={{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"}, {"hey","hey"},{"hey","hey"}
,{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"}
,{"hey","hey"},{"hey","hey"}};
String columns[] = {"First","Second"};
public Stack(){
setLayout(new BorderLayout());
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.setPreferredSize(new Dimension(1024,768));
button.addActionListener(this);
add(panel,BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String [] a){
Stack s = new Stack();
s.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
s.pack();
s.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
//panel.removeAll();
table =new JTable(data,columns);
panel.add(new JScrollPane(table));
repaint();
revalidate();
}
}