Two JComboBox working together
Hello. I磎 trying to make 2 JComboBox who works like this:
- When the first box is selected to one value the second box should be changed(become a specified box - t1 or t2). But I cant really get this to work becouse the class is only running one time in my program and when I try to call the "secondBox()" method nothing happens. Need to add a return statement somehow or some kind of repaint from the class I磎 calling from?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
publicclass Buttonextends JPanel{
private JComboBox t1;
private JComboBox t2;
private JComboBox first;
// Constructor
public Button(){
//Creating the first box
firstBox();
// The second box is created when I have added this but it never response to my changes/actions in the first box
//secondBox();
}
publicvoid firstBox(){
first =new JComboBox();
first.addItem("Title1");
first.addItem("Title2");
add(first);
}
publicvoid secondBox(){
// if "Title1" is selected in the "first" box the t1 box is created
if(first.getSelectedItem() =="Title1"){
JComboBox t1 =new JComboBox();
t1.addItem("t1-1");
t1.addItem("t1-2");
t1.addItem("t1-3");
add(t1);
}
// if "Title2" is selected in the "first" box the t2 box is created instead
elseif(first.getSelectedItem() =="Title2"){
// Guess I should remove the first box first before adding the new one
remove(t1);
JComboBox t2 =new JComboBox();
t2.addItem("t2-1");
t2.addItem("t2-2");
t2.addItem("t2-3");
add(t2);
}
}
}
[3154 byte] By [
Hunter78a] at [2007-11-27 7:56:31]

# 3
Ok. Think I磎 nearly ready. But it磗 not working to 100%. When compiling I磎 not getting any errors but the warning:
"Warning form last compilation. Note *.java uses unchecked or unsafe operation"
And also when I selected the "f1-2" in my second box I磎 still getting it磗 "f1-1" who is selected with the call "secondBox.getSelectedItem()". Any idea why?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Option extends JFrame implements ActionListener{
private boolean status;
private JButton b;
private JComboBox firstBox;
private JComboBox secondBox;
private Hashtable itemList = new Hashtable();
private Object selected;
public Option(){
String[] fst = {"firstMain", "secondMain"};
firstBox = new JComboBox(fst);
firstBox.addActionListener(this);
firstBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
add(firstBox, BorderLayout.WEST );
String[] a = {"f1-1","f1-2","f1-3"};
itemList.put(fst[0],a);
String[] b = {"f2-1","f2-2","f2-3"};
itemList.put(fst[1],b);
secondBox = new JComboBox(a);
add(secondBox);
}
// calling this method from another class
public String getPath(){
String path = "";
if(firstBox.getSelectedItem().equals("firstMain")){
path = "sc/";
if(secondBox.getSelectedItem().equals("f1-2")){
path += "bt";
}
else if(secondBox.getSelectedItem().equals("f1-3")){
path += "bp";
}
else path += "bt";
}
// checking the values whos always the same. Cant figure out why.
// second: "f1-1"
// selected: null
System.out.println("second: "+secondBox.getSelectedItem());
System.out.println("SELECTED: "+selected);
return path;
}
public void actionPerformed(ActionEvent e){
selected = itemList.get(firstBox.getSelectedItem());
if(selected == null){
secondBox.setModel(new DefaultComboBoxModel());
}
else{
secondBox.setModel(new DefaultComboBoxModel((String[])selected));
}
}
public static void main(String[] args)
{
JFrame frame = new Option();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
Thanks in advance
edit: problem solved. Dunno how ^^
Message was edited by:
Hunter78