1

For some reason, when I try to call a non-static method from another class in an action, I get an error saying I can't call a non-static method in a static method. However, I never defined the action or its parents as static. Why do I get this error?

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 public class KeyListenerFrame extends JFrame implements KeyListener {
 GridLayout gridLayout1 = new GridLayout();
 JPanel listenerPan = new JPanel();
 public KeyListenerFrame() {
 try {
 jbInit();
 listenerPan.addKeyListener(this);
 addWindowListener(new WindowAdapter() {
 public void windowClosing(WindowEvent e) {
 System.exit(0);
 }
 }); 
 listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0,true), "showKey");
 listenerPan.getActionMap().put("showKey", showKey);
 listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "showKey");
}
catch(Exception e) {
 e.printStackTrace();
 }
 }
 public static void main(String[] args) {
 KeyListenerFrame klf = new KeyListenerFrame();
 klf.setBounds(10,10,500,500);
 klf.setVisible(true);
 klf.focusPanel();
 }
 public void focusPanel() {
 listenerPan.requestFocus();
 }
 private void jbInit() throws Exception {
 this.getContentPane().add(listenerPan, null);
 ColorPanel panel = new ColorPanel(Color.lightGray);
 this.getContentPane().add(panel);
 }

this is the part that gives me trouble

 Action showKey = new AbstractAction() {
 public void actionPerformed(ActionEvent e) {
 ColorPanel.moveLeft();
 }
 };
 public void keyTyped(KeyEvent e) {
 System.out.println(e.toString());
 } 
 public void keyPressed(KeyEvent e) {
 System.out.println(e.toString());
 }
 public void keyReleased(KeyEvent e) {
 System.out.println(e.toString());
 }
 }

ColorPanel.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
import java.awt.Container.*;
import javax.swing.JComponent.*;
public class ColorPanel extends JPanel{
 public Circle circle;
 private javax.swing.Timer timer;
 public ColorPanel(Color backColor){
 int width = 500;
 int height = 500;
 setBackground(backColor);
 setPreferredSize(new Dimension(width, height));
 circle = new Circle(width / 2, height / 2, 50, Color.red);
 circle.setVelocity(5);
 addMouseListener(new MoveListener());
 }
 public void paintComponent(Graphics g){
 super.paintComponent(g);
 circle.fill(g);
 }
 public void moveLeft(){
 circle.move();
 repaint();
 }
 /* public class MoveListener extends MouseAdapter{
 public void mousePressed(MouseEvent e){
 circle.move();
 repaint();
 }
 }
 private class MoveListener implements ActionListener{
 public void actionPerformed(ActionEvent e){
 circle.move();
 //circle.turn(1);
 repaint();
 }
 } */
}

The error generated from the ColorPanel.moveLeft(); is: KeyListenerFrame.java:51: non-static method moveLeft() cannot be referenced from a static context ColorPanel.moveLeft();

BalusC
1.1m377 gold badges3.7k silver badges3.6k bronze badges
asked May 4, 2011 at 17:59
2
  • could you post the error Commented May 4, 2011 at 18:02
  • and also what ColorPanel looks like? Commented May 4, 2011 at 18:04

3 Answers 3

8

What exactly are you trying to do here:

 ColorPanel.moveLeft();

ColorPanel is a class, when it should be an instance.

What you may be intending to do is (assuming showKey is defined within ColorPanel):

 ColorPanel.this.moveLeft();

Edit To Add the following:

private void jbInit() throws Exception {
 this.getContentPane().add(listenerPan, null);
 ColorPanel panel = new ColorPanel(Color.lightGray);
 this.getContentPane().add(panel);
}

ColorPanel is created in here, but a reference isn't kept.

If you move ColorPanel panel outside of jbInit, and then initialize it as

panel = new ColorPanel(Color.lightGray);

you'll have your reference. Then instead of ColorPanel.moveLeft() call panel.moveLeft()

answered May 4, 2011 at 18:02
Sign up to request clarification or add additional context in comments.

4 Comments

Inside of the ColorPanel class there's a method moveLeft that moves a dot left on the screen. Should I be calling it differently?
Yeah ... ColorPanel.this gets you the enclosing instance. You could also do just moveLeft() since it'll go up to the next enclosing instance.
When I try that I get... KeyListenerFrame.java:44: not an enclosing class: ColorPanel ColorPanel.this.moveLeft(); showKey is not defined within ColorPanel.
That init code won't work inside showKey (<identifier> expected), nor inside actionPerformed (cannot find symbol).
0

The problem is that you are trying to access it as you would if it were a static method. You need to create an instance of the class, then through the instance you call moveLeft().

Also create a global reference to your ColorPanel Object. This would be the instance of the ColorPanal class you call moveLeft() on.

answered May 4, 2011 at 19:08

Comments

0

I get an error saying I can't call a non-static method in a static method

No you don't. There is no such message and no such error. You get an error saying 'non-static method method() cannot be referenced from a static context.'

answered May 5, 2011 at 0:17

2 Comments

I'm sorry, I wrote it without looking at the actual error. Any idea how to remedy this?
@Bro mcBro: you can edit your post. If you're referring to remedying the error, see John's answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.