ZZ 5
ZZ 5

Reputation: 1954

Create new JTable with a button click?

I'd like to ask how to make new JTables when button clicked (actionPerformed)? I'd like to add several JTables in that way .

Upvotes: 4

Views: 4926

Answers (2)

Branislav Lazic
Branislav Lazic

Reputation: 14806

Here is the way how to create one, figure out by yourself how to add more, since you didn't provide any SSCCE:

Edited:

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

Upvotes: 4

mKorbel
mKorbel

Reputation: 109813

Upvotes: 5

Related Questions