【问题标题】:Swing table update value in the DB and reload table model数据库中的摆动表更新值并重新加载表模型
【发布时间】:2016-06-11 15:45:51
【问题描述】:

我想在数据库中更新一个摆动表的行值,清除模型,然后用新的更新值加载新模型。问题是当我单击按钮更新值时,模型被清除但表中没有显示数据。我做错了什么?

为了简化示例,已删除与表格无关的部分代码。

公共类 ShowValues 扩展 JFrame {

    //Attributes
    private DefaultTableModel model;
    private JTable table  = new JTable()
        {
            public boolean isCellEditable(int row, int column)
            {                
                    return false;               
            };
        };

    public ShowValues() {

        btnDeleteItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) 
            {
                int selectedRowNumber = table.getSelectedRow();
                String selectedItemId = (String) table.getModel().getValueAt(selectedRowNumber, 0);
                //code to change selected item's state to deleted in the DB
                model.setRowCount(0); //clear model
                showTable(); //load new model
                labelMsg.setText("Item deleted");   
            }   
        });
        btnDeleteItem.setBounds(200, 320, 203, 57);
        contentPane.add(btnDeleteItem);

    }


    public void showTable()
    {
        String columnNames[] = {"Item Id", "Item Name", "Item price"};

        //code to get the data form the DB

        String dataValues[][] = new String[itemNum][3];
        for(int i=0; i<itemNum; i++)
        {
            dataValues[i][0] = idItem[i];
            dataValues[i][1] = nameItem[i]; 
            dataValues[i][2] = priceItem[i]; 
        }

        model = new DefaultTableModel(dataValues, columnNames);
        table.setModel(model);
      scrollPane = new JScrollPane(table);
    scrollPane.setBounds(17, 20, 490, 205);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    topPanel.add(scrollPane);
    }

}

【问题讨论】:

    标签: java swing jtable


    【解决方案1】:

    //将所选项目的状态更改为在数据库中删除的代码

    为什么仅仅因为更改了行中一个属性的值就想重新加载所有数据?那不是很有效。

    只需使用setValueAt(...) 方法更改JTable 中的值即可。

    问题是当我点击按钮更新值时,模型被清除,但表格中没有数据显示

    问题是您创建了一个新的 JTable,但您从未将该表添加到 JScrollPane 的视口中。

    解决方案是不创建新的 JTable。只需创建一个新的 TableModel。然后你可以使用:

    table.setModel( theNewlyCreateTableModel );
    

    【讨论】:

    • 感谢您的回答。我将表格添加到 JScrollPane 的视口,但我没有发布该代码只是为了使示例更容易并专注于重新加载模型的特定任务。
    • I add the table to the viewport of the JScrollPane - 不,你没有! ActionListener 中的代码调用showTable() 方法,该方法创建一个新的JTable。该表永远不会添加到视口中。视口仍然包含没有数据的原始 JTable。
    • 好的。我已更新到 showTable 方法的最后几行,在该方法中我将表添加到 JScrollPane 的视口并创建一个 JTable。还是一样的问题。
    • 您是否重新粉刷了面板?您是否验证了模型实际上包含数据?发布的代码看起来很合理,但如果没有整个代码的上下文,我们就无法判断出了什么问题。 Parts of the code not relative to the table have been removed in order to make the example easier. - 发布随机代码行并不容易。发布一个正确的SSCCE 来证明问题。我们无权访问您的数据库,因此您需要对数据进行“硬编码”以模拟您正在做的事情。并且不要使用空布局!
    • 是的,它包含数据。如果我写 System.out.println("model "+model.getRowCount());它表示模型中的行号。重新粉刷面板是什么意思?也许这就是错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多