Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jbutton  RSS feed

James Smith
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im reading the dietel java book right now. On chapter 6 theres an example using a jbutton in a game of craps. The action listener is applied to the jbutton.
My problem is this:
in the actionlistener method, the first line is if (firstroll)
call rolldice();
firstroll was initialized in the class as a boolean set = true
the book explains it as true means its the start of a new game, and false means continue the game.
is this the regular technique of utilizing jbuttons? with a booleans true/false way of doing things?
Gregg Bolinger
Ranch Hand
Posts: 15304
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not 100% sure I follow your question. But if I DO understand right....
I think they used that to basically check the "state" of the game at that point in time. GUI are event driven. In being that, the application state is important.
If they hadn't checked if the game was a continued game or a new game, they would have had to have 2 buttons.
It is very common to do similar actions when using JButtons.
It seems I almost always have an "if" statement or case/switch in my button action method.
It is by no means required. If you don't have anything to check, before performing your action based on the Button click, then there is no need for it.
Another option is disabling and enabling your JButton based on the "state" of your application. I also utilize this quite a bit.
This way, if the user isn't suposed to click that button, they can't even by accident.
But with Deitels example it looks like they needed to see what state the game was in on the Button Click, so thier method is perfectly acceptable.
Sorry so long winded.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!