1

I have some code:

button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFrame mainFrame = new JFrame();
            JPanel windowPanel = new JPanel(new FlowLayout());
//          windowPanel.setPreferredSize(new Dimension(200,200));
            windowPanel.add(colorChooser);
            windowPanel.add(button);
            windowPanel.setVisible(true);
            mainFrame.add(windowPanel);
        }
    });

and the problem is how to display new FlowLayout (in a new window) after clicking a button ?

NewUser
  • 3,729
  • 10
  • 57
  • 79
Tomasz Waszczyk
  • 2,680
  • 5
  • 35
  • 73

2 Answers2

2

Start by swapping the windowPanel.setVisible(true); and mainFrame.add(windowPanel);

mainFrame.add(windowPanel);
windowPanel.setVisible(true);

Adding mainFrame.pack() before the setVisible call won't hurt either.

You may wish to take a look at The Use of Multiple JFrames: Good or Bad Practice? before commiting yourself to a particulr design though.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0
        public void actionPerformed(ActionEvent e) {
            frame2 = new JFrame("Meine Frame");
            frame2.setSize(500,400);
            frame2.setLocationRelativeTo(null);
            JPanel windowPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            windowPanel.add(okButton);

            okButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    color = colorChooser.getColor();
                    System.out.println("The selected color was:" + color);
                    panel.setBackground(color);
                    frame2.dispose();

                }
            });{

            };
            windowPanel.add(colorChooser);

            windowPanel.setVisible(true);
            frame2.add(windowPanel);
            frame2.setVisible(true);

        }

in this way I solved my problem.

Tomasz Waszczyk
  • 2,680
  • 5
  • 35
  • 73