Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problems with ItemEvents

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Problems with ItemEvents

    I'm confused on how to use ItemEvents... the class contains four generic methods that all return generic results if the state of two radio buttons in a menu are changed. How do you use the ItemEvent to simply tell you what is now selected? Or should I be using a regular actionPerformed? Or menuEventListener maybe?

    Also, why is it that JRadioButtonMenuItem inherits the method isSelected from AbstractButton but my compiler says that it can't find the symbol isSelected in class javax.swing.JRadioButtonMenuItem when I try to use it?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problems with ItemEvents

    Quote Originally Posted by bgroenks96 View Post
    How do you use the ItemEvent to simply tell you what is now selected?
    I researched this recently and it would seem that the solution is to loop over all buttons to see which is selected. My personal solution was to addan action command to each button. Then call getSelection on the ButtonGroup which returns a ButtonModel. Then call getActionCommand on the Button Model.

    Also, why is it that JRadioButtonMenuItem inherits the method isSelected from AbstractButton but my compiler says that it can't find the symbol isSelected in class javax.swing.JRadioButtonMenuItem when I try to use it?
    Code and full and exact error message or it never happened.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Problems with ItemEvents

    Quote Originally Posted by Junky View Post
    Code and full and exact error message or it never happened.
    import javax.swing.*;
    import java.awt.event.*;
    class Test {
      JMenu error;
      JMenuBar menuBar;
      ButtonGroup bg;
      JRadioButtonMenuItem one,two;
      public void setUp() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        menuBar = new JMenuBar();
        error = new JMenu("Error");
        bg = new ButtonGroup();
        one = new JRadioButtonMenuItem("One");
        two = new JRadioButtonMenuItem("Two");
        bg.add(one);
        bg.add(two);
        one.addItemListener(new Listener());
        two.addItemListener(new Listener());
        error.add(one);
        error.add(two);
        menuBar.add(error);
        frame.setJMenuBar(menuBar);
        frame.add(panel);
        frame.setSize(600,600);
        frame.setVisible(true);
      }
      class Listener implements ItemListener {
        public void itemStateChanged(ItemEvent event) {
          if(one.isSelected) {
            System.out.println("One");
          } else if(two.isSelected) {
            System.out.println("Two");
          }
        }
      }
      public static void main(String[] args) {
        Test t = new Test();
        t.setUp();
      }
    }
    javac Test.java
    Test.java:30: cannot find symbol
    symbol : variable isSelected
    location: class javax.swing.JRadioButtonMenuItem
    if(one.isSelected) {
    ^
    Test.java:32: cannot find symbol
    symbol : variable isSelected
    location: class javax.swing.JRadioButtonMenuItem
    } else if(two.isSelected) {
    ^
    2 errors

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problems with ItemEvents

    Look closely at the code where the error points to - isSelected is a method, not a variable. Variables are accessed via myObject.myVariable Methods as myObject.myMethod()

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Problems with ItemEvents

    Quote Originally Posted by copeg View Post
    Look closely at the code where the error points to - isSelected is a method, not a variable. Variables are accessed via myObject.myVariable Methods as myObject.myMethod()
    Yeah just realized that.... stupidity isn't a fun feeling.

    I do that somewhat often. Type in a method and forget the () identifier.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problems with ItemEvents

    Quote Originally Posted by bgroenks96 View Post
    Yeah just realized that.... stupidity isn't a fun feeling.

    I do that somewhat often. Type in a method and forget the () identifier.
    Not stupidity - its called learning. The thing is, I could readily find the error because I myself have done this countless times through the years.

Similar Threads

  1. 2 problems...
    By Day2Day in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2010, 02:51 PM
  2. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  3. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  4. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM