我是Swing的新手。
我要在单击按钮(完成按钮)后更新表格。我认为数据正确,但屏幕无法正常工作。
以下是我程序的说明
这是我的代码:
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); } }
而不是这样做…
String[] colNames = { "BAND", "Test1", "Test2", "Test3", "Test4", "Test5" }; DefaultTableModel model = new DefaultTableModel(data, colNames); table = new Table(model);
只需更新现有模型
DefaultTableModel model = (DefaultTableModel)table.getModel(); for (Object[] row : data) { model.addRow(row); }
或简单地
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); }
假设您要继续向表中添加新行。如果您要这样做,还可以model.setRowCount(0)先清除表,然后再向其中添加新行。
model.setRowCount(0)
Swing的工作原理是MVC(模型-视图- 控制器),该视图将视图(JTable)与数据/模型(TableModel)分开,这意味着JTable不会绑定到数据,并且可以通过简单地更改或修改来轻松更改或修改。表格的模型。这是一个重要的概念,因为Swing大量使用了这种方法
JTable
TableModel