使用 Rob Camick 的 ListTableModel,但 JTable 没有显示

发布于 2024-09-13 23:12:33 字数 4279 浏览 4 评论 0原文

我正在使用 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();
            }
        });
    }
}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2024-09-20 23:12:33

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

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

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); 
迟到的我 2024-09-20 23:12:33

好吧,您将其添加到滚动窗格,而不是将滚动窗格添加到内容窗格。您只是添加表格。

如果没有滚动窗格,您将不会有任何列标题。如果没有任何数据,表的首选大小是否为 0x0?

    frame.getContentPane().add(addPerson); 
    frame.getContentPane().add(**scroll**); 
    frame.setVisible(true);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文