Problems about JComboBox
Hi all, can anyone give me a hand in JComboBox please?
I am building a java application with Netbeans. this application includes some JComboBoxes, which are named as lanComboBox, techComboBox, etc. Now I want the techComboBox, for example, show me the specifications of lan from the file HIPPI.java, if I choose "HIPPI" on lanComboBox. Here are source code:
HIPPI.java
==========================
package design;
import java.awt.Graphics;
public class HIPPI {
public final String[ ] tech = {"HIPPI-800", "HIPPI-1600", "HIPPI-800 Serial", "HIPPI-1600 Serial", "HIPPI-6400"};
public final String[ ] cable = {"Copper Wire", "Optical Fibre" };
public final String[ ] topology = {"Bus"};
//public final String[ ] topology = {"Bus", "Token Ring", "Star"};
//public final String[ ] mediaAccess = {"Token Passing", "CSMA/CD"};
public final String[ ] speed = {"800", "1600", "64000"};
/** Creates a new instance of HIPPI */
public HIPPI() {
}
public class HippiPanel extends javax.swing.JPanel{
}
}
======================================
MainFrame.java
=================
private void lanComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lanComboBoxActionPerformed
int selectedIndex = lanComboBox.getSelectedIndex();
if(selectedIndex == 1){
System.out.println("HIPPI is selected!");
techComboBox = new JComboBox(hippi.tech);
}
}//GEN-LAST:event_lanComboBoxActionPerformed
====================================
The problem is, after I chose "HIPPI", the techComboBox doesn't load anything from "HIPPI.java", I had tried other ways, but still got nothing except errors.
Can anyone help me out here please? it is appreciated!!!
[1832 byte] By [
jack_hzya] at [2007-11-27 8:08:55]

# 5
Hi camickr,
If the requirement is to change some other combo box values as a result of selection of a value at the main combo box, why do you need to change the model everytime for it, and set the client property?
Instead, you can just listen to the selecion from a combobox, and the apply the required changes in the other combo boxes in the actionPerformed method of the main combo box.
I think what you are doing is same as mine, but you are penetrating the abstraction provided by Java.
Check out my sample:
// package
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @author Sayantan Roy
* Date : Jun 21, 2007
*
* This class creates and contains the combo boxes, which are synchronized, viz,
* values in 2nd and 3rd combo boxes will change when ever some option is chosen from
* the first combo box. It can be easily extended to reflect changes of 2nd and 3rd
* combo boxes, by including logic in actioPerformed method.
*
* Note that when 'Apple' is chosen in the main combo, 2nd and 3rd combo will show 'Fruit'
* and '1' respectively. When 'Dog' is chosen, it shows 'Animal' and '2' respectively. Finally when
* 'Banyan' is chosen, they display 'Tree' and '3' respectively.
*/
public class SynchroComboBox extends JPanel implements ActionListener {
/**
* This is the serialVersionUID
*/
private static final long serialVersionUID = 1;
/**
* This is the main combo box instance in our component.
*/
private JComboBox mainCombo;
/**
* This is the second combo box instance in our component, which will reflect changes
* of the main combo box.
*/
private JComboBox secondCombo;
/**
* This is the third combo box instance in our component, which will reflect changes
* of the main combo box.
*/
private JComboBox thirdCombo;
/**
* Method: init: This method creates the combo boxes and attaches it with our
* SynchroComboBox object.
*
* Parameters: none.
* Returns: void
*/
public void init(){
String[] values = {"Apple", "Dog", "Banyan"};
mainCombo = new JComboBox(values);
mainCombo.addActionListener(this);
String[] secondValues = {"Fruit", "Animal", "Tree"};
secondCombo = new JComboBox(secondValues);
//secondCombo.addActionListener(this);
String[] thirdValues = {"1", "2", "3"};
thirdCombo = new JComboBox(thirdValues);
//thirdCombo.addActionListener(this);
GridLayout gLO = new GridLayout(1,3);
gLO.setHgap(5);
setLayout(gLO);
add(mainCombo);
add(secondCombo);
add(thirdCombo);
//setPreferredSize(new Dimension(400,300));
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent evt) {
if(evt.getSource().equals(mainCombo)){ // checks if main combo is changed
if(mainCombo.getSelectedItem().equals("Apple")){
//'Apple' is chosen in main combo.
secondCombo.setSelectedItem("Fruit");
thirdCombo.setSelectedItem("1");
}else if(mainCombo.getSelectedItem().equals("Dog")){
// 'Dog' is chosen in main combo.
secondCombo.setSelectedItem("Animal");
thirdCombo.setSelectedItem("2");
}else if(mainCombo.getSelectedItem().equals("Banyan")){
// 'Banyan' is chosen in main combo.
secondCombo.setSelectedItem("Tree");
thirdCombo.setSelectedItem("3");
}
}
}
public static void main(String[] args){
JFrame frame = new JFrame("Synchronized Combos");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
SynchroComboBox sBox = new SynchroComboBox();
sBox.init();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300,100));
panel.add(sBox);
frame.getContentPane().add(panel);
frame.pack();
frame.show();
}
}