Results 1 to 3 of 3
  1. #1
    CAD
    CAD is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default JPanel returns null outside of constructor

    Not really understanding why...
    I get a Nullpointerexception when trying to change the panel color.

    Idea is to change the background color of the panel when the user clicks.


    Problems lies in the MousListener at menuPanel.setBackground.

    Hope someone can tell me how to fix it, thx :)

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class MyFrame extends JFrame implements ActionListener {
    	private JLabel lab1;
    	private JButton b1, b2;
    	private JPanel buttonPanel;
    
    	public MyFrame() {
    		setLayout(new BorderLayout());
    		this.setName("Test");
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new FlowLayout());
    		add((buttonPanel), BorderLayout.SOUTH);
    		buttonPanel.setBackground(Color.RED);
    		// buttonPanel.setBackground(Color.LIGHT_GRAY);
    		// buttonPanel.setBackground(Color.ORANGE);
    		// buttonPanel.setBackground(Color.CYAN);
    
    		lab1 = new JLabel("Startup");
    		add((lab1),BorderLayout.CENTER);
    	
    		lab1.setFont(new Font("Serif", Font.BOLD, 20));
    		// lab1.setForeground(Color.BLUE);
    		lab1.setBackground(Color.WHITE);
    		lab1.setOpaque(true);
    
    		b1 = new JButton("One");
    		buttonPanel.add(b1);
    		b1.addActionListener(al);
    		b1.addMouseListener(ml);
    		b2 = new JButton("Two");
    		buttonPanel.add(b2);
    		buttonPanel.addMouseListener(ml);
    		b2.addActionListener(al);
    		b2.addMouseListener(ml);
    		buttonPanel.addMouseListener(ml);
    		setSize(200, 200);
    		buttonPanel.setVisible(true);
    		this.setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    	}
    
    	ActionListener al = new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			JButton knop = (JButton) (e.getSource());
    			if (knop == b1) {
    				lab1.setText("ONE");
    				//System.out.println(buttonPanel);
    			} else if (knop == b2) {
    				lab1.setText("TWO");
    			}
    		}
    	};
    
    	MouseListener ml = new MouseAdapter() {
    		public void mouseReleased(MouseEvent e) {
    
    			int aantal = e.getClickCount();
    			if (aantal == 1) {
    
    				System.out.println("1");
    				System.out.println(buttonPanel);
    				[B]buttonPanel.setBackground(Color.LIGHT_GRAY);[/B]
    
    			} else if (aantal == 2) {
    				System.out.println("2");
    			} else if (aantal >= 3) {
    				System.out.println("3+");
    			}
    		}
    	};
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    public MyFrame() {
    ......
    JPanel buttonPanel = new JPanel();
    ...
    -->
    change it to:

    buttonPanel = new JPanel();

    (without the JPanel!!!)

  3. #3
    CAD
    CAD is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    public MyFrame() {
    ......
    JPanel buttonPanel = new JPanel();
    ...
    -->
    change it to:

    buttonPanel = new JPanel();

    (without the JPanel!!!)
    Damn bro that was quick, and it works :)

    thanks for the fast response! :)

Similar Threads

  1. ArrayList returns null
    By aborgeld in forum New To Java
    Replies: 6
    Last Post: 04-01-2011, 02:32 PM
  2. XML and SAX - class returns 'null'
    By Laffel in forum Advanced Java
    Replies: 2
    Last Post: 03-07-2011, 10:14 PM
  3. Splashscreen returns null
    By Charlie161 in forum AWT / Swing
    Replies: 2
    Last Post: 03-04-2011, 02:25 PM
  4. Replies: 1
    Last Post: 02-07-2011, 11:44 AM
  5. getImplementationVersion() returns null
    By newbiejava in forum New To Java
    Replies: 22
    Last Post: 09-12-2010, 10:31 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
  •