Reputation: 1193
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:
Thanks for your help.
Upvotes: 0
Views: 242
Reputation: 36621
If you are open for alternative UI's: a JXTable
from the SwingX
project has a UI which allows to hide certain columns, or bring them back afterwards. This is of course only useful if you only want to hide/show existing columns. If you want to add brand-new columns, I am afraid that the SwingX
UI is not sufficient.
For the adding/removal of rows, I would consider using a pop-up menu containing such actions. A bit similar to what Microsoft Excel offers. There you don't have a + or - sign either, but can find these actions by right clicking on the row number.
Upvotes: 2
Reputation: 347332
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 the JTableHeader
in 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 JTableHeader
and provide you the means to layout out the components you want. I'd use something like a BorderLayout
with the JTableHeader
in the South
position and your controls in the North
IMHO
Upvotes: 2