Results 1 to 5 of 5
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default simple GUI problem

    It displays the GUI with the button saying "Click me!." I just want the text to change when the user clicks the button.

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Testing implements ActionListener{
    	JButton button;
    	
    	public static void main(String args[]) {
    		
    		Testing gui = new Testing();
    		gui.go();		
    	}
    	
    	public void go() {
    		JFrame frame = new JFrame();
    		JButton button = new JButton("Click me!");
    		button.addActionListener(this);
    		
    		frame.getContentPane().add(button);	
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);			
    		frame.setSize(300, 300);		
    		frame.setVisible(true);		
    		
    	}
    	
    	public void actionPerformed(ActionEvent event) {
    		button.setText("I've been clicked!");
    		
    	}
    	
    	
    	
    }
    When I click the button, this pops up --

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Testing.actionPerformed(Testing.java:26)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.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.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'm pretty confused. I don't see why it wouldn't be working. Any help would be appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You are declaring "button" twice, once in the class where it is never initialized, never displayed, and once in the go method where it is initialized and displayed. the two button objects are completely unrelated. The actionPerformed method tries to manipulate the non-constructed button.

    solution: declare the object only once, in the class.

    So change this line:
    Java Code:
    JButton button = new JButton("Click me!"); // button object declared and constructed
    to this:
    Java Code:
    button = new JButton("Click me!");// button object just constructed

  3. #3
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    Oh man sorry about that, I must've overlooked that :\.

    I have another question now though. I tried to add to this a little bit. I wanted to add a rectangle to the pane along with the button. I'm trying to use the setSize(int width, int height) method for the button to make it smaller so that you can see both. However, the button still takes up the whole GUI and the rectangle is practically not there.

    My two classes are as follows --

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Testing implements ActionListener{
    	DrawPanel board = new DrawPanel();
    	JButton button;
    	public static void main(String args[]) {
    		Testing gui = new Testing();
    		gui.go();		
    	}
    	
    	public void go() {
    		JFrame frame = new JFrame();
    		button = new JButton("Click Me!");
    		button.addActionListener(this);
    		button.setSize(25, 25);
    		frame.getContentPane().add(board);
    		frame.getContentPane().add(button);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);			
    		frame.setSize(300, 300);		
    		frame.setVisible(true);		
    		
    	}
    	
    	public void actionPerformed(ActionEvent event) {
    		button.setText("I've been clicked!.");
    	}
    	
    	
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class DrawPanel extends JPanel{
    	
    	public void paintComponent(Graphics g) {
    		
    		Graphics2D g2d = (Graphics2D) g; 
    		int green = (int)(Math.random() * 256);
    		int blue = (int) (Math.random() * 256);
    		int red = (int) (Math.random() * 256);
    		Color startColor = new Color(red, green, blue);
    		green = (int)(Math.random() * 256);
    		blue = (int) (Math.random() * 256);
    		red = (int) (Math.random() * 256);
    		Color endColor = new Color(red, green, blue);
    		GradientPaint gradient = new GradientPaint(75, 75, startColor, 150, 150, endColor);
    		g2d.setPaint(gradient);
    		g.fillRect(70, 70, 100, 100);
    		
    	}
    
    }
    Maybe the setSize method doesn't what I'm thinking it does? Or did I make a coding error somewhere?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    um,... and you're welcome.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Recommendations
    1) Avoid using setSize, and instead prefer using setPreferredSize
    2) read up on the layout managers at the Sun layout manager tutorial. It will tell you that a JFrame (actually its contentPane) uses BorderLayout as its default layout manager and why this is causing the effect that you are seeing.

Similar Threads

  1. Simple square root problem!
    By nortski in forum New To Java
    Replies: 7
    Last Post: 04-01-2009, 02:11 PM
  2. Simple IO problem
    By aamp in forum New To Java
    Replies: 2
    Last Post: 12-01-2008, 02:27 PM
  3. Simple JFrame Problem
    By DC1 in forum New To Java
    Replies: 4
    Last Post: 06-06-2008, 07:09 AM
  4. My Simple Refresh Problem
    By pmcastillo in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 07:59 AM
  5. problem with a simple java code
    By boy22 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •