Ditger
Ditger

Reputation: 29

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?

Upvotes: 0

Views: 648

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

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.

Upvotes: 3

Related Questions