aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Button is greyed out until a item/element on a Jlist is checked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Button is greyed out until a item/element on a Jlist is checked" Watch "Button is greyed out until a item/element on a Jlist is checked" New topic
Author

Button is greyed out until a item/element on a Jlist is checked

Olof Sturesson
Greenhorn

Joined: Dec 10, 2013
Posts: 9

As the title states, is this possible?

I want the user to check an element in a Jlist, and only then be able to continue.

Example:

Press your favorite movie in a list, and then the user can choose to press the button "review". Before that, the button "review" is greyed out and you can't click on it.

Is this possible? Thanks in advance


In this moment, I am euphoric. Not because of any phony god's blessing. But because, I am enlightened by my intelligence.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You should be able to add a selection listener to your JList. When it fires its event (whose name I forget because I'm not looking at the API documentation) your code should enable or disable the button, depending on whether anything is selected.

Also it's pretty likely that the JList tutorial has a section about using a selection listener. (But I'm not looking at the tutorial either -- check it out.)
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Read the section from the Swing tutorial on How to Use Lists. The ListDemo would be a good place to start. Just make sure you invoke the setSelectedIndex(...) method after you add data to the list. Then you don't need to worry about enabling the button, since one item will always be selected.
Olof Sturesson
Greenhorn

Joined: Dec 10, 2013
Posts: 9

Never got it to work with the grey button thing, but thanks anyway

Rob Camick wrote:Read the section from the Swing tutorial on How to Use Lists. The ListDemo would be a good place to start. Just make sure you invoke the setSelectedIndex(...) method after you add data to the list. Then you don't need to worry about enabling the button, since one item will always be selected.


Alright I did as you told me:



and it worked just fine! thanks. But now I'm stuck once again.. How do I continue with this?

Let's say I've button that states "Information", that tells informtion about the movie. The user selects his choice in the JList and presses the "Information" button. Now I would like to have a JOptionPane or something that pop ups, but how can that be done? How can i "tell" the button which choice has been made in the JList


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
I gave you a link to the tutorial for a reason. Read the tutorial, download the example code and study the code. The ActionListener added to the "Fire" button will show you how to access the item that was selected. Although instead of using getSelectedIndex() it will probably be easier to use getSelectedItem().
Olof Sturesson
Greenhorn

Joined: Dec 10, 2013
Posts: 9

Rob Camick wrote:I gave you a link to the tutorial for a reason. Read the tutorial, download the example code and study the code. The ActionListener added to the "Fire" button will show you how to access the item that was selected. Although instead of using getSelectedIndex() it will probably be easier to use getSelectedItem().


Oh I must have missed it. Not sure how I'm going to use it, but i'll look into it. Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Button is greyed out until a item/element on a Jlist is checked