Sign in to follow this  
JeffDesrosiers_88518

Small Java Swing repainting issue

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();
jpnBoard.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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this