简体   繁体   English

如何使用ActionListener更改JTable

[英]How to change JTable with an ActionListener

How would one have to change the following code to update the JTable? 如何更改下面的代码以更新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. 但是,单击搜索按钮后,它将显示第二个JTable,而不是更新第一个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);
    }
}

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. 将表存储在变量中(就像使用“ Container c”一样,并在操作侦听器中进行更改,而不是向容器中添加新表。

[...]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM