Ditger Ditger - 1 month ago 7
Java Question

JPanel Checkbox

I wonder how i can hide the second drawOval when i check the checkbox:

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(175, 185, 80, 45);
g.drawOval(170, 185, 80, 45);


I'm a beginner in Java and i can't find on how to hide this.

So to be more specific

I have a checkbox in the design panel
and when i click the checkbox
the second drawOval should disappear.

And can this work with like 6 lines?

Answer

And can this work with like 6 lines?

No, you'll have to modify that code. Wrap the drawOval line that you want to draw or not draw based on the check box state in an if block. Inside the if boolean test -- check to see if the JCheckBox is checked, and if so, draw the oval. If not don't draw it -- simple!

if (myCheckBox.isSelected()) {
   g.drawOval(....)
}

Also add an ActionListener to the JCheckBox that does one thing: calls repaint() on the JPanel.