gpt4 book ai didi

java - 使用 Rob Camick 的 ListTableModel,但未显示 JTable

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:33 26 4
gpt4 key购买 nike

我正在使用 Camick 的 ListTableModelRowTableModel从这里http://tips4java.wordpress.com/2009/03/12/table-from-database/

但是,JTable 没有出现。有谁知道为什么?我没有收到任何错误。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class AddressBook {

JLabel name, address, phone, email;
JTextField nameField, addressField, phoneField, emailField;
JButton addPerson, addEntry, cancelEntry;
JTable table;
ListTableModel model;
JDialog addEntryDialog;
String[] headings = {"Name", "Address", "Phone #", "Email"};

AddressBook() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
} catch (ClassNotFoundException e) {
System.out.println(e);
}

try {
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/AddressBook", "addressbook", "addressbook");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

ResultSet rs = stmt.executeQuery("SELECT * FROM APP.ADDRESSBOOK");
ListTableModel model = ListTableModel.createModelFromResultSet(rs);

rs.close();
//resultset.close();
stmt.close();
con.close();

} catch(SQLException e){
System.err.println(e);
}

JFrame frame = new JFrame("Address Book");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.getContentPane().setLayout(new FlowLayout());

ButtonListener listener = new ButtonListener();

addPerson = new JButton("New Entry");
addPerson.addActionListener(listener);

table = new JTable(model);
JScrollPane scroll = new JScrollPane(table);
scroll.setPreferredSize(new Dimension(500, 490));

//Add a person Dialog

name = new JLabel("Name:");
address = new JLabel("Address:");
phone = new JLabel("Phone:");
email = new JLabel("Email:");

nameField = new JTextField(8);
addressField = new JTextField(8);
phoneField = new JTextField(8);
emailField = new JTextField(8);

addEntry = new JButton("Save");
addEntry.addActionListener(listener);
cancelEntry = new JButton("Cancel");
cancelEntry.addActionListener(listener);

addEntryDialog = new JDialog(frame, "Add a Person");
addEntryDialog.setSize(190, 300);
addEntryDialog.getContentPane().setLayout(new FlowLayout());

addEntryDialog.getContentPane().add(name);
addEntryDialog.getContentPane().add(nameField);
addEntryDialog.getContentPane().add(address);
addEntryDialog.getContentPane().add(addressField);
addEntryDialog.getContentPane().add(phone);
addEntryDialog.getContentPane().add(phoneField);
addEntryDialog.getContentPane().add(email);
addEntryDialog.getContentPane().add(emailField);
addEntryDialog.getContentPane().add(cancelEntry);
addEntryDialog.getContentPane().add(addEntry);

//end of Add a person dialog

frame.getContentPane().add(addPerson);
frame.getContentPane().add(table);
frame.setVisible(true);
}

class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent event) {
JButton source = (JButton) event.getSource();

if (source == addPerson) {
addEntryDialog.setVisible(true);
}
if (source == addEntry) {
//add to db
}
if (source == cancelEntry) {
addEntryDialog.setVisible(false);
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
new AddressBook();
}
});
}
}

最佳答案

再次查看您的代码,问题在于您已将“模型”变量定义为类变量和实例变量。实例变量包含来自 ResultSet 的数据。用于创建表的类变量为空。代码应该是:

//ListTableModel model = ListTableModel.createModelFromResultSet(rs); 
model = ListTableModel.createModelFromResultSet(rs);

关于java - 使用 Rob Camick 的 ListTableModel,但未显示 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3454220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com