0

I am currently creating a program and I am trying to make a JLabel come onto the JFrame after it is cleared from "Yes" being pressed on a JOptionPane. Currently, I have not been able to get it to work.`ready = new JLabel("Are you ready?"); ready.setToolTipText("Are you ready to begin?");

    }

private class thehandler implements ActionListener { 
    public void actionPerformed(ActionEvent event) {
        String string = "";
        Object[] options1 = {"Yes","No"};

        if (event.getSource()==pname) {
            string=String.format("You entered the name, %s, is this correct?", event.getActionCommand());

         int n = JOptionPane.showOptionDialog(null, string, "Is this name correct?", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, null);

        if (n == JOptionPane.YES_OPTION) {
            getContentPane().removeAll();
            revalidate();
            repaint();
            add(credits);
            add(ready);
        }`

To prevent confusion, there is more code above which basically declares private JLabel ready in public class window extends JFrame.

charrev
  • 129
  • 1
  • 6
  • 2
    *"To prevent confusion"* - in the future provide a compilable and runnable example ;) – MadProgrammer May 20 '15 at 22:52
  • Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). In fact, I'm tempted to close this as a duplicate of that Q&A. But I might just vote to close for lack of an [MCVE](http://stackoverflow.com/help/mcve). Choices, choices.. – Andrew Thompson May 20 '15 at 23:00

1 Answers1

2

Call revalidate and repaint AFTER you have finished making the changes to the UI (remove AND add)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you very much for the information. This worked for me and I will start troubleshooting a little bit more before I resort to stack overflow. You help is much appreciated. :) – charrev May 20 '15 at 22:51
  • Remember, Swing can be lazy, this is a deliberate design choice, as it would cause a lot of issues it each time you removed or added a component, it tried to revalidate the entire component hierarchy – MadProgrammer May 20 '15 at 22:56