无法将JPanel中包装的JButton添加到JTable中 [英] Unable to add JButtons wrapped in JPanel into JTable

查看:83
本文介绍了无法将JPanel中包装的JButton添加到JTable中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,该表的最后一列用于添加2个按钮.下面是我的JTable的格式.

I am having a JTable, where the final column of the table is for adding 2 buttons. Below is the format of my JTable.

下面是我的代码

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=]

如何解决此问题?

推荐答案

您不能将Swing组件添加到表中,因为不使用表来显示实际组件.

You can't add Swing components to a table because a table is not used to display actual components.

请参见表按钮列,其中提供了一种解决方案表格列中按钮的渲染器/编辑器.

See Table Button Column for one solution that provides the renderer/editor for a button in a column of the table.

这篇关于无法将JPanel中包装的JButton添加到JTable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆