是否可以在JTable中包含JButton?

时间:2023-01-28 22:30:42

I have a JTable that stores the results of a database query, so far so good. What I want is for the last column in each table to have a clickible JButton that will open the edit screen for the object represented in that row, and that means the button will need to know the details of the first column in the table from its own row (the ID from the database).

我有一个存储数据库查询结果的JTable,到目前为止一直很好。我想要的是每个表中的最后一列有一个可点击的JButton,它将打开该行中表示的对象的编辑屏幕,这意味着该按钮需要知道表中第一列的详细信息拥有行(数据库中的ID)。

Any advice? I already tried just adding JButtons but they turned into Text when I tried to run it.

任何建议?我已经尝试过添加JButton,但是当我尝试运行它时它们变成了Text。

3 个解决方案

#1


5  

Contrary to the tutorial above, there is a way to do this without a complicated positioning math, custom mouse listeners, and custom table models. Instead, it can be done with a single simple custom class following the pattern described here:

与上面的教程相反,有一种方法可以在没有复杂的定位数学,自定义鼠标监听器和自定义表模型的情况下完成此操作。相反,它可以使用一个简单的自定义类,遵循此处描述的模式:

http://web.archive.org/web/20100623105810/http://ivolo.mit.edu/post/A-Simple-Pattern-for-Embedding-Components-into-a-Swing-JTable.aspx

http://web.archive.org/web/20100623105810/http://ivolo.mit.edu/post/A-Simple-Pattern-for-Embedding-Components-into-a-Swing-JTable.aspx

#2


3  

You may also find my tutorial on a similar subject (in this case, using a JPanel) helpful as well: Custom JPanel cell with JButtons in JTable

你也可以找到关于类似主题的教程(在这种情况下,使用JPanel)也很有帮助:在JTable中使用JButton自定义JPanel单元

#3


1  

Take a look at Sun's introduction to the JTable component, specifically, the section about Editors and Renderers. It discusses the use of alternative CellRenderers, and CellEditors. What you'd need to do is create (or borrow) a ButtonCellRenderer and a ButtonCellEditor and then apply them to the column in question in your JTable. The examples found in the linked articles should give you all the information you need.

看一下Sun对JTable组件的介绍,特别是有关编辑器和渲染器的部分。它讨论了替代CellRenderers和CellEditor的使用。您需要做的是创建(或借用)ButtonCellRenderer和ButtonCellEditor,然后将它们应用到JTable中的相关列。链接文章中的示例应该为您提供所需的所有信息。

#1


5  

Contrary to the tutorial above, there is a way to do this without a complicated positioning math, custom mouse listeners, and custom table models. Instead, it can be done with a single simple custom class following the pattern described here:

与上面的教程相反,有一种方法可以在没有复杂的定位数学,自定义鼠标监听器和自定义表模型的情况下完成此操作。相反,它可以使用一个简单的自定义类,遵循此处描述的模式:

http://web.archive.org/web/20100623105810/http://ivolo.mit.edu/post/A-Simple-Pattern-for-Embedding-Components-into-a-Swing-JTable.aspx

http://web.archive.org/web/20100623105810/http://ivolo.mit.edu/post/A-Simple-Pattern-for-Embedding-Components-into-a-Swing-JTable.aspx

#2


3  

You may also find my tutorial on a similar subject (in this case, using a JPanel) helpful as well: Custom JPanel cell with JButtons in JTable

你也可以找到关于类似主题的教程(在这种情况下,使用JPanel)也很有帮助:在JTable中使用JButton自定义JPanel单元

#3


1  

Take a look at Sun's introduction to the JTable component, specifically, the section about Editors and Renderers. It discusses the use of alternative CellRenderers, and CellEditors. What you'd need to do is create (or borrow) a ButtonCellRenderer and a ButtonCellEditor and then apply them to the column in question in your JTable. The examples found in the linked articles should give you all the information you need.

看一下Sun对JTable组件的介绍,特别是有关编辑器和渲染器的部分。它讨论了替代CellRenderers和CellEditor的使用。您需要做的是创建(或借用)ButtonCellRenderer和ButtonCellEditor,然后将它们应用到JTable中的相关列。链接文章中的示例应该为您提供所需的所有信息。