我创建了一个简单的注册表单,将Name,Gender和city添加到表中,但我在这里遇到了一些问题。 当我单击表单左上角的最大化按钮时,我的表格才会刷新。
这是代码: http://pastebin.com/dFnix5NY
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Main extends JFrame{
private Vector<String> columnNames = new Vector<String>();
private Vector data = new Vector<>();
private Vector<Vector> details = new Vector<Vector>();
private DefaultTableModel dtm = new DefaultTableModel(details, columnNames);
private JTable table = new JTable(dtm);
private JScrollPane spane = new JScrollPane(table);
private JTextField field1 = new JTextField("Nama");
private JTextField field2 = new JTextField("Gender");
private JTextField field3 = new JTextField("City");
private JButton ok = new JButton("Ok");
JPanel panelForm = new JPanel(new GridLayout(1,4));
Main(){
columnNames.add("Nama");
columnNames.add("city");
columnNames.add("Gender");
data.add("Dodi");
data.add("Jakrta");
data.add("Laki");
details.add(data);
panelForm.add(field1);
panelForm.add(field2);
panelForm.add(field3);
panelForm.add(ok);
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
data = new Vector<>();
data.add(field1.getText());
data.add(field2.getText());
data.add(field3.getText());
details.add(data);
}
});
dtm = new DefaultTableModel(details, columnNames);
table = new JTable(dtm);
spane.setViewportView(table);
add(panelForm, BorderLayout.SOUTH);
add(spane, BorderLayout.CENTER);
setTitle("Registration");
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
答案 0 :(得分:3)
将新行添加到TableModel
而不是Vector
,将details.add(data);
方法中的下一行actionPerformed()
替换为dtm.addRow(data);
。
向TableModel
添加行后,会触发事件以进行刷新。
答案 1 :(得分:0)
要添加行,您需要致电dtm.addRow(data);
并在DefaultTableModel
而不是列表中添加行。
因此更新的代码如下所示:
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
data = new Vector<>();
data.add(field1.getText());
data.add(field2.getText());
data.add(field3.getText());
dtm.addRow(data);
}
});