Clearing my JFrame & JPanel in a new game

java, jframe, jpanel, swing

Solution

This works for me

panel.removeAll();
//add your elements
revalidate();
repaint();

Problem

I have a simple JPanel for tic-tac-toe, drawing lines... so class TTT extends JPanel, and holds a GameLogic object inside. all is good, application is a JFrame in Main adding TTT and all good. BUT, when i want to restart a new game, I call "restart" in my TTT which basiclly does : gameLogic = new GameLogic(); & repaint(); now my data array is clean and it should paint only lines.. any way Windows is not changed at all. I've tried everything with no luck. Any suggestions? GameBoard.java: ``` public class GameBoard extends javax.swing.JPanel { private GameBoardLogic GameLogic; //..... public void Restart() { GameLogic = new GameBoardLogic(); removeAll(); repaint(); } ``` Main.java: ``` public class Main { private static GameBoard TTT; private static JFrame application; public static void main(String[] args) { application = new JFrame("Tic-Tac-Tow"); TTT = new GameBoard(); application.add(TTT); application.setSize(350, 350); application.setVisible(true); //..... if ( JOptionPane.showConfirmDialog(null, "Do you want to play again?") == JOptionPane.YES_OPTION ) { application.removeAll(); TTT.Restart(); application.add(TTT); application.validate(); } ```

Original source