Tobias Mai
Tobias Mai

Reputation: 111

How to change JTable with an ActionListener

How would one have to change the following code to update the JTable?

I would like to click on the search button and change the amount of columns and rows. And set values for each cell.

But after clicking the search button it shows a second JTable instead of updating the first JTable. I do not know what the problem is.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;


public class Test extends JFrame {

    Container c;

    public Test(){
        c= getContentPane();
        c.setLayout(new BorderLayout());

        //create table
        JTable table = new JTable(new MyTableModel());
        JScrollPane scrollPane = new JScrollPane(table);

        //create search button
        JButton search = new JButton("search");

        c.add(scrollPane, BorderLayout.WEST);
        c.add(search, BorderLayout.EAST);

        //create splitter
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane, search);
        splitPane.setResizeWeight(0.7);
        add(splitPane, BorderLayout.CENTER);

        //ActionListener
        ListenerSearch listenersearch = new ListenerSearch();
        search.addActionListener(listenersearch);
    }


    public class ListenerSearch implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String[] columnNames = {"First Name",
                    "Last Name",
                    "Sport",
                    "# of Years",
                    "Vegetarian","One More Column"};

            MyTableModel changedtable = new MyTableModel();
            changedtable.setString(columnNames);

            Object[][] data = {
                    {"new", "value",
                     "new", new Integer(10), "false", "new column"},
                    {"new", "value",
                     "new", new Integer(3), "true", "new column"},
                    {"new", "value",
                     "new", new Integer(2), "true", "new column"},
                    {"new", "value",
                     "new", new Integer(20), "true", "new column"},
                    {"new", "value",
                     "new", new Integer(10), "true", "new column"},
                    {"new", "value",
                     "new", new Integer(5), "true", "new column"},
                    {"new", "value",
                     "new", new Integer(3), "true", "new column"},
                    {"new", "value",
                     "new", new Integer(2), "true", "new column"},
                    {"new", "value",
                     "new", new Integer(20), "true", "new column"},
                    {"new", "value",
                     "new", new Integer(10), "true", "new column"}
            };

            changedtable.setObject(data);


            JTable table = new JTable(changedtable);
            JScrollPane scrollPane = new JScrollPane(table);

            c.add(scrollPane, BorderLayout.WEST);
            c.revalidate();

        }
    }


    public static class MyTableModel extends AbstractTableModel { 
            public String[] columnNames = {"First Name",
                                            "Last Name",
                                            "Sport",
                                            "# of Years",
                                            "Vegetarian"};

            public void setString(String[] columnNameshelper){
                columnNames=columnNameshelper;
            }

            public Object[][] data = {
            {"Kathy", "Smith",
             "Snowboarding", new Integer(5), "true"},
            {"John", "Doe",
             "Rowing", new Integer(3), "true"},
            {"Sue", "Black",
             "Knitting", new Integer(2), "true"},
            {"Jane", "White",
             "Speed reading", new Integer(20), "true"},
            {"Joe", "Brown",
             "Pool", new Integer(10), "true"}
            };

            public void setObject(Object[][] datahelper){
                data=datahelper;
            }


            public int getColumnCount() {
                return columnNames.length;
            }

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int col) {
                return columnNames[col];
            }

            public Object getValueAt(int row, int col) {
                return data[row][col];
            }

            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }


            public boolean isCellEditable(int row, int col) {
                if (col < 2) {
                    return false;
                } else {
                    return true;
                }
            }


            public void setValueAt(Object value, int row, int col) {
                data[row][col] = value;

            }
    }



    public static void main(String[] args) {
        Test fenster = new Test();
        fenster.setSize(800,400);
        fenster.setVisible(true);
        fenster.setTitle("Test");
        fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Upvotes: 1

Views: 1594

Answers (1)

Phiwa
Phiwa

Reputation: 319

Store the table in a variable (just like you do it with "Container c" and change this in the action listener instead of adding a new table to the container.

[...]

public class Test extends JFrame {

    Container c;
    JTable t


     [...]
     // In constructor:
     t = new JTable(new MyTableModel());


     [...]
     // In method actionPerformed

     // Do not create a new table, but instead fill the existing one with the changed data
     t.setModel(changedtable);
 }

Upvotes: 3

Related Questions