Java JFrame does not update button settings

I am currently having a small problem with Java Jframe and the button is not updating.

I try to disable the print button until the print of the new JFrame is printed and that the JFrame is closed ...

The button will be disabled only when and when a new window appears, but will not until then, which may take a little time ....

I set the button to disable by doing the following: PrintBttn.setEnabled(false);

I tried calling mainPanel.revalidate(); mainPanel.repaint(); PrintBttn.revalidate(); PrintBttn.repaint, as well as the mixture above, as they recommended in other forums ...

I am now lost in this and why it doesn’t turn off the button until a new window appears, since the first thing I do is turn it off, as shown above, and then go through and create a new window ....

Thanks, Erik

+5
source share
2 answers

Most likely, it is a matter of freeing the EDT so that it can redraw the disabled button.

Typically, it will look something like this:

PrintBttn.setEnabled(false);
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        // Code to display the second JFrame goes here
    }
};
+6
source

You may also not be able to post your first frame in EDT, look at the code, this is what you really want:

import java.awt.event.*;

import javax.swing.*;

public class TwoFrames
{
    private JFrame frame1, frame2;
    private JPanel panel1, panel2;
    private JButton button1, button2, button3;
    private ActionListener action;

    public TwoFrames()
    {               
        frame1 = new JFrame("Frame One");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame2 = new JFrame("Frame Two");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel1 = new JPanel();      

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (ae.getSource() == button1)
                {
                    // Here goes your code for displaying your Second Frame.
                    SwingUtilities.invokeLater(new Runnable()
                    {
                        public void run()
                        {
                            if (!frame2.isShowing())
                            {                                                           
                                panel2 = new JPanel();

                                button2 = new JButton("Click Me to HIDE FRAME.");
                                button2.setHorizontalTextPosition(AbstractButton.CENTER);
                                button2.setVerticalTextPosition(AbstractButton.CENTER);
                                button2.addActionListener(action);

                                panel2.add(button2);
                                panel2.setOpaque(true);
                                frame2.setContentPane(panel2);

                                frame2.setSize(200, 200);
                                frame2.setLocationRelativeTo(null);
                                frame2.setVisible(true);
                            }
                        }
                    });             
                    button3.setEnabled(false);
                }
                else if (ae.getSource() == button2)
                {
                    frame2.dispose();
                    button3.setEnabled(true);
                }
            }       
        };

        button1 = new JButton("Click Me to Display FRAME.");
        button1.setHorizontalTextPosition(AbstractButton.CENTER);
        button1.setVerticalTextPosition(AbstractButton.CENTER);
        button1.addActionListener(action);          

        button3 = new JButton("Watch Me getting DISABLED");
        button3.setHorizontalTextPosition(AbstractButton.CENTER);
        button3.setVerticalTextPosition(AbstractButton.CENTER);
        button3.addActionListener(action);

        panel1.add(button1);
        panel1.add(button3);
        panel1.setOpaque(true);
        frame1.setContentPane(panel1);      

        frame1.setSize(200, 200);       

        frame1.setVisible(true);
    }

    public static void main(String... args)
    {
        // Here we are Scheducling a JOB for Event Dispatcher Thread.
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()               
            {
                new TwoFrames();
            }
        });
    }
}
+3
source

All Articles