Search code examples
javaswingjscrollpanevisibilityjcheckbox

Setting JScrollPane visible/invisible via JCheckBox not working


There is a JCheckBox called "one" and another called "two". There is also a JScrollPane called "sp". In it is a JTextArea. The point of the checkboxes is to hide and show certain parts of the program. I simplified the program and here I tediously explain what is supposed to happen just to make sure you understand the program.

This is supposed to happen:

Initially only one is visible and it is unselected. Whenever one is selected, two should be set visible. Whenever two is selected, sp should be set visible. When a checkbox is unselected, the corresponding component is set invisible. However, when one is unselected, sp is also set invisible. (one controlls two and sp).

The problem:

When one is selected, two is visible. But when two is selected, sp is not visible (it should be). When one is unselected while two is selected, two is invisible (this should happen). But when one is selected, two is visible and all of a sudden sp is now visible. After this point, the program functions as it was intended.

This however works with other JComponents (in replace of the JScrollPane).

What could be wrong?

package tests;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Checkboxscrollpane extends JPanel {

    private JCheckBox one, two;
    private JScrollPane sp;

    private Checkboxscrollpane() {
        Listener listener = new Listener();

        one = new JCheckBox();
        one.addActionListener(listener);
        add(one);

        two = new JCheckBox();
        two.addActionListener(listener);
        add(two);

        sp = new JScrollPane(new JTextArea("hello"));
        add(sp);

        one.setVisible(true);
        two.setVisible(false);
        sp.setVisible(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        one.setLocation(50, 50);
        two.setLocation(70, 70);
        sp.setLocation(90, 90);
    }

    private class Listener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == one) {
                two.setVisible(one.isSelected());
            }
            sp.setVisible(one.isSelected() && two.isSelected());
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(300, 200);
        frame.add(new Checkboxscrollpane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Solution

  • You should revalidate the Checkboxscrollpane panel.

    But you shouldn't set location of components on every paint event:

    setLayout(null);
    
        one.setSize(100, 20);
        two.setSize(100, 20);
        sp.setSize(100, 20);
        one.setLocation(50, 50);
        two.setLocation(70, 70);
        sp.setLocation(90, 90);
    

    And remove the public void paintComponent(Graphics g) { method.