Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Customize only the first row to JComboBox in JTable 1

Status
Not open for further replies.

rach18

Programmer
Feb 6, 2009
9
US
How can i customize only the first row to JComboBox in jTable? (Only one row in the JTable, not the whole column)

I tried using the following code:
EachRowEditor rEditor = new EachRowEditor(bti.BTIC.comboBox,false,bti.BTIC);
bti.BTIC.jTable.getColumn(0).setCellEditor(rEditor);

Here the whole column(0) was set to comboBox. When executed, when i click on any cell of column(0), the comboBox is displayed. That is, when the user clicks in a cell of the first column, the drop down list of comboBox is appearing. This behaviour is correct.

But I only need the whole row(0) to be set to JComboBox in JTable. So I tried using the following code:
for (int n=1;n<bti.BTIC.columns.length ; n++)
{
EachRowEditor rowEditor = new EachRowEditor(false,bti.BTIC);
rowEditor.add(0, new DefaultCellEditor(new JComboBox(bti.comboBox.getModel())));
bti.BTIC.jTable.getColumn(bti.BTIC.columns[n]).setCellEditor(rowEditor);
}

where JTable and JComboBox are defined/implemented under bti.BTIC.
Here i was able to achieve in setting the whole row(0) to comboBox. When executed, when i clicked on any cell of row(0), immediately, i'm not able to display the comboBox. The user has to click on that cell, type something to get the drop-down list of comboBox, OR double click on that cell to get the drop-down list.

My question is to get the drop-down list of comboBox when the user once clicks on the cell of a JTable row which is customized to JComboBox.

Any help would be appreciated.

 
You would set a TableCellEditor on the JTable. Sub-class the DefaultCellEditor for this, overriding the getTableCellEditorComponent method. This method supplies a column parameter (among others). If this parameter indicates column zero, return a custom editing component of you choice, otherwise delegate sourcing the component to the superclass.

Tim
 
... and I meant row, not column :) Return your customised editing component when the ROW parameter is zero.

Tim
 
Thanks Timw! :)
Overriding getTableCellEditorComponent method, helped me in resolving the issue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top