Results 1 to 9 of 9
- 08-16-2012, 11:47 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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?
-
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.
- 08-17-2012, 04:51 AM #3
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
- 08-17-2012, 06:06 AM #4
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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.
- 08-17-2012, 07:48 AM #5
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-17-2012, 09:01 AM #6
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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.
- 08-17-2012, 09:47 AM #7
Re: My Checkbox program is not working properly
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-17-2012, 03:28 PM #8
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.My API:Java Code:cat > a.out || cat > main.class
- 08-17-2012, 09:44 PM #9
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
for loop not working properly
By lbgladson in forum New To JavaReplies: 8Last Post: 10-15-2011, 12:33 AM -
TCP/IP client not working properly
By Chrillz in forum NetworkingReplies: 2Last Post: 07-20-2011, 07:39 PM -
Place a working checkbox in a JTable header.
By Vorrin in forum AWT / SwingReplies: 1Last Post: 03-28-2011, 01:39 PM -
date is not working properly
By newnewgen in forum New To JavaReplies: 1Last Post: 10-12-2010, 09:04 AM -
Log4j not working properly....
By prakash_dev in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 12:13 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks