Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Can't make button visible in Java Swing

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a button called 'Play Again!' that I want to hide and then show when the user wins or loses and wants to play again.

This is my function:



If I make the button visible created (as a test) I can see it:



This is what I want the play again button to look like: https://pasteboard.co/JHb7jUf.png

Ordinarily it would start out as hidden:



This is how the window should look when the play again button is hidden: https://pasteboard.co/JHb8JI4.png



If I set the button visible where I want it in my function, the button does not become visible and the other messages (such as "You lost...") don't appear on the screen at all:




I'm declaring the button at the top of my class:



And this is the function that creates the window:



My questions are, why does the button not become visible when I set visible to true?

If I try to set visible in an if statement like this:



Why does the `message` not appear?

How can I set the button to visible correctly?

Here is all my code for the program (in case that helps):



 
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tim,

you have a Field called 'btnPlayAgain', but in your constructor you create another local JButton 'btnPlayAgain' (line 103).

In your method 'decrementNumberOfTries' you should check numBerOfTries. If that is 0, then you have the situation that the game is over. You should decide then what to do. For instance:

wait for a click on that button to start a new game. It may be wise to set the 'Guess' button to disabled or so, while a new game hasn't started.
 
Tim Dunphy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Tim,

you have a Field called 'btnPlayAgain', but in your constructor you create another local JButton 'btnPlayAgain' (line 103).

In your method 'decrementNumberOfTries' you should check numBerOfTries. If that is 0, then you have the situation that the game is over. You should decide then what to do. For instance:

wait for a click on that button to start a new game. It may be wise to set the 'Guess' button to disabled or so, while a new game hasn't started.



Ok thanks! If I remove JButton btnPlayAgain = new JButton("Play Again!"); and make it just btnPlayAgain = new JButton("Play Again!"); that works.

But how do I hide the btnGuess and txtGuess button and field? If I add that to the code near the message "You lost" or "Let's play again!" those messages don't appear in the window anymore!

Also how do I make it wait for the play again button to be clicked for it to create a new game? Here's my updated code so far:

 
Marshal
Posts: 71760
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Dunphy wrote:. . . how do I hide the btnGuess and txtGuess button and field?  . . .

Don't hide them. That would make for a very peculiar display, which changes its appearance frequently. Call setEnabled(false) on them. Or at least on the button.
 
Tim Dunphy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ok I tried that but it's still not working. The Guess button and text fields are still visible when the game ends and the play button shows.

 
Campbell Ritchie
Marshal
Posts: 71760
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Dunphy wrote:. . . The Guess button and text fields are still visible . . .

As I said, you don't want components appearing and disappearing. Disable them and they change their text colour to grey and your display remains legible. Same for the play again button.
By the way, give them pronounceable names: playAgainButton with the type last please, rather than btnPlayAgain.
 
Tim Dunphy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Tim Dunphy wrote:. . . The Guess button and text fields are still visible . . .

As I said, you don't want components appearing and disappearing. Disable them and they change their text colour to grey and your display remains legible. Same for the play again button.
By the way, give them pronounceable names: playAgainButton with the type last please, rather than btnPlayAgain.



Using setEnabled is ugly: https://pasteboard.co/JHcjMSS.png

However, setVisible works the way I want: https://pasteboard.co/JHck4xK.png

I'm just trying to get setVisible (on or off depending on where it is in the code) to work properly. That's what I need help with.

 
Piet Souris
Bartender
Posts: 4272
160
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was experimenting with a method 'processGameOver(String message)', that can be invoked whenever the game is over, either when the user guesses correctly or when max tries is reached.

Then think of what must be done: For instance:

1) disable the guess button and the textfield (in the waty Campbell described)
2) enable the playAgain button
3) do you have a label where you can display the message?
4) now wait for the playAgain button to be clicked; if so, call newGame where you enable the disabled components, et cetera
5) and maybe, add a JButton to end the game.

Lastly: can you indicate between what boundaries a guess is supposed to be?
 
Tim Dunphy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:I was experimenting with a method 'processGameOver(String message)', that can be invoked whenever the game is over, either when the user guesses correctly or when max tries is reached.

Then think of what must be done: For instance:

1) disable the guess button and the textfield (in the waty Campbell described)
2) enable the playAgain button
3) do you have a label where you can display the message?
4) now wait for the playAgain button to be clicked; if so, call newGame where you enable the disabled components, et cetera
5) and maybe, add a JButton to end the game.

Lastly: can you indicate between what boundaries a guess is supposed to be?



Thanks! I'll try that. I have to work on some other things, but I will try to get to this again later on today. Appreciate your input!
 
Tim Dunphy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Lastly: can you indicate between what boundaries a guess is supposed to be?



The boundaries for a guess are 1 to 100. But I frequently set them to 1 to 10 for testing. That makes testing a lot easier! That code is in the newGame() function.

 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you make these boundaries visible to the user?

But instead of all that messing with setEnabled/Visible, why not use a JOptionPane.showConfirmDialog (see the API)? That Dialog is modal, meaning that you can't proceed with your frame until you do something with that Dialog. I think that would make the code much easier.
 
Tim Dunphy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Do you make these boundaries visible to the user?


Yes. I have a JLabel for that:


But instead of all that messing with setEnabled/Visible, why not use a JOptionPane.showConfirmDialog (see the API)? That Dialog is modal, meaning that you can't proceed with your frame until you do something with that Dialog. I think that would make the code much easier.


That's an excellent suggestion! I am not familiar with JOptionPane.showConfirmDialog. But that sounds like I want! I'll have to look that up.
 
Campbell Ritchie
Marshal
Posts: 71760
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look in the Java™ Tutorials: this section.
 
Tim Dunphy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thank you. I will!
 
Tim Dunphy
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Do you make these boundaries visible to the user?

But instead of all that messing with setEnabled/Visible, why not use a JOptionPane.showConfirmDialog (see the API)? That Dialog is modal, meaning that you can't proceed with your frame until you do something with that Dialog. I think that would make the code much easier.



I got the code working the way I want. Many thanks to you and all who helped on this thread!

 
I child proofed my house but they still get in. Distract them with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic