Home arrow static arrow Java Programming [Archive] - Problem about JComboBox
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Problem about JComboBox
This topic has 5 replies on 1 page.

Posts:35
Registered: 7/16/04
Problem about JComboBox   
Aug 8, 2004 4:57 AM



 
I have the following code :
I want to get the string I select in the combox !
Strings names[]={"A" ,"B" ,"C"};
JComboBox cb = new JComboBox (names);
cb.addItemListener (
new ItemListener (){
public void ItemStateChanged (){
Strings strTemp = cb.getSelectedItem().toString() ;
}
}
);

//I try to used " cb.getSelectedItem().toString() " , but it is not what I want !

 

Posts:18,384
Registered: 21.03.00
Re: Problem about JComboBox   
Aug 8, 2004 5:04 AM (reply 1 of 5)



 
Hi,

What do you mean by "not what I want"? That should give you the select item.

/Kaj
 

Posts:35
Registered: 7/16/04
Re: Problem about JComboBox   
Aug 8, 2004 5:30 AM (reply 2 of 5)



 
I mean if I select item "A" , what I want is that it return String "A" ,
and so does Item "B" and "C"
 

Posts:18,384
Registered: 21.03.00
Re: Problem about JComboBox   
Aug 8, 2004 6:19 AM (reply 3 of 5)



 
I have the following code :
I want to get the string I select in the combox !
Strings names[]={"A" ,"B" ,"C"};
JComboBox cb = new JComboBox (names);
cb.addItemListener (
new ItemListener (){
public void ItemStateChanged (){
Strings strTemp = cb.getSelectedItem().toString()
;
}
}
);

//I try to used " cb.getSelectedItem().toString() "
, but it is not what I want !

You shouldn't add an item listener, it should be an action listener (and call getSelectedItem in that listener)

/Kaj
 

Posts:1,135
Registered: 1/16/04
Re: Problem about JComboBox   
Aug 8, 2004 6:38 AM (reply 4 of 5)



 
Okay... Then check this out...
import java.awt.*;import java.awt.event.*;import javax.swing.*; public class testCombo {       JComboBox cb;	   public testCombo(){	   		  JFrame f = new JFrame();			  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	   		  String names[]={"A" ,"B" ,"C"};			  cb = new JComboBox (names);			  cb.addItemListener (new ItemListener (){			  					 public void itemStateChanged (ItemEvent ie){			  		 			 		if (ie.getStateChange() == ItemEvent.SELECTED){			  		 					   String strTemp = cb.getSelectedItem().toString() ;			  							   System.out.println("got it " + strTemp);}}});			  f.getContentPane().add(cb);			  f.pack();			  f.setVisible(true);	   }	   public static void main(String[] a){			 new testCombo();		}}


I added this line, so it returns the selected item only...
if (ie.getStateChange() == ItemEvent.SELECTED){			  		 					   String strTemp = cb.getSelectedItem().toString() ;

Is this what you are looking for ?

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:24,517
Registered: 98-02-27
Re: Problem about JComboBox   
Aug 8, 2004 7:49 AM (reply 5 of 5)



 
The reason it doesn't work is because you posted the question in the wrong forum. There is a Swing forum for Swing related questions.

See [url http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html]How to Use Combo Boxes[/url] for working examples.
 
This topic has 5 replies on 1 page.