0

I have JLabels in multiple JFrames, and all of them serve the purpose of being a status indicator. In the class Controller, I have a method that changes the JLabel in the given JFrame. My question is that how do I reference the JFrame as a variable?

import javax.swing.*;

public class Controller {
    public static class Status {
        public static void vPrint(JFrame frame, String text) {
            JLabel label = frame.getConsoleLabel();
            label.setText(text)
        }
    }
}

So when I call it in the JFrame: Controller.Status.vPrint(this, "Set the JLabel Text.");

All of the JFrames have the method getConsoleLabel()

The problem is that when I call it, it says: cannot find symbol: method and I think the cause is that the variable frame isn't referenced.

Any solutions?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Brandon Nguyen
  • 345
  • 2
  • 3
  • 15
  • *"I have JLabels in multiple JFrames.."* 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 25 '13 at 21:13

2 Answers2

3

It seems like you are extending the JFrame to some Class but trying to invoke the getConsoleLabel() on the super class JFrame which doesn't have such method. So yes, you can't invoke getConsoleLabel on instance of JFrame no matter to which type the reference of this instance is referring to. Rather declare your own frame MyFrame extends JFrame which has implementation of getConsoleLabel() change the signature of vPrint with parameter type MyFrame and pass the required instance of type MyFrame to vPrint.

And DON'T USE MULTIPLE JFrame as Andrew has suggested. Follow the linked answer by him.

Sage
  • 15,290
  • 3
  • 33
  • 38
0

Well you specialized JFramess have getConsoleLabel() method.

You have two solutions:

  1. Create an interface with getConsoleLabel() and make all of your JFrames implement that interface. And change the signature of vPrint to accept an instance of your interface instead of JFrame.
  2. Create an class such as LabledFrame extends JFrame which has getConsoleLabel() and extend all of your JFrames from that, and change the vPrint to accept an instance of LabledFrame instead of JFrame.
Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69