0

I already know that a JComponent cannot be added to two different containers, but I need some way to mimic this. I have a JLabel which has an overridden draw method, and I want this label to appear in two JFrames at once. Since I cannot add it to both, how can I make it so when one JLabel gets re-drawn and looks different, the other JFrame mirrors it?

  • You would generally mirror a JComponent by writing some code. Have you done this? Maybe you could post it so people could see what's going wrong and help you fix it – Tibrogargan Sep 26 '16 at 00:19
  • `I have a JLabel which has an overridden draw method,` - I have no ideas what that means since there is no "draw()" method in a JLabel. Maybe you should be doing custom painting on a BufferedImage. Then you can create an ImageIcon using the BufferedImage. An Icon can be shared by multiple labels. – camickr Sep 26 '16 at 01:32
  • An alternative approach using `RepaintManager` is cited [here](http://stackoverflow.com/a/6333584/230513). – trashgod Sep 26 '16 at 02:12

2 Answers2

2

If this were my problem, I'd create a class for creating that JComponent (myself I prefer to use JPanel), and I'd use an M-V-C or "Model-View-Controller" type of program structure for this program. The model would hold the state of the logic of the program, including the logical representation of the graphical objects shown, while the view would be the the class that extended the JPanel, and which would display, in whatever fashion needed, the data held in the model. I'd then create two of these creatures and have them both share the same model -- this is key. Then when changes occur to one, it is seen in both.

If you need further details and a better explanation with code, please consider creating and posting a valid a minimal example program.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

You should use a model-view pattern:

https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel

Your model would be a class with a single string. Your view, i.e. the JLabel needs to be notified when ever the model changes. You can do that by using the observer pattern:

https://en.wikipedia.org/wiki/Observer_pattern

This means, your model maintains a list of observers and whenever you update the model all observers get notified. For example:

class Model {
    public interface Observer {
        void onTextChanged();
    }
    private String text;
    final private List<Observer> observerList = new ArrayList<>();

    public void addObserver(Observer observer) {
        observerList.add(observer);
    }

    public void setText(String text) {
        this.text = text;
        for (Observer observer : observerList)
            observer.onTextChanged();
    }

    public String getText() {
        return text;
    }
}

In this way you can display your model in as many views/JLabels as you want. For example:

final JLabel label = new JLabel();
model.addObserver(new Model.Observer() {
       public void onTextChanged() {
            label.setText(model.getText());
       }
   });
final JLabel label2 = new JLabel();
model.addObserver(new Model.Observer() {
       public void onTextChanged() {
            label2.setText(model.getText());
       }
   });
Clemens
  • 86
  • 8