Ok, using Eclipse IDE and getting tripped up on the static/non static issue. I think I understand it, but not completely, here is the code.
First Part, mostly created with the swing UI builder. edited out comments/imports
public class Screen {
private JFrame frame;
public JLabel lblNewLabel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Screen window = new Screen();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Screen() {
initialize();
}
void initialize() {
XListener listenItem = new XListener("Woot"); // creates listen object
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(193, 154, 56, 16);
frame.getContentPane().add(lblNewLabel);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(163, 73, 97, 25);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(listenItem); // attaches it to listen object
}
void changeLabel(String setString) {
lblNewLabel.setText(setString);
}
}
Second part is the listener class
// creates class for listen item
public class XListener implements ActionListener {
String foo;
XListener(String setString) {
foo = setString;
}
public void actionPerformed(ActionEvent btnNewButton) {
**Screen.changeLabel(foo);**
}
}
It complains that cannot make a static reference to the non-static method changeLabel(String) from the type Screen. Yet, if I use window, in place of Screen, it cannot find the object. This has me extremely confused. The way I understand the code, is the main method creates a Screen object, named window, which when initialized also creates a XListener object. Why does it consider the actionPerformed method to be static? I know there is something fundamental I am missing, and I have been following the java 'trails' and just don't get it.