|
-
February 13th, 2012, 01:20 PM
#1
jComboBox in specific JTable cell
I've a problem that is driving me crazy.
I have a classic JTable, with several columns. I need that a particular column, instead of simple texts values in its cell, it must contain a ComboBox. I searched A LOT, all I found was examples that would implement the same JComboBox in each cell of the column, that it's not what I need: I need that each cell of the column has a combo box with different values.
Can anyone give me some practical example of how to do it, please?
Thanks.
PS: I'm using NetBeans.
-
February 13th, 2012, 04:27 PM
#2
Re: jComboBox in specific JTable cell
Code:
TableColumn sportColumn = table.getColumnModel().getColumn(2);
...
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
i got this from docs.Oracle.. Here's the link http://docs.oracle.com/javase/tutori...nts/table.html
The pic looked promising but i cant grant you anything since i have no experience with jtables
Last edited by cens; February 13th, 2012 at 04:33 PM.
-
February 13th, 2012, 06:48 PM
#3
Re: jComboBox in specific JTable cell
@Cens: That gives the same JCombox for each row.
Really, I searched once and found several code examples including this one:
http://www.java2s.com/Code/Java/Swin...torExample.htm
And there's a slightly clearer example of how to use this code here: http://stackoverflow.com/questions/6...table-in-swing
-
February 14th, 2012, 05:38 AM
#4
Re: jComboBox in specific JTable cell
How do you specify which values belong to which row? Who is in charge of those, the table model?
You can write an extension of DefaultCellEditor. When getTableCellEditorComponent(...) is called, you have reference to the row, cell and model. If the model has capacity to define the combobox values, you can populate the combobox's model with desired values in that method.
This would be neat in the sense that it is dynamic, reuses the same code / combobox, yet however provides varying values per cell/row context. You do not need to worry about "all editors having reference to the same combobox", because any editor is alive only when triggered by user and user may edit only one cell at a time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|