Results 1 to 3 of 3
- 04-28-2011, 07:01 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
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+"); } } }; }
- 04-28-2011, 07:04 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
public MyFrame() {
......
JPanel buttonPanel = new JPanel();
...
-->
change it to:
buttonPanel = new JPanel();
(without the JPanel!!!)
- 04-28-2011, 07:08 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
ArrayList returns null
By aborgeld in forum New To JavaReplies: 6Last Post: 04-01-2011, 01:32 PM -
XML and SAX - class returns 'null'
By Laffel in forum Advanced JavaReplies: 2Last Post: 03-07-2011, 09:14 PM -
Splashscreen returns null
By Charlie161 in forum AWT / SwingReplies: 2Last Post: 03-04-2011, 01:25 PM -
String passed as argument to a constructor returns null value
By eLancaster in forum New To JavaReplies: 1Last Post: 02-07-2011, 10:44 AM -
getImplementationVersion() returns null
By newbiejava in forum New To JavaReplies: 22Last Post: 09-12-2010, 09:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks