2

I have a Swing-GUI (built with Netbeans GUI-Builder), there is an label which I move around using the arrow-keys. Pressing the space-key another class is called. How can I access the label from this class and e.g. get the position?

Thanks in advance for help! Example-Code of the second class:

    public class Xy extends Thread {
      private Window s; // this is the jFrame-form
      public Xy(Window s) {
        this.s = s;
        s.setBackground(Color.yellow); // works
}


public void run() {
          // here i want to use the position of a label which is located on the form s
}
3
  • 2
    Simply pass the component to the constructor of your other class. But beware that Swing components can't be used outside of the event dispatch thread. Google for "swing concurrency tutorial" Commented May 16, 2012 at 10:10
  • 1
    possible duplicate of Accessing Swing components of another class Commented May 16, 2012 at 10:30
  • @JB Nizet - Thanks, I was not aware that I directly could pass a component to another class! Commented May 16, 2012 at 20:04

2 Answers 2

2

The best option is not to expose the JLabel if you don't absolutely need to do it. If you want the other class to change the label's position, then give the class that holds the label a method that allows outside classes to do this. If other classes need to query the label's position, then give the containing class public getter methods so that outside code can obtain the position data (not the label itself). This way, if you later decide that you don't want to use a JLabel but rather would like to display a different component altogether, you won't break code in other classes.

Or better still, base your GUI on a MVC model, and then change the logical label's position in the model. The view can listen for changes in the model via the observer pattern and then the view itself can change the label's position.

They key to all of this is use loose coupling whenever and wherever possible.

Sign up to request clarification or add additional context in comments.

1 Comment

That absolutely makes sense to me! I will try to realize this. Thanks for your help.
1

There are two ways you can do this:

  1. Pass a reference to the JLabel to the class that needs it. For example, you could write a method that takes a JLabel as a parameter and then sets it to an instance variable.
  2. Make the JLabel a private instance variable in SomeClass and access it using a "getter" method (like getJLabel()).
  3. Make the JLabel a public instance variable in SomeClass (your JLabel/JFrame?) and access it using SomeClass.label.

The first would probably be considered better programming practice.

2 Comments

There are other better options.
Thanks NullPointer, I now solved it passing the label to the constructor to the other class like: code // in JFrame-Class Xy xy = new Xy(this, lblSomething); xy.start(); // in the other class public Xy(WindowClass s, Component c) { this.s = s; c.setLocation(30, 30);

Your Answer

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.