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.
You can create a custom
TableCellRendererthat extendsJLabel. ThisJLabelcan be created with an icon (JLabelcan display icons, to the right or left of the text). You will want thegetTableCellRendererComponentto 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
MouseListeneron the table, and when processing themouseClickedmethod, you can find the cell that was clicked in by testing therowAtPointandcolumnAtPointby creating aPointfrom 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 theMouseListeneron that component.