Is actionPerformed a static method ?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Is actionPerformed a static method ?

  1. #1
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    35

    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

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    This has nothing to do with actionPerformed. You are doing trying to access firstPanel and secondPanel as if they were static.

    This will fix your problem:
    Code:
    public void actionPerformed(ActionEvent ex) {
        combo c = new combo();
        int sum = c.firstPanel.getNumber() + c.secondPanel.getNumber(); 
    }
    Although I don't recommend making firstPanel and secondPanel public.

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Going back to your question about ActionPerformed - [even though it looks like destin has gotten to the meat of your immediate issue] ...

    In Java, a static method is one defined by the "class definition" - the compiler uses that class definition and places one copy of the method in the "static" portion of the memory footprint of the application. No other copy of the method is put into memory - even if you instantiate the class. When a call is made to this method, it refers back to this single statement of code which is in the "static" or "unchanging" part of the memory footprint. That is why a static method is called by referring to the ClassName, not to a particular instance or object of the class.

    This set-up would be fast but inflexible if ActionPerformed() was a static method. Any call to the method would be "universal" and deal only with the code available to the class outside of any method - rather than making use of the environment (methods, fields, etc.) encapsulated in a particular instance of the class. You don't want to have your application set up this way.

  4. #4
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    35
    Thanks destin,
    Your code works perfectly and has helped me a lot. The difference is that you created a new instance of the combo class and then referenced the panels from that instance whereas in my code firstPanel and secondPanel were static even though I wrongly thought I had made them two seperate instances of the PanelWithCombo class.

    Hi nspils,
    Thank you as well. It is important for me that I grasp the meaning of 'static', for me it is subtle. Your definition adds a lot to my understanding. You said "a static method is called by refering to the class name" and this is where I was going wrong.

    Obiviously there is a lot here for me to think about but thanks to you guys I can begin to reasoon this one out.

Similar Threads

  1. Trouble buffering transparent gif
    By axlan in forum Java
    Replies: 2
    Last Post: 06-22-2005, 09:21 AM
  2. Replies: 3
    Last Post: 02-27-2005, 11:43 AM
  3. Replies: 0
    Last Post: 05-27-2003, 12:12 PM
  4. Invoking a static method with Reflection
    By Staffan in forum .NET
    Replies: 2
    Last Post: 05-14-2003, 09:34 AM
  5. static method question
    By Sean Woods in forum .NET
    Replies: 2
    Last Post: 02-20-2003, 06: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center