Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 228
Quillion Avatar asked Nov 26 '13 19:11

Quillion


People also ask

Can you have multiple Jpanels?

We can add most of the components like buttons, text fields, labels, tables, lists, trees, etc. to a JPanel. We can also add multiple sub-panels to the main panel using the add() method of Container class.

How do I create multiple Jframes?

java write the actual logic code, in this class write the code which creates JFrame, add JButton for open New Frame. In this class write the code which creates JFrame, add JLabel into the New Frame. Simillarly, you can create multiple frames and navigate from one frame to another.


Video Answer


3 Answers

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.

like image 147
dcsohl Avatar answered Sep 27 '22 00:09

dcsohl


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

like image 34
MadProgrammer Avatar answered Sep 26 '22 00:09

MadProgrammer


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..
}
like image 25
arynaq Avatar answered Sep 26 '22 00:09

arynaq