I want to update table when Button is clicked

别等时光非礼了梦想. 提交于 2019-12-02 11:47:41

问题


I am newbie of Swing.

I want to update table after click button (done button). I think data correct but the screen does not work.

The followings are explanation of my program

  1. check checkBoxes and the click Done button
  2. the bottom layer should change.
  3. there is no main

This is my code:

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import net.miginfocom.swing.MigLayout;

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

public class Gui extends JFrame {

    class Table extends JTable{
        public Table(DefaultTableModel model) {
            super(model);
        }
        public Class getColumnClass(int column) {
            switch (column) {
            case 1:
                return Boolean.class;
            default:
                return String.class;
            }
        }
    }

    private final int BANDNUM = 43;
    int[] checkBands;
    Object[][] data;
    Table table;
    JScrollPane upperScrollpane;
    JScrollPane downScrollPane;
    JSplitPane splitPane;

    public Gui() {

        // BASE SETTING
        setSize(900, 800);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(5, 5));

        JPanel northPanel = new JPanel(new MigLayout());

        // northPanel SETTING
        JLabel labelIp = new JLabel("IP");
        JTextField tFIp = new JTextField(20);

        JLabel labelMask = new JLabel("Subnet Mask");
        JTextField tFMask = new JTextField(20);

        JLabel labelPing = new JLabel("Ping");
        JTextField tFPing = new JTextField(10);

        JButton btnReady = new JButton("Ready");
        JLabel labelReady = new JLabel("NOT READY");

        northPanel.add(labelIp);
        northPanel.add(tFIp);
        northPanel.add(labelMask);
        northPanel.add(tFMask);
        northPanel.add(labelPing);
        northPanel.add(tFPing);
        northPanel.add(btnReady);
        northPanel.add(labelReady, "wrap");

        // upper scrollpane -> will be included in JsplitPane Upper Side
        JPanel checkPanel = new JPanel(new MigLayout());

        JCheckBox[] checkBoxes = new JCheckBox[BANDNUM];
        JButton doneButton = new JButton("DONE");

        for (int i = 0; i < BANDNUM; i++) {
            checkBoxes[i] = new JCheckBox("" + (i + 1));
            checkBoxes[i].setHorizontalTextPosition(SwingConstants.CENTER);
            checkBoxes[i].setVerticalTextPosition(SwingConstants.BOTTOM);
            if (i == 32) {
                checkPanel.add(checkBoxes[i], "wrap");
            } else if (i == 42) {
                checkPanel.add(checkBoxes[i], "wrap");
            } else {
                checkPanel.add(checkBoxes[i]);
            }
        }

        checkPanel.add(doneButton, "span 3");

        // startButton Action

        /////////////////////////////////////////
        //I think you should watch from this line!!!
        /////////////////////////////////////////
        doneButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // 1. CHECK WHAT ARE CLICKED
                int length = 0;
                for (int i = 0; i < BANDNUM; i++)
                    if (checkBoxes[i].isSelected())
                        length++;
                checkBands = new int[length];

                System.out.println(length);

                int k = 0;
                for (int i = 0; i < BANDNUM; i++) {
                    if (checkBoxes[i].isSelected()) {
                        checkBands[k++] = i + 1;
                    }
                }

                // 2. Ready for display
                data = new Object[length][6];
                for (int i = 0; i < length; i++) {
                    data[i][0] = checkBands[i];
                    data[i][1] = true;
                    data[i][2] = 1;
                    data[i][3] = 2;
                    data[i][4] = 3;
                    data[i][5] = 4;
                }

                // 3. display
                String[] colNames = { "BAND", "Test1", "Test2", "Test3",
                        "Test4", "Test5" };
                DefaultTableModel model = new DefaultTableModel(data, colNames);
                table = new Table(model);
                setVisible(true);
                table.repaint();
                downScrollPane.repaint();
                splitPane.repaint();
            }
        });

        // down scrollpane -> will be included in JsplitPane down Side
        String[] colNames = { "BAND", "Test1", "Test2", "Test3", "Test4",
                "Test5" };
        Object[][] data = { { null, null, null, null, null, null }};
        DefaultTableModel model = new DefaultTableModel(data, colNames);
        table = new Table(model);

        // include
        upperScrollpane = new JScrollPane(checkPanel);
        downScrollPane = new JScrollPane(table);

        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperScrollpane, downScrollPane);

        mainPanel.add(northPanel, BorderLayout.NORTH);
        mainPanel.add(splitPane, BorderLayout.CENTER);
        getContentPane().add(mainPanel);

        setVisible(true);

    }

}

回答1:


Instead of doing this...

String[] colNames = { "BAND", "Test1", "Test2", "Test3",
                    "Test4", "Test5" };
DefaultTableModel model = new DefaultTableModel(data, colNames);
table = new Table(model);

Simply update the existing model

DefaultTableModel model = (DefaultTableModel)table.getModel();
for (Object[] row : data) {
    model.addRow(row);
}

or simply

DefaultTableModel model = (DefaultTableModel)table.getModel();
for (int i = 0; i < length; i++) {
    data = new Object[6];
    data[0] = checkBands[i];
    data[1] = true;
    data[2] = 1;
    data[3] = 2;
    data[4] = 3;
    data[5] = 4;
    model.addRow(data);
}

This assumes that you want to keep adding new rows to the table. You can also use model.setRowCount(0) to clear the table first and then add new rows to it, if that's what you want to.

Swing works a principle of MVC (Model-View-Controller) which separates the view (the JTable) from the data/model (TableModel), this means that the JTable is not bound to the data and can easily be changed or modified by simply changing or modifying the table's model. This is an important concept to understand, as Swing makes a great deal of use of this methodology



来源:https://stackoverflow.com/questions/30117334/i-want-to-update-table-when-button-is-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!