将文本和图标(使用 MouseListener)添加到 JTable 列
我想实现以下功能,但我很困惑是否可以在 Java 中实现。如果是,那么如何?请帮助:
我想创建一个 JTable 类型的表格,其中表格的 第一行 包含列名称和每列中的图标,即第一行的每个单元格中的图标。单击该图标应该会导致从表中删除该列(可以使用 MouseListener??)。
我找到了许多解决方案,可以将按钮添加到 JTable 中的单元格,但没有一个解决方案描述将文本和图标(使用 MouseListener)添加到单元格。请看看是否可以提供帮助,非常感谢您的阅读。
I want to implement following functionality but I am confused if it's possible in Java. If yes, than how? Please help:
I want to create a JTable kind of table where 1st row of table contains column names and an icon in each column i.e. in each cell of 1st row. Clicking on that icon should lead to removal of that column from table (possible using MouseListener??).
I have found many solution where I can add button to a cell in JTable but none which describes adding both text and icon (with MouseListener) to a cell. Please see if you can help and thanks a lot for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个扩展
JLabel
的自定义TableCellRenderer
。此JLabel
可以使用图标创建(JLabel
可以在文本的右侧或左侧显示图标)。您将需要 getTableCellRendererComponent 来测试正在呈现的行是否是第一行,如果是,则设置图标,否则不设置。对于移除动作,可以在表格上添加一个
MouseListener
,在处理mouseClicked
方法时,可以通过测试来找到被点击的单元格。 rowAtPoint
和columnAtPoint
通过从mouseEvent.getX()
和mouseEvent.getY()
创建一个Point
代码>.如果您确定单击了带有图标的第一行,则可以从列模型中删除该列。如果第一行实际上是表头,则可以为
JTableHeader
创建相同的呈现器,并在该组件上设置MouseListener
。You can create a custom
TableCellRenderer
that extendsJLabel
. ThisJLabel
can be created with an icon (JLabel
can display icons, to the right or left of the text). You will want thegetTableCellRendererComponent
to test wether the row being rendered is the first or not, and if so, set the icon, otherwise do not.For the removal action, you can add a
MouseListener
on the table, and when processing themouseClicked
method, you can find the cell that was clicked in by testing therowAtPoint
andcolumnAtPoint
by creating aPoint
from themouseEvent.getX()
andmouseEvent.getY()
. If you determine the first row with the icon was clicked, you can remove the column from the column model.If by 1st row, you actually mean the table header, you can create the same renderer for the
JTableHeader
, and set theMouseListener
on that component.嗯,我不明白你的问题。
您的意思是表头,就像通过显示列名称和排序方向进行排序的方式一样吗?
如果是这样,那么您可以为表头使用自定义呈现器,并向表头添加 MouseListener 以确定单击了哪一列。您应该能够自定义 默认表头渲染器 做你想做的事。
或者你的意思是表中的第一行数据。如果是这样,那么您仍然需要使用自定义渲染器,但这次您将 MouseListener 添加到表而不是表头。
在这两种情况下,您都可以使用 TableColumnModel.removeColumn() 方法从表视图中删除列。
Well, I don't understand your question.
Do you mean the Table Header, like the way sorting works by displaying the column name and the sort direction?
If so then you use a custom renderer for the table header and add a MouseListener to the header to determine which column was clicked. You should be able to customize the Default Table Header Renderer to do what you want.
Or do you mean the first row of data in the table. If so then you still need to use a custom renderer but this time you add the MouseListener to the table not the table header.
In both cases you can use the TableColumnModel.removeColumn() method to remove the column from the view of the table.