• Post Reply Bookmark Topic Watch Topic
  • New Topic

JButtons disappearing on click and reappearing when mouse-over occurs.

 
Steven Herther
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having issues with JButtons and painting methods, whenever I click a grid on the board, the buttons in the JPanel disappear. I've been looking at it for too long and decided to post here.
Here is the board class:

Here is the GUI cllass:

I know the code is messy in places, this will be fixed later, just need some pointers on how to solve the JButton issue.
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without the related classes, I can't check the flow of your program, but this is definitely not correct.
Steven Herther wrote:
the first of those adds panel to the WEST of the contentPane; the second forwards the call to add(...) to the contentPane and adds the same panel to the default CENTER, thus removing it from WEST.

Can you reduce the program to a SSCCE that demonstrates the problem?
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code you have shown, board is not added to any visible hierarchy. And what does the RunGUI class do?
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


By default a JFrame uses a BorderLayout. When you don't specify a constraint, the component is added to the BorderLayout.CENTER.




But then you also add a second component to the BorderLayout.CENTER. However, the layout manager will only manage the second component added to the CENTER, which is the "board".

So the solution is DON'T add two components to the CENTER.

Try the following:



Also, in your Board class you should be overrideing the "getPreferredSize()" method to return the dimension of the Board. Then the layout manager can do its job properly. Then:



You don't need the setSize(...) method, as the pack() method will do job for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!