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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JComboBox Problem [Java Swings]

Status
Not open for further replies.

hrgopal

Programmer
Jan 15, 2001
1
IN
Hi,
Problem with JComboBox. I have two comboBox both contains same number of items. if the element in one comboBox is changed the same index element is to be set in second comboBox. My problem is, if any one ComboBox contains duplicate items, the change is not reflecting in the combo box which contains duplicate items, it will be always at zero index only. Can anyone help me. Here i am sending the peice of code.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ComboTest extends JFrame {

public ComboTest(){

String[] strFirstCombo = { "One","Two","Three"};
String[] strSecondCombo = { "BOne","BOne","BThree"};
firstCombo = new JComboBox(strFirstCombo);
firstCombo.setEditable(true);
firstCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(isChainedEvent){
int iSelected = firstCombo.getSelectedIndex();
isChainedEvent = false;
secondCombo.setSelectedIndex(iSelected);
isChainedEvent = true;
System.out.println("Listener A Fired ");
}
}});


secondCombo = new JComboBox(strSecondCombo);
secondCombo.setEditable(true);
secondCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(isChainedEvent) {
int iSelected = secondCombo.getSelectedIndex();
isChainedEvent = false;
firstCombo.setSelectedIndex(iSelected);
isChainedEvent = true;
System.out.println("Listener B Fired ");
}
}});


JPanel fieldPanel = new JPanel(new GridLayout(0,1,0,5));
fieldPanel.setBorder(BorderFactory.createEmptyBorder(5,5,10,10));
fieldPanel.add(firstCombo);
fieldPanel.add(secondCombo);

this.setContentPane(fieldPanel);
this.pack();
// Center dialog on screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point loc = new Point((int)(screenSize.getWidth()-this.getWidth())/2,
(int)(screenSize.getHeight()-this.getHeight())/2);
this.setLocation(loc);
setVisible(true);

}

public static void main(String[] args){
new ComboTest();

}
private boolean isChainedEvent = true ;


JComboBox firstCombo;
JComboBox secondCombo;
}



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top