public class UserPage extends JFrame implements ActionListener {
JTable table;
UserModel model;
private JButton addButton;
public UserPage() {
UserJDBC userJDBC = new UserJDBC();
model = new UserModel(userJDBC.getData(), userJDBC.getColumn());
table = new JTable(model);
addButton = new JButton("Add User");
addButton.addActionListener(this);
add(addButton,BorderLayout.SOUTH);
add(new JScrollPane(table), BorderLayout.CENTER);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) addAction();
}
public void addAction() {
AddUserDialog UserDialog = new AddUserDialog(this);
model.addQuery(UserDialog.getfName(), UserDialog.getlName(), UserDialog.getGender(), UserDialog.getDate());
}
添加用户对话框类:
public class AddUserDialog extends JDialog implements ActionListener {
public AddUserDialog(JFrame owner) {
super(owner, "New User", true);
add(createForm(), BorderLayout.CENTER);
setVisible(true);
pack();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okBtn) {
okAction();
this.dispose();
}
}
public void okAction() {
setfName(fNameTf.getText()); // Get First name from text field
setlName(lNameTf.getText()); // Get Last name from text field
if (!genderSelection()) { // Get gender as radio buttons
return;
}
setDate(dateGenerate()); // Get date
}
型号类:
public class UserModel extends DefaultTableModel {
public UserModel(Vector<Vector<String>> data, Vector<String> column) {
super(data, column);
}
public void addQuery(String fname, String lname, String gender, String date) {
try {
Connection con;
Statement statement;
String query = "insert into UserTable(FirstName,LastName,Gender,Date) " +
"values ( '" + fname + "' , '" + lname + "' , '" + gender + "', '" + date + "' ) ";
con = DriverManager.getConnection(...);
statement = con.createStatement();
if (statement.execute(query)) {
UserJDBC userJDBC = new UserJDBC();
fireTableRowsInserted(userJDBC.getData().size() - 1, userJDBC.getData().size() - 1);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
我的 JDBC 类:
public class UserJDBC {
Vector<String> column = new Vector<String>();
Vector<Vector<String>> data = new Vector<Vector<String>>();
Connection connection = null;
Statement statement;
ResultSet resultSet;
public Vector<String> getColumn() {
// get column correctly
}
public Vector<Vector<String>> getData() {
try {
connection = DriverManager.getConnection(...);
statement = connection.createStatement();
resultSet = statement.executeQuery("select * from Table");
int c = resultSet.getMetaData().getColumnCount();
while (resultSet.next()) {
Vector<String> eachRow = new Vector<String>(c);
for (int i = 1; i <= c; i++) {
eachRow.add(resultSet.getString(i));
}
data.add(eachRow);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return data;
}
}
现在当我想添加一个新用户时,我的 GUI 需要刷新以显示新行!
更新:
public void addQuery(String fname, String lname, String gender, String date) {
try {
Connection con;
Statement statement;
String query = "insert into UserTable(FirstName,LastName,Gender,Date) " +
"values ( '" + fname + "' , '" + lname + "' , '" + gender + "', '" + date + "' ) ";
con = DriverManager.getConnection(...);
statement = con.createStatement();
if (statement.execute(query)) {
Object[] addedRow = new Object[]{fname,lname,gender,date};
Vector addedVector = new Vector();
addedVector.addElement(addedRow);
this.addRow(addedRow); // this?
this.addRow(addedVector); // Or this?
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}