Doan
Doan

Reputation: 208

Toggle the visibility of Jlabel and JPanel

I am trying to toggle the visibility of JLable and JPanel. Both are in initial case in visible state. After pressing a Button, they should be invisible for two seconds. After this time they are going to be visible again. In adddition, there are two checkBoxes on the JPanel, which are grouped by ButtonGroup. The selection should be cleared after pressing the Button.

For solving I wrote the following code:

package Aufgaben;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;

public class Aufgabe1 extends JFrame {

    private JPanel contentPane;
    private JPanel checkPanel;
    private JPanel pointPanel;

    private JRadioButton rdbtnJa;
    private JRadioButton rdbtnNein;
    private ButtonGroup btnGroup; 

    private JButton btnStart;
    private JLabel lblX;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Aufgabe1 frame = new Aufgabe1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Aufgabe1() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 700, 300);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnStart = new JButton("Start");
        btnStart.setBounds(0, 0, 684, 23);
        contentPane.add(btnStart);

            checkPanel = new JPanel();
            checkPanel.setBounds(0, 229, 684, 33);
            contentPane.add(checkPanel);

            btnGroup = new ButtonGroup();

            rdbtnJa = new JRadioButton("Ja");
            checkPanel.add(rdbtnJa);
            btnGroup.add(rdbtnJa);

            rdbtnNein = new JRadioButton("Nein");
            checkPanel.add(rdbtnNein);
            btnGroup.add(rdbtnNein);

        pointPanel = new JPanel(new BorderLayout());
        pointPanel.setBackground(Color.BLACK);
        pointPanel.setBounds(0, 23, 684, 209);
        contentPane.add(pointPanel);

        lblX = new JLabel("X");
        lblX.setForeground(Color.WHITE);
        lblX.setVerticalAlignment(JLabel.CENTER);
        lblX.setHorizontalAlignment(JLabel.CENTER);
        pointPanel.add(lblX, BorderLayout.CENTER);

        run();
    }
    public void run() {
        System.out.println("----------");
        System.out.println("Method run()");
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("btn actionPerformed()");
                createLightPoint();
            }
        });
    }

    private void createLightPoint() {
        System.out.println("----------");
        System.out.println("Method createLightPoint()");
        btnGroup.clearSelection();
        lblX.setVisible(false);
//        lblX.repaint();
//        pointPanel.repaint();
        checkPanel.setVisible(false);
//        checkPanel.repaint();

        System.out.println("Bevor Sleep");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("After Sleep");

        lblX.setVisible(true);
//        lblX.repaint();
        checkPanel.setVisible(true);
//        checkPanel.repaint();
    }

}

As you can see I tried to solve the problems (what I described above) over the function .setVisibility() as well .repaint(). I tried the functions .show() and .hide althought they are deprecatet as well.But in any case, it doesn't work.

I already read the following posts:

Show/Hide JLabel with button?

https://softwareengineering.stackexchange.com/questions/233068/is-better-show-hide-or-setvisiblebool-visible

But none of them could help the solve my problem.

I hope you can Help me.

Upvotes: 0

Views: 243

Answers (1)

tenorsax
tenorsax

Reputation: 21223

Don't use sleep() on Event Dispatch Thread - it will prevent the thread from processing painting and other UI related events and the UI will become frozen. See The Event Dispatch Thread tutorial for more details.

You can use Swing timer instead, see How to Use Swing Timers tutorial for examples.

Upvotes: 1

Related Questions