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 7 of 7

Thread: Swing Help

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Swing Help

    package gui;
     
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class javaFP extends JApplet implements ActionListener{
    	JButton b;
    	JButton b1;
    	JButton b2;
    	JTextField field;
    	public String text;
    	public void init(){
    		try{
    		SwingUtilities.invokeAndWait(new Runnable(){
    			public void run(){
    				swingGUI();
    			}
    		});
    	}
    		catch(Exception e){
    			System.out.println(e);
    		}
    	}
    	public void swingGUI(){
     
    		/*b= new JButton("Button 1"); 
    		b.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e) {
    						field.setText("Button 1");
    					}
    				}
    				);
    		b1= new JButton("Button 2"); 
    		b1.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e) {
    						field.setText("Button 2");
    					}
    				}
    				);*/
    		JButton b = new JButton(new AbstractAction ("Button 1") {
    			 public void actionPerformed( ActionEvent e ) {
    				 field.setText("Button 1");
    			 }
    		});
     
    		JButton b1 = new JButton(new AbstractAction ("Button 2") {
    			 public void actionPerformed( ActionEvent e ) {
    				 field.setText("Button 2");
    			 }
    		});
     
    		JButton b2 = new JButton(new AbstractAction ("Button 3") {
    			 public void actionPerformed( ActionEvent e ) {
    				 field.setText("Button 3");
    			 }
    		});
     
    		JTextField field = new JTextField(20);
    		setLayout(new FlowLayout());
    		setSize(300, 250);
    		setVisible(true);
    		add(b);
    		add(b1);
    		add(b2);
    		add(field);
    		field.setEditable(true);
    		b.addActionListener(this);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
     
    	}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
     
    	}
    }


    Using this code I keep getting an error. The way this is supposed to work is that when I click on any button, 1 2 or 3, it puts the name of the button into the TextField. When I click on a button all I get is this:

    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at gui.javaFP$2.actionPerformed(javaFP.java:46)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102 )
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 673)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)
    Last edited by ByteSized; June 20th, 2012 at 04:02 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Swing Help

    Put this line before your actionListeners are coded.

    JTextField field = new JTextField(20);

    Right now field is null when it's going into them.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing Help

    package gui;
     
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class javaFP extends JApplet implements ActionListener{
    	JButton b;
    	JButton b1;
    	JButton b2;
    	public String text;
    	public void init(){
    		try{
    		SwingUtilities.invokeAndWait(new Runnable(){
    			public void run(){
    				swingGUI();
    			}
    		});
    	}
    		catch(Exception e){
    			System.out.println(e);
    		}
    	}
    	public void swingGUI(){
    		JTextField field = new JTextField(20);
    		JButton b = new JButton(new AbstractAction ("Button 1") {
    			 public void actionPerformed( ActionEvent e ) {
    				 JTextField field = new JTextField(20);
    				 field.setText("Button 1");
    			 }
    		});
     
    		JButton b1 = new JButton(new AbstractAction ("Button 2") {
    			 public void actionPerformed( ActionEvent e ) {
    				 JTextField field = new JTextField(20);
    				 field.setText("Button 2");
    			 }
    		});
     
    		JButton b2 = new JButton(new AbstractAction ("Button 3") {
    			 public void actionPerformed( ActionEvent e ) {
    				 JTextField field = new JTextField(20);
    				 field.setText("Button 3");
    			 }
    		});
     
    		setLayout(new FlowLayout());
    		setSize(300, 250);
    		setVisible(true);
    		add(b);
    		add(b1);
    		add(b2);
    		add(field);
    		field.setEditable(true);
    		b.addActionListener(this);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
     
    	}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
     
    	}
    }

    Now when I click on my buttons nothing is added into the field.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Swing Help

    JTextField field = new JTextField(20);
    field.setText("Button 1");

    That is because you are referring to the field object you created that only has the scope of the ActionListener.

    Also, either make field either a class variable or final as it will say that field needs to be declared final or something like that if it's being declared inside that method.

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

    Default Re: Swing Help

    You have defined several variables with the same name local to methods that ALL shadow the variable that you want to assign the value to. Remove the definitions from all the methods and move the definition to the class level.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing Help

    Ah okay. Thanks.

  7. #7
    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: Swing Help

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/60845-settext-problems.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. SWING
    By aman_chauhan in forum Java Applets
    Replies: 1
    Last Post: June 9th, 2012, 06:40 PM
  2. SWING
    By aman_chauhan in forum Java Applets
    Replies: 1
    Last Post: June 8th, 2012, 08:34 AM
  3. Help with swing GUI
    By mwr76 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 5th, 2011, 03:54 AM
  4. Could need some help with Swing
    By Kerr in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2011, 10:11 AM
  5. Swing
    By waboke in forum AWT / Java Swing
    Replies: 2
    Last Post: November 3rd, 2010, 08:55 AM