• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic