user1383134
user1383134

Reputation: 1

How to dynamically repaint JPanels in a JFrame?

I am in the process of making a simple java swing program that will allow the user to play blackjack. I have 10-15 classes that represent 1 "screen" in the program, they each extend JLayeredPane and implement action listener. I also have other classes for deck, card, round, player etc.

Then in another class has two properties: an array arra(which holds all the panels) and a Jframe one (which all the panels will go to), with the public static void main.

Then in that same class I do two things, a method which replaces panels:

public void screenInit(int i)
{
    one.setContentPane(arra[i-1]);
    one.invalidate();
    one.validate();
    one.repaint();
}

Then I create an object of this class in the psvm and start by putting in the first panel.

So my question is now that the first panel is up, which has the action listener attached to a JButton how does it access the JFrame created in the class holding the JFrame and the array so it can use the method above. Right now, I'm having to create a new frame everytime, so it builds on top of each other?

If this is not possible, how should I organise my classes in a way that multiple buttons from each panels will lead to one another?

Thanks

Upvotes: 0

Views: 671

Answers (1)

mKorbel
mKorbel

Reputation: 109813

1) in the case that you use Swing JComponents then you can remove code line one.invalidate(); because there no reason set status for current LayoutManager that JPanel isn't valid element of current displayed GUI

2) if you remove / modify / add JComponent(s) top the already visible Container, then I have success with code lines revalidate() and repaint()

3) nobody knows what's you real issue(s), nor someone can see code in your monitor, better would be to isolating the issue with repainting of GUI and edit your quesion with a SSCCE

Upvotes: 4

Related Questions