I’d like to create a JTable with a UI similar to this:

where the “+” and “-” indicate buttons that add or remove the associated row or column. To ensure that the buttons align well with the existing rows and columns, it seems easiest to extend the table beyond the data portion to also include the buttons. So, the table above would have 5 rows and 6 columns.
Questions:
- Can I place buttons above the column headers in a JTable?
- My understanding is that I can’t remove the cell borders for the buttons and blank cells (see here) at the cell level. Then, are there alternative ways to make the buttons align with the columns or rows?
- Are there similar UI styles you’ve seen or used that permit the desired behavior? Any libraries? I know this can be accomplished with, e.g., a combo box for choosing a particular row or column to be deleted. However, I’d prefer styles where the option to add/remove a row/column is placed immediately next to the row/column.
Thanks for your help.
The short answer is (mostly) yes, the long answer, it’s a bit of work.
I would take advantage of the
JScrollPane‘s row header, see setRowHeader for more details.(Picture used from the JavaDocs)
This will require you to layout your components to match the height of each row.
The table header is a little more complicated and comes down to whether this is a custom table implementation or not.
If you intent to extend
JTable(or a sub component of), you should take a look into replacing the functionality of the configureEnclosingScrollPane (and corresponding unconfigureEnclosingScrollPane) method. This would allow you to wrap theJTableHeaderin a custom component and set it to the scroll pane’s column header position.If not, you will need to adjust the scroll pane’s column header yourself. Either way, the basic approach is the same.
You want a component, that can use the
JTableHeaderand provide you the means to layout out the components you want. I’d use something like aBorderLayoutwith theJTableHeaderin theSouthposition and your controls in theNorthIMHO