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

  • Home
  • SEARCH
  • 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 7513349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:09:31+00:00 2026-05-30T00:09:31+00:00

I’m working on a small Java applet, very much new to this programming thing.

  • 0

I’m working on a small Java applet, very much new to this programming thing. Using Java 1.6

I have a class that creates a JPanel of multiple checkboxes. The goal is to have the user select some fixed number of options, then pass those options to other methods

public class OptionSelector extends JPanel {

private JCheckBox[] optionCheckBoxes;
private int numberSelected;
private int goalNumber;
private ArrayList<String> selectedOptions = new ArrayList<String>();
private boolean rightAmountSelected;
private boolean isFinalChoice = false;
private DoneButton done = new DoneButton();

/**
 * This method is used only for the purposes of initialization
 *
 */
public OptionSelector()
{
    this(1, "test", "test");
}

public OptionSelector(int howManyToSelect, String ... options)
{
    super(new FlowLayout());
    JCheckBox option;
    optionCheckBoxes = new JCheckBox[options.length];
    for (int i = 0; i < options.length; i++)
    {
        option = new JCheckBox(options[i]);
        optionCheckBoxes[i] = option;
        add(option);
    }
    add(done);
    numberSelected = 0;
    goalNumber = howManyToSelect;
    rightAmountSelected = false;
}

public void setOptions(String ... options)
{
    this.removeAll();
    JCheckBox option;
    optionCheckBoxes = new JCheckBox[options.length];
    for (int i = 0; i < options.length; i++)
    {
        option = new JCheckBox(options[i]);
        optionCheckBoxes[i] = option;
        add(option);
        System.out.println(option.getBounds());
    }
    add(done);
    System.out.println(done.getBounds());
}

public void paintComponent(Graphics g)
{
    g.clearRect(0,0,this.getWidth(), this.getHeight());
    for (int i = 0; i < getComponentCount(); i++)
    {
        System.out.println(this.getComponents()[i].getBounds());
    }
}

The applet that I used to test it is below

public class TestDisplay extends JApplet implements MouseListener{

OptionSelector s = new OptionSelector();

public void init()
{
    setSize(640,480);
    this.getContentPane().setLayout(null);
    addMouseListener(this);
    s.setBounds(10,10,200,200);
    add(s);
    s.repaint();
}

public void paint(Graphics g)
{
    s.repaint();
}

public void mouseClicked(MouseEvent arg0) 
{
    s.setOptions("1","2");
    repaint();
}

public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

}

The problem is that when I test the applet, clicking the mouse changes the options just fine, but the new JCheckBoxes are not displayed. Experimenting with printing out the bounds of each component in the selector shows that the JCheckBox bounds are still stuck at (0,0,0,0), while the button’s bounds have not changed. I’ve tried repainting the OptionSelector and the entire applet many times, but nothing seems to work. Any ideas on how to fix this?

  • 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-05-30T00:09:34+00:00Added an answer on May 30, 2026 at 12:09 am

    You appear to be forgetting to call revalidate() on your JPanel container when removing and adding components:

    public void mouseClicked(MouseEvent arg0) {
      s.setOptions("1", "2");
      s.revalidate();
      repaint();
    }
    

    This is essential if you want the container (your JPanel) to layout the new components in the container appropriately. Also, this is not a good thing to do:

    public void paint(Graphics g) {
        s.repaint();
    }
    

    as you’re overriding the paint method of a top-level component for no good reason and without calling the super method. Just get rid of that method.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.