使用 Rob Camick 的 ListTableModel,但 JTable 没有显示
我正在使用 Camick 的 ListTableModel 和 RowTableModel 从这里 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();
}
});
}
}
I'm Using Camick's ListTableModel and RowTableModel from here http://tips4java.wordpress.com/2009/03/12/table-from-database/
However, the JTable is not showing up. Does anyone know why? I'm not getting any errors.
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();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
再看一下您的代码,问题在于您已将“模型”变量定义为类变量和实例变量。实例变量包含来自 ResultSet 的数据。用于创建表的类变量为 null。代码应该是:
Taking a second look at your code the problem is that you have defined the "model" variable as a class variable and an instance variable. The instance variable contains the data from the ResultSet. The class variable which is used to to create the table, is null. The code should be:
好吧,您将其添加到滚动窗格,而不是将滚动窗格添加到内容窗格。您只是添加表格。
如果没有滚动窗格,您将不会有任何列标题。如果没有任何数据,表的首选大小是否为 0x0?
Well, you're adding it to a scrollpane, and not adding the scrollpane to the content pane. you're just adding the table.
without the scrollpane, you won't have any column headers. and without any data, does the table have a preferred size of 0x0?