简体   繁体   English

无法将JPanel中包装的JButton添加到JTable中

[英]Unable to add JButtons wrapped in JPanel into JTable

I am having a JTable , where the final column of the table is for adding 2 buttons. 我有一个JTable ,该表的最后一列用于添加2个按钮。 Below is the format of my JTable . 下面是我的JTable的格式。

在此处输入图片说明

Below is my code 下面是我的代码

private class ViewLawyersDisplayData extends ComponentAdapter
     {
        @Override
        public void componentShown(ComponentEvent e) 
        {
            dbConnector = new DBHandler();
            dbConnector.makeConnection();

            ResultSet rs =  dbConnector.selectAllLawyerDetails();

            if(rs==null)
            {
                JOptionPane.showMessageDialog(null,"The table is empty");
            }
            else
            {
                try
                {
                    while(rs.next())
                    {
                        int id = rs.getInt("lawyer_id");
                        String name = rs.getString("Name");
                        String address = rs.getString("Address");
                        String email = rs.getString("Email");
                        String phone = rs.getString("Phone");

                        JButton update = new JButton("Update");
                        JButton delete = new JButton("Delete");

                        JPanel btnPanel = new JPanel();
                        btnPanel.setLayout(new FlowLayout());
                        btnPanel.add(update);
                        btnPanel.add(delete);

                        Object[]row = {id,name,address,email,phone,btnPanel};

                        DefaultTableModel model = (DefaultTableModel) viewLawyersTable.getModel();
                        model.addRow(row);
                    }
                }
                catch(SQLException sqlE)
                {
                    JOptionPane.showMessageDialog(null,sqlE.getLocalizedMessage());
                }
            }
        }
     }

However I am unable to add the buttons into the final columns. 但是,我无法将按钮添加到最后一栏中。 Instead of showing the buttons, it shows some error text in the column. 它没有显示按钮,而是在列中显示了一些错误文本。 Below is the error text it shows to me. 下面是显示给我的错误文本。

javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

How can I fix this issue? 如何解决此问题?

You are mixing the model and the view . 您正在混合模型视图 Note that the model is the data to be displayed. 请注意,模型是要显示的数据 In the case of id it is an integer value and in the case of name it is a String. id的情况下,它是一个整数值;在name的情况下,它是一个字符串。 You also have several other strings in your model. 您的模型中还有其他几个字符串。

The output you see is not an error message. 您看到的输出不是错误消息。 It is simply the result of calling toString() on your JPanel because you are treating it as part of the model rather than a displayable view. 这只是在JPanel上调用toString()的结果,因为您将其视为模型的一部分而不是可显示的视图。

Since your JPanel is supposed to display UI components, it is a view . 由于您的JPanel应该显示UI组件,因此它是一个view You need to read more about JTable in order to learn how to customize the view in each column. 您需要阅读有关JTable的更多信息,以了解如何在每一列中自定义视图。

You can't add Swing components to a table because a table is not used to display actual components. 您不能将Swing组件添加到表中,因为表不用于显示实际组件。

See Table Button Column for one solution that provides the renderer/editor for a button in a column of the table. 请参阅“ 表格按钮列”中的一种解决方案,该解决方案在表格的列中提供按钮的渲染器/编辑器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM