Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8293509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:51:04+00:00 2026-06-08T13:51:04+00:00

There is a JCheckBox called one and another called two . There is also

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:51:06+00:00Added an answer on June 8, 2026 at 1:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In IIS 5 & 6 there's a tab called Directory Security where one can
In WindowViewModel There is a property called statusBar In StatusBarViewModel , There is a
The first line of code, called on every one of my application's pages, is:
I've got a content type called contact. One user can create hundreds of contacts.
There are posts that touch on this question, but no one that I can
I took over another project and have fixed everything else except this one problem.
Is there a way to uncheck/check a checkbox within a webpage that is loaded
Is there a read only property for a checkbox? Because I can still tick
Is there a case when IsChecked property on WPF CheckBox null ? I guess
Is there any way to cancel a RadioButton or CheckBox's change in state before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.