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?
You appear to be forgetting to call
revalidate()on your JPanel container when removing and adding components: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:
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.