0

I am a total noob to jpanel and I learned everything from the web so I don't know what I am doing wrong. I have this simple function for a Jpanel setup:

private static void GUI(String TIME, int Action){
    if (Action == 1){
        JFrame EnterMessage = new JFrame("Tester");
        EnterMessage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        EnterMessage.setSize(190, 80);
        EnterMessage.setVisible(true);

        JPanel Panel = new JPanel();
        Panel.setBackground(Color.WHITE);

        JLabel TextLabel = new JLabel(TIME);

        Panel.add(TextLabel);

        EnterMessage.add(Panel);
    }else {
        Panel.removeAll(); //Comment 1
        }
    }
}

Comment 1: after removing all stuff I still need to refresh but I didn't got to that yet, first I need to fix errors (Otherwise you would be scrolling way to long.)

Basicly when I hover over the 'removeAll();' in eclipse It tels me this: Cannot make a static reference to the non-static method removeAll() from the type Container

What does that mean? And how to fix it?

I did search google and stack for this one but I could not find anything that looked like my code so I could not use that

Full code:

package Main;

import java.awt.Color;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Panel;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import junit.framework.Test;

public class MainClass {
static String TIME = functions.gettime();
private static boolean wPressed = false;
public static boolean isWPressed() {
    synchronized (MainClass.class) {
        return wPressed;
    }
}
public static void main(String[] args) {
    GUI(TIME,1);
    System.out.printf("before key \n");
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new     KeyEventDispatcher() {

    @Override
    public boolean dispatchKeyEvent(KeyEvent ke) {
        synchronized (Test.class) {
            switch (ke.getID()) {
            case KeyEvent.KEY_PRESSED:
                if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
                    System.out.printf("Enter Key Detected");
                    TIME = functions.gettime();
                    GUI(TIME,2);
                    break;
                }
                break;
            }

        }
        return false;
    }
});

  JFrame test = new JFrame();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(false);
}


private static void GUI(String TIME, int Action){
if (Action == 1){
    JFrame EnterMessage = new JFrame("Tester");
    EnterMessage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    EnterMessage.setSize(190, 80);
    EnterMessage.setVisible(true);

    JPanel Panel = new JPanel();
    Panel.setBackground(Color.WHITE);

    JLabel TextLabel = new JLabel(TIME);

    Panel.add(TextLabel);

    EnterMessage.add(Panel);
}else {
    Panel.removeAll();
    }
}
}
BRHSM
  • 854
  • 3
  • 13
  • 48
  • I checked that question but I didn't understand the answers – BRHSM Feb 01 '15 at 15:00
  • Then tell us what you don't understand. This is a key concept that you really need to nail before trying to do more complex coding such as GUI coding. – Hovercraft Full Of Eels Feb 01 '15 at 15:01
  • I did read the full page and i just don't get it to work. sorry if I am doing stuff wrong here i'm new – BRHSM Feb 01 '15 at 15:02
  • Declare your `Panel` variable (which should be called `panel` -- please learn Java naming conventions, variables begin with a lower case letter) **above** the if block. Where it's currently declared -- inside of the if block, it is not in scope, it is not seen, within your else block. The key concept to learn here is variable scope -- variables are **only** visible within the scope with which they are declared. – Hovercraft Full Of Eels Feb 01 '15 at 15:03
  • that did the job, put it as an answer so I can give you points for the answer – BRHSM Feb 01 '15 at 15:05
  • I can't since the question is closed. You might as well delete it since this has been asked many times on this site. – Hovercraft Full Of Eels Feb 01 '15 at 15:05
  • By the way, the compiler thought that you were trying to call a method on `java.awt.Panel` since your Panel variable was not visible within the else block's scope. That's why you saw the error message that was shown. – Hovercraft Full Of Eels Feb 01 '15 at 15:07
  • Okey so that had nothing to do with the initializing of the panel – BRHSM Feb 01 '15 at 15:11
  • It had all to do with variable scope. The error just happened to exist within your panel initialization code section. – Hovercraft Full Of Eels Feb 01 '15 at 15:12
  • Okey so it would not be a duplicate because it was a diferent problem – BRHSM Feb 01 '15 at 15:16
  • Correct, it is not a duplicate of the link @JBNizet used. Rather it's a duplicate of a thousand other similar questions on Java variable scope. Still no reason to open this question. – Hovercraft Full Of Eels Feb 01 '15 at 15:17
  • It's a duplicate because if you read the error message and understand it, you understand that you're trying to call the instance method removeAll() on the class Panel. Which then should makes you think: "why does the compiler think that?". And then you'd realize that 1. you're importing the class Panel which shouldn't be imported, since you don't intend to use it, 2. you're using a variable out of its scope, 3. you're not naming variables correctly, which allows confusing them with classes. – JB Nizet Feb 01 '15 at 15:21

0 Answers0