• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Olof Sturesson
Greenhorn
Posts: 9
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic