Jump to content
  • Advertisement
Sign in to follow this  

Small Java Swing repainting issue

This topic is 2598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on a small Java Swing GUI chessboard program for a little while. Each of the 64 squares are their own JButtons. I do have Chessboard class that holds all the information of the game pieces, and a Swing class that extracts that information to paint the images on the JButtons and change the background color needed to show the available moves for each piece.
All of that is working fine; however, I wish to create a JButton that will reverse the view, so that a player can view the board the other way. My jbnReverse has its own ActionListener that easily calls a reverse function. It swaps the buttons from adding each button 0 - 63 to adding them 63 - 0. This works fine; however, for it to repaint on the GUI I have to click on another button. It then visually reverses the buttons. ATM I have this:

My Swing program extends JFrame
I have a JPanel called jpnMain with a BorderLayout
I have another JPanel call jpnBoard which has a GridLayout of 8, 8
I have jpnBoard added to jpnMain as BorderLayout.CENTER
I have a JPanel named jpnLower with a GridLayout 1, 1 added to jpnMain BorderLayout.PAGE_END
jpnLower has my Reverse Button

Now My JButton name jbnReverse has it own action Listener which calls the Swing class's method called reverseButtons

The reverseButtons method does the swap which does work, only after I click on another button.

My question is how do I make the buttons visually reverse automatically and not after I click another button.

I did try repaint();
I even tried after adding each JButton in the reverse method to repaint each JButton.

This is probally an easy fix, googled for a little while then said I can come here too.

Thx in advance.

Jeff Desrosiers

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!