Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By kammce

Thread: My Checkbox program is not working properly

  1. #1
    newbie106 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default My Checkbox program is not working properly

    Hi,
    I'm new to java and I made a program that creates an oval and asks the user whether it should be filled or not. The checkbox starts checked just like I programmed it to be but when I click it, it does not check off however, the oval becomes unfilled. What's the problem?

    My code is:
    private JCheckBox checkbox;
    checkbox = new JCheckBox("Filled");
    checkbox.setSelected(true);
    add (checkbox, SOUTH);

    is there anything I need to add so the box would uncheck when I click it?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: My Checkbox program is not working properly

    An ActionListener would probably help out a lot. Check out the buttons Swing tutorial for more on this.

  3. #3
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: My Checkbox program is not working properly

    So you have a listener on the JCheckBox? it works when you attempt to uncheck it but it does not check off when you click it? are you getting any weird console feed back. Your listener is working since it becomes unfilled, but your component is not responding.

    Provide the console output if there is any and also provide the checkbox listener code (itemStateChanged).
    My API:
    Java Code:
    cat > a.out || cat > main.class

  4. #4
    newbie106 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: My Checkbox program is not working properly

    Well the function works just fine its just that when I click it it does not change from checked to unchecked. The full code is:

    import acm.graphics.*;
    import acm.program.GraphicsProgram;

    import javax.swing.*;

    import java.awt.event.*;
    public class InteractiveOvals extends GraphicsProgram {
    private JCheckBox checkbox;
    public void init(){
    checkbox = new JCheckBox("Filled");
    checkbox.setSelected(true);
    add (checkbox, SOUTH);
    addMouseListeners();
    addActionListeners();
    }

    public void mouseClicked(MouseEvent e){
    GOval obj;
    double diam = 40;
    if(checkbox.isSelected()){
    obj = new GOval(diam, diam);
    obj.setFilled(true);
    } else {
    obj= new GOval(diam, diam);
    obj.setFilled(false);
    }
    add (obj, e.getX(), e.getY());
    }


    }

    you can try it if you want, but I used the acm library because it is easy and I just started learning.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: My Checkbox program is not working properly

    <rant>Why do so many people think a forum devoted to Java can help them with code that uses classes not part of the JDK?</rant>

    Most of us don't have (nor want to have) the acm library. Maybe you should ask your question wherever you got it.

    Additionally, go through these:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    newbie106 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: My Checkbox program is not working properly

    Well, I'm not asking for help regarding the acm library. All what the acm library does is that it creates the oval and sets it to be filled or not and there's no problem with that. The part of the code that I'm asking for help with is the JCheckBox part which is in javax library that is part of the JDK. So I don't know what the problem with the acm library is or why you're angry! The entire problem is that the checkbox icon does not change when I click it.
    P.S if I posted this where I got the acm library they would say it's not part of their library and therefore can't help me.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: My Checkbox program is not working properly

    Quote Originally Posted by newbie106 View Post
    Well, I'm not asking for help regarding the acm library.
    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. An SSCCE that doesn't use the acm library.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: My Checkbox program is not working properly

    Why are you using MouseEvent for this? You could, but it is much simpler to use itemStateChanged, since it was built for checkBoxes and many other components. Check out this tutorial so you can adjust your code accordingly.

    How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Also, you did not put a listener on your JCheckBox. You added it init() which is an extension of GraphicsProgram() whatever that is. Check out the tutorial, it will help to figure out how to correctly place a listener on a JCheckBox.
    Fubarable likes this.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  9. #9
    newbie106 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: My Checkbox program is not working properly

    I solved the problem by removing the SOUTH from the add(checkbox) and now it works just fine
    Last edited by newbie106; 08-17-2012 at 10:14 PM.

Similar Threads

  1. for loop not working properly
    By lbgladson in forum New To Java
    Replies: 8
    Last Post: 10-15-2011, 12:33 AM
  2. TCP/IP client not working properly
    By Chrillz in forum Networking
    Replies: 2
    Last Post: 07-20-2011, 07:39 PM
  3. Place a working checkbox in a JTable header.
    By Vorrin in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2011, 01:39 PM
  4. date is not working properly
    By newnewgen in forum New To Java
    Replies: 1
    Last Post: 10-12-2010, 09:04 AM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •