aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Disabling JButton until valid selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Disabling JButton until valid selection" Watch "Disabling JButton until valid selection" New topic
Author

Disabling JButton until valid selection

Neil Campbell
Greenhorn

Joined: Apr 12, 2004
Posts: 1
Hi,
I am working on the URLyBird assignment - but this is a more general question...
On my "Search Results" Page I have a JTable. I also have a JTextField, for the customer ID to be entered, and a JButton to "Book".
I'm looking to disable the JButton until:
- a row in the JTable has been selected
- and, an 8-Digit customer ID has been entered.
The JTable is setup so it cannot be edited, and only one row can be selected at a time.
I've had a play around with the API, but not getting anywhere.
Any hints would be much appreciated,
Neil
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hi Neil
Originally posted by Neil Campbell:
Hi,
I am working on the URLyBird assignment - but this is a more general question...
On my "Search Results" Page I have a JTable. I also have a JTextField, for the customer ID to be entered, and a JButton to "Book".
I'm looking to disable the JButton until:
- a row in the JTable has been selected
- and, an 8-Digit customer ID has been entered.
The JTable is setup so it cannot be edited, and only one row can be selected at a time.
I've had a play around with the API, but not getting anywhere.
Any hints would be much appreciated,
Neil

One way of doing is you disable the button when you show results using setEnable(false) at the beginning. Later when the events you want occur, then in those respective event handler methods(like item selection handler method for table), enable the button using setEnabled(true).
This is one approach. Good Luck.
Jason Hocker
Ranch Hand

Joined: Jul 23, 2003
Posts: 132
This is a nice detail. To what level should we put these details in our program?
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
The extent to which you should put in "nice features" like disabling the edit button if no record is highlighted is a judgement call. However, I think its a good idea to make sure you put in enough "features" to make sure the user can't crash your program simply by clicking a button.


Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Eben Hewitt
Author
Ranch Hand

Joined: Apr 16, 2004
Posts: 36
I agree with Anthony. I think it is important to take care of those kinds of details, as you would on a real system. I started to think of my project as a sort of code repository that I could go to later when I need some bit of functionality.


Eben Hewitt. SCJP, SCWCD, SCJD, SCJWSD for JEE 5, TOGAF 8 Certified Architect, author of Java SOA Cookbook (O'Reilly, 2009) and contributor to 97 Things Every Software Architect Should Know
Eben Hewitt
Author
Ranch Hand

Joined: Apr 16, 2004
Posts: 36
More specifically, if you are looking to enable the button in the way Satish describes, check out ListSelectionListener to determine if the user has picked a row.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Disabling JButton until valid selection