1

Hello my Japplet is using a JComboBox and 5 JRadioButtons to draw and paint on the applet. Currently everything works except for my JRadioButtons which doesn't call the itemStateChanged() when a button is selected. So on the applet I can click on a button, but it won't fire. My combobox is also using the itemlistener interface and it works, but no matter what I've tried I can't get the buttons to send information/fire.

I noticed that it requires two clicks to select a button, and hope that a problem lies within that.

This is for a homework problem and if I could use an actionperformed and actionlistener I would :(. I need to use itemlistener. Below are examples of how I'm calling my radiobuttons, adding them to the shapes buttongroup, and adidng the buttons to the container c.

Thanks for any help!

Sorry to anyone reading this but because it was homework I'm not 100% sure I can keep the code up, PM me if you need help understanding it!

n00begon
  • 3,503
  • 3
  • 29
  • 42
Howard Ho
  • 13
  • 3
  • The code you posted does not help us. I suggest you read the JRadioButton API and follow the link to the Swing tutorial on "How to Use Radio Buttons" where you will find a working example. If you still need help then post your SSCCE (http://sscce.org) that demonstrates the problem. – camickr Feb 23 '11 at 04:46
  • Almost every single guide online says to use an actionlistener which I'm not allowed to use. My problem isn't not knowing how to do it so much as not understanding why my itemlistner buttons aren't firing itemstatechanged when selected. Also I have a debugging counter eventfire that reads 0 even if I click the buttons 100 times. also I added code to the original post – Howard Ho Feb 23 '11 at 05:13

2 Answers2

1

In general it's a bad idea to use your Applet class for so many Listeners. It just adds to the confusion and you now have a God object that handles too many events. See this discussion for more info:

Advantages to Nested Classes For Listeners in GUIs

The second issue is that you are heavily mixing java.awt and javax.swing objects, which are known to cause problems when they are put in the same container. You should definitely try splitting your Applet into 2 JPanels, one for awt stuff (paint, shapes, etc) and one for swing stuff (buttons, boxes, etc).

You seem to be using the ItemListener class properly, but when I saw that it takes two clicks to select a button, that was an obvious sign of awt/swing mixing/painting problems.

Community
  • 1
  • 1
donnyton
  • 5,874
  • 9
  • 42
  • 60
  • Thanks this is exactly something that I was looking for. I won't have time to implement the changes but I will let you know by tomorrow if it worked. Would this "confusion" also cause the JRadioButtons not to activate the itemStateChanged? – Howard Ho Feb 23 '11 at 06:11
  • reminder to self: daily vote limit reached. up vote reply when allowed to vote again. – Hovercraft Full Of Eels Feb 23 '11 at 11:23
  • @Howard the confusion itself isn't the error--plenty of confusing code can still work. But right now it's not working, and the low readability of your code makes it confusing for us to actually FIND the part with the error. I guarantee this--event if you rewrite and organize your class and it STILL doesn't work, we will at least be able to pinpoint the problem in a second. – donnyton Feb 23 '11 at 18:07
1

The code you posted is NOT a SSCCE!

Your question is about an ItemListener, so why did you post code related to a MouseListener and a MouseMotionListener? What does the custom painting code have to do with your problem?

How do you know you the ItemListener code isn't being invoked? Did you add a System.out.println(...) statement to the listener code? Test your code first using the "appletviewer". Its easier than using the browser. From the command line all you do is:

appletviewer P6.html

Or I find it easer to test the applet without even creating an HTML file. YOu can add the following line of code to the top of your source file:

// <applet code="P6.class" width="800" height="600"></applet>

Now from the command you you can test the applet just by using:

appletviewer P6.java

The problem with you code is that your radio buttons are defined as both class and local variables. The ItemListener generates a NullPointerException, because the class variables are null.

JRadioButton jrbOval = new JRadioButton("Oval");    

should be:

jrbOval = new JRadioButton("Oval");    

Also, you should not be overriding the paint() method of JApplet. Custom painting is done by overriding the paintComponent() method of a JPanel. Then you add the panel to the applet.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Sorry I thought one of the SSCCE requirements was to be able to copy and paste and use it immediately. But anyways your answer is correct that was the problem. Thanks for the help! – Howard Ho Feb 23 '11 at 06:35
  • @Howard Ho, yes that is a requirement, but only with the code that specifically demonstrates the problem. Did the MouseListener cause the problem? No, so it should not be included. Its easier to debug 20 lines of code than it is to debug 100 lines of code. It is your job to simplify the code. – camickr Feb 23 '11 at 06:44