【问题标题】:JTable not updating itselfJTable 不更新自身
【发布时间】:2013-03-15 04:48:09
【问题描述】:

当我从组合框中的数据库中选择一个表时,我希望我的 jtable 自行更新。但是什么也没发生,程序给出了数组索引超出范围的异常。我使用了repaint()revalidate() 函数,但找不到解决方案。

编辑:我现在在这里有空指针异常:table.setModel(tableModel);

   public class DBC extends JFrame{

    static String tablo;
    static JTextField tf = new JTextField(20);
    static int columnCount;
    static JPanel tfPanel = new JPanel();
    static String[] sutunlar;
    static JLabel sutunLabel;
    static JPanel sutunPanel = new JPanel(new BorderLayout());
    static JTable table;
    static JScrollPane scrollPane;
    static DefaultTableModel tableModel;

    public static void main(String[] args) throws Exception {

        Class.forName("com.mysql.jdbc.Driver");
        Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/project"
                  ,"root","123456789");

        final Statement statement = connect.createStatement();

        JLabel tabloSec = new JLabel("Tablo Seçin:");
        final JComboBox<String> tablolar = new JComboBox<String>();
        final DatabaseMetaData md = connect.getMetaData();
        final ResultSet rs = md.getTables(null, null, "%", null);

        while (rs.next()) {
            tablolar.addItem(rs.getString(3));
        }

        tablolar.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {

                tablo = tablolar.getSelectedItem().toString();

                try {

                     ResultSet rs2 = statement.executeQuery("SELECT * FROM "+tablo);
                     ResultSetMetaData rsmd = rs2.getMetaData();
                     columnCount = rsmd.getColumnCount();

                     sutunlar = new String[columnCount];

                     Object columnNames[] = new Object[columnCount];
                     Object rowData[][] = {{""}};
                     tableModel = new DefaultTableModel(rowData, columnNames);

                         for(int i=0;i<columnCount;i++){

                             sutunlar[i] = rsmd.getColumnLabel(i+1);
                             columnNames[i] = sutunlar[i];

                             }

                         tableModel.addRow(rowData);
                         tableModel.addColumn(columnNames);
                         table.setModel(tableModel);
                         table.repaint();
                         scrollPane = new JScrollPane(table);
                         sutunPanel.add(scrollPane);
                         sutunPanel.revalidate();
                         sutunPanel.repaint();


                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        JButton ekle = new JButton("Ekle");
        ekle.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {

                try {
                    switch(tablo){
                    case "department":

                        statement.executeUpdate("INSERT INTO department(Name,Location) VALUES('"+tf.getText()+"')");
                    case "employee":

                        statement.executeUpdate("INSERT INTO employee(Id,FirstName,LastName,Sex,Address,Email,Salary,BirthDate,JoinDate) VALUES('"+tf.getText()+"')");
                    case "engineer":

                        statement.executeUpdate("INSERT INTO engineer(EngineerType) VALUES('"+tf.getText()+"')");
                    case "manager":

                        statement.executeUpdate("INSERT INTO manager(Department) VALUES('"+tf.getText()+"')");
                    case "project":

                        statement.executeUpdate("INSERT INTO project(Name,Number,Value) VALUES('"+tf.getText()+"')");
                    case "secretary":

                        statement.executeUpdate("INSERT INTO secretary(TypingSpeed) VALUES('"+tf.getText()+"')");
                    }

                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        JButton cik = new JButton("Çık");
        cik.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                System.exit(0);
            }
        });

        JPanel panel = new JPanel(new FlowLayout());

        panel.add(tabloSec);
        panel.add(tablolar);
        panel.add(sutunPanel);
        panel.revalidate();
        panel.add(ekle);
        panel.add(cik);

        JFrame frame = new JFrame("Deneme");
        frame.setSize(600,600);
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.setVisible(true);
      }
    }

【问题讨论】:

  • 在 SQL INSERT 的情况下缺少 break;

标签: java swing jtable refresh repaint


【解决方案1】:

从数据库中取回数据后,尝试添加 SwingUtilities.invokeLater()。它可能是一个事件调度问题。另外,不要在 actionListener() 中构造你的 GUI 组件(那肯定会导致问题)

【讨论】:

  • invokeLater() 不需要,因为所有代码都在 EDT 上执行。但是,在 EDT 上执行所有代码并不是一个好主意,因为这会使 GUI 在执行数据库调用时无响应。而是看看使用 SwingWorker。
【解决方案2】:

你不应该替换JTable

table = new JTable(1,columnCount);

改为更新现有的TableModel 或创建新的TableModel 并使用JTable#setModel 替换模型。

【讨论】:

  • 好的,我使用了表模型,但现在我有空指针异常。我编辑了我的帖子。
  • @Miral 第一次需要创建一个新的JTable。只有之后,您才能致电setModel。这解释了你的NullPointerException
  • 好的,罗宾,谢谢。但是我的代码创建一个 4 列 3 行的表是否正常。我只想要该特定数据库表的 2 列和 1 行,前 2 列带有 A 和 B 名称。
【解决方案3】:

但是没有任何反应,程序给出了数组索引超出范围的异常。

不要使用数组,因为您不知道将从 SQL 查询返回的数据量。

您应该使用 Vector 作为列名。您还需要为数据提供第二个向量。最后,对于返回的每一行,您都需要一个新的 Vector,将其添加到数据 Vector。

目前您的代码甚至没有尝试从 ResultSet 中获取数据。你只得到了不是很有用的列名。

获得所有数据后,您可以使用 columnName Vector 和 data Vector 创建一个新的 DefaultTableModel。然后你使用 table.setModel() 方法来更新你的表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多