Results 1 to 5 of 5

Thread: How to write an Item Listener in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    122

    How to write an Item Listener in Java?

    Hi friends,
    I have recently started doing Java coding. So i am not having much knowledge about the Java. Before that I have done C++ and C# programming languages. Now in Java, I want Item Listener so that item events can be handled. So please tell me how to write an Item Listener in Java.?? Hoping that someone would help me soon.!!
    Last edited by shivendra; 13-02-2010 at 12:39 AM.
    In the end, we will remember not the words of our enemies, but the silence of our friends.
    -Martin Luther King Jr.

  2. #2
    Join Date
    Aug 2006
    Posts
    227

    Re: How to write an Item Listener in Java?

    Since you don't know much about the Item Listener, I think you should know the basic concepts about that. Otherwise you will find hard while doing the coding. Item events are fired by components that implement the ItemSelectable interface. Normally, the on/off state for one or more items are maintained by an ItemSelectable components. Buttons like check boxes, check menu items, toggle buttons, etc are included in the Swing components that fire item events. Hope that you got the point that when exactly to use the Item Listener.!!
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  3. #3
    Join Date
    Aug 2006
    Posts
    235

    Re: How to write an Item Listener in Java?

    I have provided you with the sample of coding that explains how to write an item-event handling code, have a glace on this code :
    Code:
    checkbox.addItemListener(this);
    ...
    public void itemStateChanged(ItemEvent ie) {
        if (ie.getStateChange() == ItemEvent.SELECTED) {
            label.setVisible(true);
            ...
        } else {
            label.setVisible(false);
        }
    }
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  4. #4
    Join Date
    Jul 2006
    Posts
    289

    Re: How to write an Item Listener in Java?

    You should also know the methods that are used for an Item Listener. ItemListener has only one method. So it is not having any corresponding adapter class. The following is the method of an ItemListener Interface :
    • itemStateChanged(ItemEvent) - This method is called just after a state change in the listened-to component.

    And the following are the methods for an ItemEvent Class :
    • Object getItem() - This method returns the component-specific object associated with the item whose state changed.
    • getItemSelectable() - This method returns the component that fired the item event.
    • int getStateChange() - This method is used for returning the new state of the item.
    Signatures reduce available bandwidth

  5. #5
    Join Date
    Jul 2006
    Posts
    442

    Re: How to write an Item Listener in Java?

    You can create a toggle button with the help of an ItemListener. The following code explains the same :
    Code:
    import java.awt.BorderLayout;
    import java.awt.event.ItemEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.ItemListener;
    import java.awt.event.WindowEvent;
    import javax.swing.AbstractButton;
    import javax.swing.ButtonGroup;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JToggleButton;
    
    public class ToggleButtonDemo extends JFrame {
      public static void main(String[] args) {
        ToggleButtonDemo that = new ToggleButtonDemo();
        that.setVisible(true);
      }
    
      public ToggleButtonDemo() {
        setSize(520, 435);
    
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().add(new TogglePanel(), BorderLayout.NORTH);
      }
    }
    
    class TogglePanel extends JPanel {
      public TogglePanel() {
        JToggleButton tog = new JToggleButton("Toggle");
        ItemListener listener = new ItemListener() {
          public void itemStateChanged(ItemEvent e) {
            AbstractButton src = (AbstractButton) (e.getSource());
            System.out.println("Toggle: " + src.getText());
          }
        };
        tog.addItemListener(listener);
        add(tog);
    
        JCheckBox cbox = new JCheckBox("Checkbox");
        cbox.addItemListener(listener);
        add(cbox);
        ButtonGroup btngroup = new ButtonGroup();
        for (int i = 1; i <= 4; i++) {
          JRadioButton radio = new JRadioButton("Radio " + i);
          btngroup.add(radio);
          radio.addItemListener(listener);
          add(radio);
        }
      }
    }
    "When they give you ruled paper, write the other way..." J.R.J.

Similar Threads

  1. How to write a Document Listener in Java?
    By pushpendra in forum Software Development
    Replies: 8
    Last Post: 18-03-2010, 01:22 PM
  2. How to write a Key Listener in Java?
    By N I C K in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 01:52 AM
  3. How to write a Component Listener in Java?
    By hariharan_00 in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 06:28 AM
  4. How to write a Change Listener in Java?
    By Samarth in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 06:10 AM
  5. How to write a Caret Listener in Java?
    By Dilbert in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,243,450.16255 seconds with 17 queries