Logo 
Search:

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: 1514
  

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: "+
airportCodes[cbAirport.getSelectedIndex()]);

break;
}
repaint();
}//end of if statement

}//end of itemStateChanged
});

Share: 

 

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:

/**/
airportStuff.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
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)
e.getSource();
if (lsm.isSelectionEmpty()) {
return;
} else {
int selectedRow = lsm.getMinSelectionIndex();
airportStuff.setSelectionBackground
(Color.yellow);
}
}
});
} else {
airportStuff.setRowSelectionAllowed(false);
}
/* */

 
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.




Tagged: