I get a Nullpointerexception
at addPropertyChangeListener
when I am using Nimbus Look and Feel. When I use the Linux Standard LAF (Metal) everything works just fine.
Here is a small Testproject which simulates the problem! I have a Class which extends JPanel and provides to Content to a Frame. The Finsih button on the frame will only be enabled when some conditions are met (in this case the button2 is pressed).
This is the Mainclass:
public class Main extends JFrame {
/**
*
*/
private static final long serialVersionUID = -3120562776241721109L;
private JPanel contentPane;
private JButton button;
private PropertyChangeListener changeListener;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
System.out.println(UIManager.getLookAndFeel());
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
button = new JButton("BUTTON");
button.setEnabled(false);
changeListener = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equalsIgnoreCase("state")) {
button.setEnabled((boolean) evt.getNewValue());
}
}
};
contentPane.add(button, BorderLayout.SOUTH);
Panel panel = new Panel();
contentPane.add(panel, BorderLayout.CENTER);
panel.addPropertyChangeListener(changeListener);
}
Here is the Panel Class:
public class Panel extends JPanel {
/**
*
*/
private static final long serialVersionUID = -5036784576513445229L;
private PropertyChangeSupport changes;
private boolean state;
/**
* Create the panel.
*/
public Panel() {
this.changes = new PropertyChangeSupport(this);
JButton button = new JButton("BUTTON2");
add(button);
state = false;
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
state = !state;
changes.firePropertyChange("state", null, state);
}
});
}
@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
changes.addPropertyChangeListener(listener);
}
}
Like is said before, it works without any problem with Metal LAF