Quillion
Quillion

Reputation: 6476

Add JPanel to multiple JFrames

I have a JPanel that contains bunch of context.

I would like to be able to add the said JPanel to two JFrames. Any tips?

This is example of what I would like

public class Test
{
    public static void main(String [] args)
    {
        JPanel panel = new JPanel();
        panel.add(new JLabel("Hello world"));
        panel.validate();

        JFrame frame1, frame2;
        frame1 = new JFrame("One");
        frame2 = new JFrame("Two");
        frame1.add(panel);
        frame2.add(panel);
        frame1.validate();
        frame2.validate();
        frame1.setVisible(true);
        frame2.setVisible(true);
        frame1.pack();
        frame2.pack();
    }
}

I want both JFrames to show hello world, rather than one showing it and the other being empty. And if data in JPanel updates I want both JFrames to show it.

Thanks to anyone for their help, this has been bugging me for a while.

Upvotes: 5

Views: 490

Answers (3)

arynaq
arynaq

Reputation: 6870

Sounds like you want two views of the same thing, since it doesn't work with one JPanels on two frames, have you considered creating two panels each with a reference to this thing you want a view of in each panel thereby circumventing this problem?

The rough idea is sketched below. Essentially you don't need two seperate JPanels to reflect eachother, you need two JPanels that reflect something else.

class Data<T> {
.....stuff
  void update();
  T getdata();
}

class DataView<T> extends JPanel {
  private Data<T> data;
  ...stuff
  public DataView(T data) {
     this.data = data;
  }
  ...stuff
  protected void paintComponent(Graphics g) {
     drawData();
     ...stuff
  }
}

Then

public static void main(String[] args ) {
  Data<String> data = new Data();
  DataView<String> dataViewOne = new DataView(data);
  DataView<String> dataViewTwo = new DataView(data);
  ..add to two seperate frames, pack set visible etc..
}

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347184

All Swing components can only have one parent, this means that if you add a component to another container, it will be removed from the first container automatically...

Try something like...

    JPanel panel = new JPanel();
    panel.add(new JLabel("Hello world"));

    JFrame frame1, frame2;
    frame1 = new JFrame("One");
    frame1.add(panel);
    frame1.pack();
    frame1.setVisible(true);

    JPanel panel2 = new JPanel();
    panel2.add(new JLabel("Hello world"));
    frame2 = new JFrame("Two");
    frame2.add(panel2);
    frame2.pack();
    frame2.setVisible(true);

Instead

Upvotes: 1

dcsohl
dcsohl

Reputation: 7396

I don't think you can. The code in java.awt.Container contains this tidbit when you add a Component to it. Here, read JFrame for the Container and your JPanel for comp:

        /* Reparent the component and tidy up the tree's state. */
        if (comp.parent != null) {
            comp.parent.remove(comp);
            if (index > component.size()) {
                throw new IllegalArgumentException("illegal component position");
            }
        }

(that's in java.awt.Container.addImpl(Component, Object, int))

So right there, when you add it to a different Container, it removes it from the old Container. I suppose you could rewrite this base-level AWT code, but I wouldn't recommend it.

Upvotes: 6

Related Questions