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]
# 1

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

I think my solution in this posting is what you are looking for:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=688505

camickra at 2007-7-12 19:51:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
Many thanks, camickr. also I am sorry for the formatting, this is my first time to post problem here.Anyway, thanks a lot, I will try your way.
jack_hzya at 2007-7-12 19:51:56 > top of Java-index,Desktop,Core GUI APIs...
# 3

Hello camickr

I had tried your way, but it is not working so well in my program, cos there are many comboBoxes need to work together, the source code become too complex to me.

So do you have another way that simply like:

if(selectedIndex == 1){

System.out.println("HIPPI is selected!");

techComboBox = new JComboBox(hippi.tech); // as easy as this please

}

also could you explain why the

techComboBox = new JComboBox(hippi.tech);

here does not work? I am buiding the application on NetBeans, is it because the NetBeans already initiated the techComboBox first?

Thanks for your patient.

Jack

jack_hzya at 2007-7-12 19:51:57 > top of Java-index,Desktop,Core GUI APIs...
# 4

> techComboBox = new JComboBox(hippi.tech); // as easy as this please

Thats basically what my example did:

comboBox.setModel(....);

That is a easy as it gets. My code was more generic in that it can handle 100 different combo box models in a single line of code.

If you want to use 100 if / else statements then go ahead, but thats a lot of code to write.

> also could you explain why the

> techComboBox = new JComboBox(hippi.tech);

> here does not work?

Creating a new combo box does not add it to the GUI.

That why my code uses models. You simply update the combo box with a new model. The "concept" of using models in the code example I gave you is as easy as it gets. I can't help you any further.

camickra at 2007-7-12 19:51:57 > top of Java-index,Desktop,Core GUI APIs...
# 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();

}

}

JavaStarter2897a at 2007-7-12 19:51:57 > top of Java-index,Desktop,Core GUI APIs...