Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to code button, that get selected element from JComboBox and perform operations for elements  RSS feed

 
Krystian Janik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I've decided to ask because i've spend an hour or two, pasting fragments from tutorials and nothing changed.

Well - i have GUI, that have JComboBox with two elements:



Below i have normal button (named Update) with "update" set as ActionCommand. This button after clicked should check which value from list is selected and perform different operations for that elements. But it's harder than i thought.

Code for update button:



What i want to do is:

- after user clicked Update button, button should check which value is selected in JComboBox (fedora_list).
- if Fedora 19 is selected and button clicked, then action in Xterm
- if Fedora 20 is selected, this same action in Xterm is performed, but for Fedora Linux 20

The problem is - i don't know hot to code Update button to check which value from fedora_list (JComboBox) is selected and perform other actions for every element on List.

Best Regards
Krystian
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the question. You have two items in the combo box so (as a simple solution) why can't you use 2 if statements?
 
Krystian Janik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You for reply.

Maybe i'll show fragment of this GUI (screenshot): https://www.dropbox.com/s/px0crr6k3tksa0g/the_problem.png

As You can see, there is a JComboBox (named fedora_list in my code) and have two elements - Fedora 19 and Fedora 20. Below is a "start upgrade" button, but i don't know how to code that button to check which Fedora is selected in JComboBox and perform one action for Fedora 19 and other for 20. I know how to code operations i want to execute but i don't know how Upgrade button should check which Fedora is selected :-(

I want to do something like:



But how Upgrade button should know which Fedora was selected? That's the question.

I'm learning Java from 2-3 weeks and my book was about basic-basic Java knowledge and leave me with tons of questions without answers (like above ) :-(
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how Upgrade button should know which Fedora was selected? That's the question.


You already have an if statement for "fedora 19". Why can't you add another if statement for "fedora 20"?
 
Krystian Janik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is, that this if not working - instead Eclipse give me ton of errors. I don't know how to code that if correct :-(

I've tried to getSelectedItem() from list and if list equals (or ==) Fedora 19 (seems logic to me) {

operations}

But it would be too easy so it not work - Eclipse gives errors. Gave up :-(
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Don't give up. Programming can be frustrating. Try posting the code that gives you the errors, and then posting the errors. Usually it's the first errors that is the most important.
 
Krystian Janik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You :-)

I've not gave up on coding but it's frustrating that simple actions can take so much trouble and i don't have knowledge to solve this (after completed Java book for begginers i still don't know nothing solid to code real apps).


Basically, this is the code for JComboBox with Fedoras list and Upgrade button that should check a Fedora version from ComboBox:



Now setting the Actions (problem is with update section, command was triggered to Start Upgrade button):



But when i run app and click Start Upgrade there is first JOptionPane Message and thats OK (it's at start, because i don't know how to code it to show after Xterm window is closed after upgrade). But Upgrade button not working, because after JOptionPane message i have:



I only want to set Upgrade button to check if Fedora 19 is selected then upgrade to 19, if 20 is selected, then upgrade to 20. But how to get value from ComboBox and set action to each of them? :-( I know how to code operations to trigger upgrade in Linux Xterm (code in try-cach), but i don't know how bind it to each of Fedoras from ComboBox.

PS: Full source code (if needed): http://pastebin.com/ugQ09Vyt
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thatnks for the full code; it was needed.

The part of the error that is most important is this:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at KFUpdate.actionPerformed(KFUpdate.java:98)


That tells us the error and the line of code. Using your full code link, the offending line is this:

String selectedFedora = (String) fedora_list.getSelectedItem();

I'm not a Swing expert, but I think that means nothing was selected. Do you know how to run a debugger? Put a break at line 98 and test fedora_list for a value.
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I think I misspoke (miswrote?) The NPE means that fedora_list is null, not what it returns.
 
Krystian Janik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


And Eclipe saying something that break cannot be used outside loop or switch :-(
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry. Not "break" the keyword, but a debugging break. If you don't know about the debugger in Eclipse, you could read up on that. Someone more clever than me may see why "fedora_list" is null. I'm still looking, though.
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got it, but let's see if you can get it too. Check everywhere there's a fedora_list. Somewhere two fedora_listss get created.
 
Krystian Janik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only know that fedora_list is a local type in constructor, but i don't know why it can't be seen outside it in ActionEvents.

Maybe it's too much for me and need to search better books - then bite again into Java... :-/ Maybe this will be funny but when i've coded in dynamic languages like JavaScript - it was more easy to send variables to other part of code :-(

EDIT:

OK, i've changed second line from:



to fedora_list = new JComboBox(fedoras);

And now in ActionEvents section
:


But i have problem with if (fedora_list == "Fedora 19") - Eclipse says: "Incompatible operand types JComboBox and String"

So MAYBE now variable is global? But what with incompatible type between JComboBox and String - String[] fedoras = { "Fedora 19", "Fedora 20" }; bind to JComboBox named fedora_list are Strings :-/

Ech :-)
 
Knute Snortum
Sheriff
Posts: 3553
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know about generics? Take a quick look at a Google search for them, but I will give you the bare bone:

Say you have a List, but a List of what? Before, it was just Objects, but that's not very type-safe. I could be thinking that they're Integer and then put a String in there.

So I think it was Java 5, that they decided to do something about that and they created generics (which is kind of a weird name for it, I think, because it's more like Specifics).

So for a List, the syntax is:



And in Java 7 I think they made it easier:



So this is a List of only Integers. If you try to put a String in there, you get a compiler. Before you would have to wait until run time.

So what do you think your JComboBox should be if it only takes Strings?
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics predates Java5 by at least twenty years. I suspect Java was released in a hurry in 1996 before they worked generics out properly and had to add it 8 years later.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!