1

I'm very much a rookie to java and I am trying to make a GUI client which once a button is pressed, then a new panel will be added to the frame. In this instance, when the a button called createMessage is pressed then the following panel will be added to the main frame.

    JPanel createMessagePanel = new JPanel();

    createMessagePanel.setLayout(new GridLayout(8, 0, 5, 10));

    messageText = new JTextArea(5, 20);

    sendTo = new JTextField("Recipient"); //Declared Global

    JLabel createMessagePrompt = new JLabel("CREATE MESSAGE");
    JLabel userPrompt = new JLabel("Send to:");
    JLabel messagePrompt = new JLabel("Message:");

    sendButton = new JButton("Send!");
    createAMessageButton.addActionListener(this);

    createMessagePanel.add(createMessagePrompt);
    createMessagePanel.add(userPrompt);
    createMessagePanel.add(sendTo);
    createMessagePanel.add(messagePrompt);
    createMessagePanel.add(new JScrollPane(messageText));
    createMessagePanel.add(sendButton);

    add(createMessagePanel, BorderLayout.CENTER);

This sadly doesn't work, I've been told that this may be down to that the frame needs to know about the components added to the frame at runtime. How would I be able to achieve this effect of upon a button pressed then different panels which contains other components would be displayed in the position BorderLayout.CENTER.

Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jono_2007
  • 1,016
  • 3
  • 12
  • 23
  • 1
    If you are really a rookie in Java, I really don't think that making GUI is the best thing to start with. – talnicolas Feb 29 '12 at 20:05
  • 1
    Try `revalidate()` and `repaint()` your panel. –  Feb 29 '12 at 20:08
  • 1
    Put a `CardLayout` in the `CENTER`, add the new component to the center panel, then show that card. For better help sooner, post an [SSCCE](http://sscce.org/). Agree with talnicolas re. 'in too deep'. – Andrew Thompson Feb 29 '12 at 20:12

0 Answers0