|
-
Is actionPerformed a static method ?
Hi, I'm new to Java and still trying to get a handle on all this object oriented stuff but I would appreciate it if one of you guys could explain this;
Suppose I create a class which is a panel with a combobox on it. I create two instances of this class and add them to the content pane. I also add a panel with a JButton on it to the content pane.
I want to select a number from each combobox so that when I press the JButton the actionPerformed method adds the two numbers I selected.
My problem is the actionPerformed method seems to be a static method (is this true ?) as a result JBuilder keeps telling me "non-static variable cannot be referenced from a static context". Does this mean I can never use clicking a button to perform a job that involves manipulating instance variables belonging to an object, surely this can't be true.
Here is my code...
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class combo extends JFrame
{
public PanelWithCombo firstPanel = new PanelWithCombo();
public PanelWithCombo secondPanel = new PanelWithCombo();
private PanelWithButton buttonPanel = new PanelWithButton();
private JPanel MainPanel = new JPanel();
public static void main(String[] args)
{
new combo();
}
combo()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,200);
this.setLocationRelativeTo(null);
this.getContentPane().add(MainPanel);
MainPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10,30));
MainPanel.add(firstPanel);
MainPanel.add(secondPanel);
MainPanel.add(buttonPanel);
this.setVisible(true);
}
}
class PanelWithCombo extends JPanel implements ItemListener
{
public int number;
private String[] contents = {"1","2","3"};
private JComboBox myCombo = new JComboBox(contents);
public PanelWithCombo()
{
myCombo.addItemListener(this);
this.add(myCombo);
}
public void itemStateChanged(ItemEvent e)
{
number = Integer.parseInt((String) myCombo.getSelectedItem());
}
public int getNumber()
{
return number;
}
}
class PanelWithButton extends JPanel implements ActionListener
{
private JButton btnGenerate = new JButton("Generate");
PanelWithButton()
{
this.add(btnGenerate);
btnGenerate.addActionListener(this);
}
public void actionPerformed(ActionEvent ex)
{
int sum = combo.firstPanel.getNumber() + combo.secondPanel.getNumber();
}
}
As I say I am new to object oriented programming and its quite possible that i have completely gone off on a tangent with this one but if anyone has any thoughts or suggestions I would really appreciate it.
Thanks
Similar Threads
-
Replies: 2
Last Post: 06-22-2005, 08:21 AM
-
Replies: 3
Last Post: 02-27-2005, 10:43 AM
-
By twahl in forum ASP.NET
Replies: 0
Last Post: 05-27-2003, 11:12 AM
-
Replies: 2
Last Post: 05-14-2003, 08:34 AM
-
By Sean Woods in forum .NET
Replies: 2
Last Post: 02-20-2003, 05:23 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks