Using Rob Camick's ListTableModel, but JTable doesnt show up

99封情书 提交于 2019-12-11 12:58:00

问题


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();
            }
        });
    }
}

回答1:


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); 



回答2:


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);


来源:https://stackoverflow.com/questions/3454220/using-rob-camicks-listtablemodel-but-jtable-doesnt-show-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!