Reputation: 4115
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();
}
});
}
}
Upvotes: 1
Views: 955
Reputation: 324157
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:
//ListTableModel model = ListTableModel.createModelFromResultSet(rs);
model = ListTableModel.createModelFromResultSet(rs);
Upvotes: 3
Reputation: 25146
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?
frame.getContentPane().add(addPerson);
frame.getContentPane().add(**scroll**);
frame.setVisible(true);
Upvotes: 1