Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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: 5167
11
Netbeans IDE Opera Java
  • 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?
 
Rancher
Posts: 3191
29
  • 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.
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic