• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Toeknizer.nextElement for JComboBox  RSS feed

 
Afifah Hamidon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm using ` int TxtAge = Integer.parseInt(tfAge.getText().trim());`to get value from my textfield and search it in database.

Then, I'm using `Integer age = Integer.parseInt(stringTokenizer.nextElement().toString());` to go to next attributes in my database.

I have no problem using those codes for textfield but when I'm using the JComboBox the result won't display. How to use the StringTokenizer.nextElement for JComboBox? Is is the same with TextField?

`String sex=(String) stringTokenizer.nextElement();` ---> I tried this code but still failed
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Afifah,
Welcome to the Ranch!

You have posted a question related to Swing API, but in the JavaFX forum. Note that JavaFX is the new API for building desktop applications in Java. I have moved the topic to the Swing forum for you.

You need to first get the 'selected' value from the JComboBox. Note that, JTextField deals only with text, whereas JComboBox deals with objects. The method getSelectedItem() returns the selected object. In your case, if it is String, you can then continue to use the tokenizer methods on that.
 
Afifah Hamidon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh thank you very much for your help
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Afifah Hamidon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I still got problem to retrieve value from my JComboBox. Would please take a look at my code?
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you getting the current value from JComboBox? Which method are you invoking to get the value? Have you gone through the JComboBox API?
Also, check out the tutorial page to know more.
 
Afifah Hamidon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeahh i think i go through the tutorial already but by program still not running

This is my piece of the code. The user will have to choose the gender from the comboxbox then it will look through the data files and matched the values. The problem is is still cannot retrive value from combo box. It works prefectly when i use text field.


Thank you
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line no.36 has this in your code:

So, you are indeed getting the selected item from combo box. What is the problem then?
 
Afifah Hamidon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is I unable to retrieve the value from the JComboBox. I want display the result after retrieve the value from combo box.There are others 13 attributes but I just show one of them (Sex). I don't know what is wrong with my combo box
 
Maxim Karvonen
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use == to check object equality. You should not do this. And here is a recent discussion about string equaltiy.

I am also not sure what is ComboSex in "sex == ComboSex". It is a single occurrence of this name. Is it misspelled comboSex? In this case you are comparing String to a JComboBox. Of course, this will not yield true unless both are nulls. You should compare string with a selected value.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to understand your issue. Can you explain it?

Also, I would suggest you create a small runnable program that replicates this issue. This is called SSCCE.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!