Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Need Help on JComboBox and JTable interaction

  Asked By: Elliott    Date: Mar 04    Category: Java    Views: 1413

I want to take the selection from a JComboBox and have it highlight
the corresponding row in a JTable. I don't have a clue as to how
to do this. I currently have an array of Airport"NAMES" that are
listed in the combo box. I also have an array of Airport "Codes"
that correspond to the respective airportName. In my code, I update
a label to show the correct Airport Code. I want to take this
airport code and have it reflect (highlight and focus gotten)the
corresponding row in my table. Once I get the correct row selected
in the table, I need to use data from each of the fields in the
selected row to update other labels on my GUI.
In my table, the first column of the table has airport "CODES".
I have an itemListener on my ComboBox, but not sure how to have it
interact with the table. The updating of the label to the correct
Airport code works correctly.
//ADD ITEMListener to Airport Combo Box

cbAirport.addItemListener(new ItemListener(){
public void itemStateChanged (ItemEvent e)
if (e.getStateChange() == ItemEvent.SELECTED)
// Process airport selection:
for(int x=0; x < airports.length; x++ )
ACodeLabel.setText("Airport Code: "+

}//end of if statement

}//end of itemStateChanged



1 Answer Found

Answer #1    Answered By: Ana Silva     Answered On: Mar 04

I forgot to also show  my code  for my JTable (airportStuff). This is
where I think I should be coding for highlighting selected row
utilizing the value that is obtained by this:

airportCodes [cbAirport.getSelectedIndex()];

Here's my table  airportStuff listSelection Listener:

if (ALLOW_ROW_SELECTION) { // true by default
ListSelectionModel rowSM = airportStuff.getSelectionModel
rowSM.addListSelectionListener(new ListSelectionListener
() {
public void  valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) return;

ListSelectionModel lsm = (ListSelectionModel)
if (lsm.isSelectionEmpty()) {
} else {
int selectedRow = lsm.getMinSelectionIndex();
} else {
/* */

Didn't find what you were looking for? Find more on Need Help on JComboBox and JTable interaction Or get search suggestion and latest updates.