Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Disabling JButton until valid selection

 
Neil Campbell
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 697
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a nice detail. To what level should we put these details in our program?
 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eben Hewitt
Author
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic