0

Let's say that I have 1 frame, 1 JDialog and 1 panel. The panel is on the frame. What I want to do is if a button is clicked i wanna switch the location of the panel to JDialog. I need two windows so i use Jdialog for that. Maybe there is a better way of creating that window rather then using JDialog.

Part of my code:

    public class Bestellterminal { 

    private static JPanel panel;

    public static void addComponentsToPane(final Container pane)  {}

    public static void addComponentsToPane1(final Container pane) {}

    public static void addComponentsToPane2(final Container pane) {

    final JPanel kpanel1 = new JPanel();

    kpanel1.setBounds(0 + insets.left, 0 + insets.top, size.width + 900, 
    size.height + 700);
    kpanel1.setVisible(true);


    final JDialog meinJDialog = new JDialog();

    meinJDialog.setTitle("Küchenterminal");
    meinJDialog.setSize(1200,900);
    meinJDialog.setVisible(true);
    meinJDialog.setLayout(null);
    meinJDialog.add(kpanel1);


     Classic.addActionListener(new ActionListener() {

     public void actionPerformed(ActionEvent e) {

             if (brclassic == 1)        {

                 if (kunde == 1) 
     {Bestellpanel.add(buttonx);buttonx.setVisible(true);brclassic++;
                 kpanel1.add(Bestellpanel);
     }
     }
     }


    private static void createAndShowGUI() 

    {

    JFrame frame = new JFrame("test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     

    addComponentsToPane(frame.getContentPane());
    addComponentsToPane1(frame.getContentPane());
    addComponentsToPane2(frame.getContentPane());

    Insets insets = frame.getInsets();
    frame.setSize(1200 + insets.left + insets.right,
                  900 + insets.top + insets.bottom);
    frame.setVisible(true);

     }

public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
    }    
    }
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Jennifer96
  • 55
  • 1
  • 8
  • to get the position of the panel: [enter link description here](http://stackoverflow.com/questions/12589824/get-location-of-a-swing-component) – kfc Apr 29 '17 at 18:32
  • So a component can only belong to a single parent, if you want to show the same panel on both windows simultaneously, you'll need to create a new instance of the panel so you can add it to the other window – MadProgrammer Apr 29 '17 at 22:15
  • Thx, good to know. But i dont need the panel simultaneously on both windows. The panel (Bestellpanel) is at the frame on default and i wanna transfer the panel (with a button click) with all the components into the panel (kpanel1) located at the JDialog (meinJDialog). – Jennifer96 Apr 29 '17 at 23:41

1 Answers1

0

I made a simple example using two JFrames, it could be done with any type of container/layout really.

package helloworld;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;

/**
 * Created on 4/30/17.
 */
public class SwapPanel {

    public static void main(String[] args){

        JPanel panel = new JPanel();
        panel.add(new JLabel("mover"));

        JFrame a = new JFrame("frame a");
        JButton aButton = new JButton("swap");
        JFrame b = new JFrame("frame b");
        JButton bButton = new JButton("swap");
        bButton.setEnabled(false);

        a.getContentPane().add(aButton, BorderLayout.SOUTH);
        a.getContentPane().add(panel, BorderLayout.CENTER);

        b.getContentPane().add(bButton, BorderLayout.SOUTH);

        aButton.addActionListener(evt->{
            if(aButton.isEnabled()){
                aButton.setEnabled(false);
                a.getContentPane().remove(panel);
                b.getContentPane().add(panel, BorderLayout.CENTER);
                bButton.setEnabled(true);
                a.pack();
                b.pack();
                a.repaint();
                b.repaint();
            }
        });

        bButton.addActionListener(evt->{
            if(bButton.isEnabled()){
                bButton.setEnabled(false);
                b.getContentPane().remove(panel);
                a.getContentPane().add(panel, BorderLayout.CENTER);
                aButton.setEnabled(true);
                a.pack();
                b.pack();
                a.repaint();
                b.repaint();
            }
        });


        a.pack();
        a.setVisible(true);
        b.pack();
        b.setVisible(true);

        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

The main thing that needs to be done, first remove the panel from one layout/container, then add the component panel to another container, and finally validate/repaint.

matt
  • 10,892
  • 3
  • 22
  • 34
  • Thank you very much, the next days i will try to implement the knowledge you gave me. It seems i cant get your code working with eclipse. I get the error at this line: (evt->{ if(aButton.isEnabled()){ Also i wanna ask you: if i remove the panel from one container and add it to another container. Do the components for example buttons that are in the panel also transfer to the new container? – Jennifer96 Apr 30 '17 at 19:55
  • Yes, that is why in my example I move `panel` which has a `JLabel` inside of it, you can make a more complete panel and do the same thing. The reason eclipse doesn't like the code I pasted might be because you have a lower language level set. If you are using java 8 then this code should compile. – matt May 01 '17 at 11:46
  • Oh ok i understand. Thank you <3 – Jennifer96 May 01 '17 at 18:16