Skip to content
Advertisement

How to use getContentPane in independent class?

I want to set background color by key pressed event. This is my code (using independent class)

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class KeyCharEx extends JFrame {
    JLabel la = new JLabel("r=red y=yellow b=blue");
    Container c = getContentPane();
    public KeyCharEx() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c.setLayout(new FlowLayout());
        c.add(la);
        c.addKeyListener(new MyKeyListener());
        setSize(250, 150);
        setVisible(true);
        
        c.setFocusable(true);
        c.requestFocus();

    }
    
    public static void main(String[] args) {
        new KeyCharEx();
    }
}
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class MyKeyListener extends KeyAdapter {
    
    public void keyPressed(KeyEvent e) {
        switch(e.getKeyChar()) {
            case 'r':
                KeyCharEx.getContentPane().setBackground(Color.RED);
                break;
            case 'y':
                KeyCharEx.getContentPane().setBackground(Color.YELLOW);
                break;
            case 'b':
                KeyCharEx.getContentPane().setBackground(Color.BLUE);
                break;
            case 'q':
                System.exit(0);
        }
    }

}

But I can’t use getContentPane with an error “Cannot make a static reference to the non-static method getContentPane() from the type JFrame” How can I solve this problem?

Advertisement

Answer

Your KeyCharEx.getContentPane() is trying to call a static method in the KeyCharEx class that does not exist. Well, it exists, but it’s non-static, therefore only accessible when used on an instance of the class. In this case, it might be best to pass the pane as an argument:

// For your KeyCharEx
public class KeyCharEx extends JFrame {
    JLabel la = new JLabel("r=red y=yellow b=blue");
    Container c = getContentPane();
    public KeyCharEx() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c.setLayout(new FlowLayout());
        c.add(la);
        c.addKeyListener(new MyKeyListener(c));
        setSize(250, 150);
        setVisible(true);
        
        c.setFocusable(true);
        c.requestFocus();

    }
    
    public static void main(String[] args) {
        new KeyCharEx();
    }
}

// For your MyKeyListener
public class MyKeyListener extends KeyAdapter {
    Container pane;
    public MyKeyListener(Container pane) {
        this.pane = pane;
    }
    
    public void keyPressed(KeyEvent e) {
        switch(e.getKeyChar()) {
            case 'r':
                pane.setBackground(Color.RED);
                break;
            case 'y':
                pane.setBackground(Color.YELLOW);
                break;
            case 'b':
                pane.setBackground(Color.BLUE);
                break;
            case 'q':
                System.exit(0);
        }
    }

}

If getContentPane() is a public method, you could also pass your KeyCharEx over in a similar way and use keyCharEx.getContentPane() instead. You’d be calling the method on your KeyCharEx object, not the class itself.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement