Unable to add JButtons wrapped in JPanel into JTable

北城以北 提交于 2021-02-08 09:28:14

问题


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

enter image description here

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?


回答1:


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.




回答2:


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. 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.

Since your JPanel is supposed to display UI components, it is a view. You need to read more about JTable in order to learn how to customize the view in each column.



来源:https://stackoverflow.com/questions/25707854/unable-to-add-jbuttons-wrapped-in-jpanel-into-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!