简体   繁体   English

将 JPanel 行添加到 JTable 模型

[英]Adding a JPanel row to a JTable model

I am trying to add a jpanel as a row to my jtable, something that looks like this: table , the red buttons are supposed to be invisible till the edit button on the top right is clicked.我正在尝试将一个 jpanel 作为一行添加到我的 jtable 中,看起来像这样: table ,在单击右上角的编辑按钮之前,红色按钮应该是不可见的。

I tried something like this:我试过这样的事情:

JPanel row = new JPanel();
            row.setBackground(new Color(255, 255, 255, 0));
            row.setAutoscrolls(true);
            row.setBorder(new EmptyBorder(0, 0, 0, 0));
            row.setLayout(new TableLayout(new double[][]{
                    {TableLayout.FILL, TableLayout.FILL},
                    {TableLayout.PREFERRED}}));
            ((TableLayout)row.getLayout()).setHGap(0);
            ((TableLayout)row.getLayout()).setVGap(0);

            JLabel deleteRow = new JLabel();
            deleteRow.setText("");
            deleteRow.setIcon(new ImageIcon(getClass().getResource("/com/example/clinicsystem/pictures/remove.png")));
            JLabel rowText = new JLabel();
            rowText.setText(comboBoxPermissions.getSelectedItem().toString());
            rowText.setForeground(Color.black);
            rowText.setFont(new Font("Helvetica-Normal", Font.PLAIN, 14));
            rowText.setHorizontalAlignment(SwingConstants.CENTER);

            row.add(deleteRow, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
            row.add(rowText, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));

            model.addRow(new JPanel[]{row});

but when I run the project, I get this text inside the row where this panel is supposed to be:但是当我运行这个项目时,我在这个面板应该所在的行中得到了这个文本:

javax.swing.JPanel[,0,0,0x0,invalid,layout=info.clearthought.layout.TableLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.EmptyBorder@254d8187,flags=33554441,maximumSize=,minimumSize=,preferredSize=]

I get this text inside the row where this panel is supposed to be:我在这个面板应该所在的行中得到了这个文本:

By default render of the table will simply invoke the toString() method on the data in the TableModel , so you see the toString() value of the JPanel.默认情况下,表的渲染将简单地调用TableModel中数据的toString()方法,因此您会看到 JPanel 的 toString() 值。

A JTable is not designed to add a component to the TableModel. JTable并非旨在将组件添加到 TableModel。 It is designed for you to add data to the TableModel.它专为您向 TableModel 添加数据而设计。 The data is then rendered based on the type of data added to the model.然后根据添加到模型的数据类型呈现数据。

the red buttons are supposed to be invisible till the edit button on the top right is clicked.在单击右上角的编辑按钮之前,红色按钮应该是不可见的。

So you would need to add a column of data to represent the red buttons.因此,您需要添加一列数据来表示红色按钮。 Start by readingTable Button Column .从阅读Table Button Column开始。 It demonstrates how to add a column of buttons to the table and how to add an Action to be invoked when you click on the button.它演示了如何向表中添加一列按钮,以及如何添加单击按钮时要调用的操作。

If you don't want the column to be visible then you can remove the TableColumn from the TableColumnModel , after you create the table.如果您不希望该列可见,则可以在创建表后从TableColumnModel中删除TableColumn Then when the "edit" button is clicked, you can add the TableColumn back to the TableColumnModel .然后当单击“编辑”按钮时,您可以将TableColumn添加回TableColumnModel

The TableColumnModel has methods like removeTableColumn(...) and addTableColumn(..) to help with this. TableColumnModel有像removeTableColumn(...)addTableColumn(..)这样的方法来帮助解决这个问题。 You can also use the getColumn(...) method of the JTable to get the column to remove and save for future use.您还可以使用 JTable 的getColumn(...)方法来获取要删除的列并保存以备将来使用。

Read the section from the Swing tuturial on How to Use Table for more information about renderers and editors.阅读 Swing 教程中有关如何使用表的部分,了解有关渲染器和编辑器的更多信息。

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

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