点击按钮创建新的JTable?

时间:2012-09-04 13:25:38

标签: java swing jtable

我想问一下点击按钮(JTable)时如何制作新的actionPerformed? 我想以这种方式添加几个JTable

2 个答案:

答案 0 :(得分:5)

答案 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();
    }
}