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: Problem with ItemListener.... help pleas

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Problem with ItemListener.... help pleas

    My problem is with ItemListener I'm not sure why it is not working. Well I'm trying to split the ItemListener
    into two class.

    in the main class I have this

    final JCheckBox whole_C_CheckBox = new JCheckBox("Whole");
    whole_C_CheckBox.addItemListener(new CheckBox(whole_C_QTextField, whole_C_WTextField, whole_C_PTextField));
    whole_C_CheckBox.setVerticalAlignment(SwingConstants.BOTTOM);
    whole_C_CheckBox.setHorizontalAlignment(SwingConstants.LEFT);
    whole_C_CheckBox.setBounds(12, 66, 113, 25);
    chikenPanel.add(whole_C_CheckBox);

    In the other class I have

     
    public class CheckBox implements ItemListener{
     
     
     
    	private JTextField q;
    	private JTextField p;
    	private JTextField w;
    	private JCheckBox whole_C_CheckBox;
     
    	public CheckBox(JTextField whole_C_QTextField, JTextField whole_C_WTextField, JTextField whole_C_PTextField) {
     
    		this.q = whole_C_QTextField;
    		this.w = whole_C_WTextField;
    		this.p = whole_C_PTextField;
     
     
    	}
    	@Override
    	public void itemStateChanged(ItemEvent e) {
     
    		if(whole_C_CheckBox.isSelected()){
     
    			q.setText("");
    			w.setText("");
    			p.setText("");
     
    		}
    	       else{
     
    			q.setText("0");
    			w.setText("0.00");
    			p.setText("0.00");
    		}	
     
     
    }}

    will when I try to run the program its working.However, when I click on the Check Box for (whole_C_CheckBox ) it give an error I don't know what is wrong with my code.

    Note: in the main class all these whole_C_QTextField, whole_C_WTextField, whole_C_PTextField has
    been set in default to (0, 0.00, 0.00), so I'm trying when I click on Check Box for (whole_C_CheckBox )
    it will set them to nothing, and if I unchecked them again they will return to their default (0, 0.00, 0.00).


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with ItemListener.... help pleas

    it give an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem with ItemListener.... help pleas

    What error does it give you? Can you post an SSCCE that demonstrates the problem?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with ItemListener.... help pleas

    Quote Originally Posted by Dragon3002 View Post
    public class CheckBox implements ItemListener{
     
    	private JCheckBox whole_C_CheckBox;
     
    	public CheckBox(JTextField whole_C_QTextField, JTextField whole_C_WTextField, JTextField whole_C_PTextField) {
     
    		this.q = whole_C_QTextField;
    		this.w = whole_C_WTextField;
    		this.p = whole_C_PTextField;
     
     
    	}
    }
    One thing is sure: the whole_C_CheckBox instance variable is not initialized.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with ItemListener.... help pleas

    This is the error I get:

     
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at test1.CheckBox.itemStateChanged(CheckBox.java:31)
    	at javax.swing.AbstractButton.fireItemStateChanged(Unknown Source)
    	at javax.swing.AbstractButton$Handler.itemStateChanged(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireItemStateChanged(Unknown Source)
    	at javax.swing.JToggleButton$ToggleButtonModel.setSelected(Unknown Source)
    	at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

    I looked at line 31, but I can not find what is wrong.

    Note: this is the line 31: if(whole_C_CheckBox.isSelected()){...etc

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with ItemListener.... help pleas

    Quote Originally Posted by Dragon3002 View Post
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at test1.CheckBox.itemStateChanged(CheckBox.java:31)

    I looked at line 31, but I can not find what is wrong.

    Note: this is the line 31: if(whole_C_CheckBox.isSelected()){...etc
    Quote Originally Posted by andbin View Post
    One thing is sure: the whole_C_CheckBox instance variable is not initialized.
    Excuse me, but isn't this clear? You have a whole_C_CheckBox that you use in itemStateChanged but that you don't inizialize.

    You have (at least) 2 options:
    1) Pass the JCheckBox reference to the CheckBox constructor and store it in whole_C_CheckBox instance variable.
    2) Use the JCheckBox reference from the ItemEvent object (since the event comes from JCheckBox the e.getSource() clearly gives that JCheckBox!)
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Replies: 8
    Last Post: February 2nd, 2014, 09:09 AM
  2. Pleas help! array index out of bouderies
    By ProgrammerNoobE in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 29th, 2013, 04:30 AM
  3. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  4. Getting a TextField to activate ItemListener.
    By javapenguin in forum AWT / Java Swing
    Replies: 5
    Last Post: July 20th, 2010, 11:07 PM
  5. Item Listener problem with destroying Midlet
    By hanizainol in forum Java Applets
    Replies: 0
    Last Post: July 22nd, 2008, 08:59 PM

Tags for this Thread