0
//GUI.java
public class GUI extends JFrame implements ActionListener {

    private static final long serialVersionUID = 870343916997182570L;
    private JPanel btmPanel;

    public GUI(String arg0) throws HeadlessException {
        super(arg0);
        createGUI();
    }

    private void createGUI() {
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        //ResultPanel rslt = new ResultPanel();
        //this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);

        btmPanel = new JPanel();
        btmPanel.setBackground(Color.LIGHT_GRAY);
        btmPanel.setLayout(new FlowLayout());

        JButton blueSearch = new JButton("Search");
        blueSearch.setBackground(Color.WHITE);
        blueSearch.addActionListener(this);
        btmPanel.add(blueSearch);

        JButton blackChart = new JButton("Chart");
        blackChart.setBackground(Color.WHITE);
        blackChart.addActionListener(this);
        btmPanel.add(blackChart);

        this.getContentPane().add(btmPanel, BorderLayout.SOUTH);    
    }   

    @Override
    public void actionPerformed(ActionEvent e) {
        String buttonString = e.getActionCommand();

        if (buttonString.equals("Search")) {
            ResultPanel rslt = new ResultPanel();
            this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);
        }
    }

}

//ResultPanel.java
public class ResultPanel extends JPanel implements ActionListener {

    private static final long serialVersionUID = -7851502165390304971L;
    private JPanel textPanel;
    private JTextArea textDisplay;

    public ResultPanel() {
        textPanel = new JPanel(); 
        textDisplay = new JTextArea("Text Area:");
    }

    public JPanel createPanel() {

        textDisplay.setEditable(true);
        textPanel.setBackground(Color.LIGHT_GRAY);
        textPanel.setLayout(new BorderLayout());
        textPanel.add(textDisplay,BorderLayout.CENTER);

        return textPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {


    }

}

I have two buttons on the main frame, and I hope to change the panel when I press the button.

The question is that the code in "actionPerformed" doesn't work,

but it works well if I put them in the creatGUI()....(see the marked section).

Is that anything wrong ?

user1371541
  • 43
  • 1
  • 8
  • Have you checked that the actionPerformed method is being called at all? Printing something to the console from it is the easiest – dann.dev May 20 '12 at 07:10
  • 1
    Use a `CardLayout` as shown [here](http://stackoverflow.com/questions/5665156/calling-awt-frame-methods-from-subclass/5786005#5786005) (and a `ButtonGroup` with `JRadioButton` instances). – Andrew Thompson May 20 '12 at 08:10

4 Answers4

2

After adding components on the run, to your JFrame you need to call revalidate() and repaint() for changing to be visible.

Though as you said, if you are adding them inside the createGUI(), that way it is visible, since at that time it's a static addition to your Swing Application, you added that first and then set it to visible.

Though your code has a bit of loopholes, the best I can tell you real quick, is you extending JPanel for ResultPanel, though you never used ResultPanel, so I modified your code to take ResultPanel into perspective. Here try this modified code from your example :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddComponentExample
{
    private JFrame frame;
    private JPanel btmPanel;
    private ResultPanel resultPanel;

    public AddComponentExample()
    {
        resultPanel = new ResultPanel();
    }

    private void display()
    {
        frame = new JFrame("Adding Component Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btmPanel = new JPanel();
        btmPanel.setBackground(Color.LIGHT_GRAY);
        btmPanel.setLayout(new FlowLayout());

        JButton blueSearch = new JButton("Search");
        blueSearch.setBackground(Color.WHITE);
        blueSearch.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (!resultPanel.isShowing())
                {
                    resultPanel = resultPanel.createPanel();
                    frame.getContentPane().add(resultPanel, BorderLayout.CENTER);
                    frame.revalidate();  // For Java 7 and above.
                    // frame.getContentPane().revalidate(); // For Java 1.6 or below.
                    frame.repaint(); // required sometimes
                }
                else
                    System.out.println("Panel is already Visible");
            }
        });
        btmPanel.add(blueSearch);

        JButton blackChart = new JButton("Chart");
        blackChart.setBackground(Color.WHITE);
        //blackChart.addActionListener(this);
        btmPanel.add(blackChart);

        frame.getContentPane().add(btmPanel, BorderLayout.PAGE_END);
        frame.setSize(500, 500);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new AddComponentExample().display();
            }
        });
    }
}

class ResultPanel extends JPanel implements ActionListener {

    private static final long serialVersionUID = -7851502165390304971L;
    private JPanel textPanel;
    private JTextArea textDisplay;

    public ResultPanel() {
        textPanel = new JPanel(); 
        textDisplay = new JTextArea("Text Area:");
    }

    public ResultPanel createPanel() {

        textDisplay.setEditable(true);
        textPanel.setBackground(Color.LIGHT_GRAY);
        textPanel.setLayout(new BorderLayout());
        textPanel.add(textDisplay,BorderLayout.CENTER);
        add(textPanel);
        return this;
    }

    @Override
    public void actionPerformed(ActionEvent e) {


    }

}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
1

Just call pack(); after you add the panel. This will get the JFrame to show the update.

    if (buttonString.equals("Search")) {
        ResultPanel rslt = new ResultPanel();
        this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);
        pack();
    }
n00begon
  • 3,503
  • 3
  • 29
  • 42
0

Use anonymous class of ActionListener

0
Use one line of code: this.revalidate();
This will validate and repaint the frame so that it can show the JPanel.

if (buttonString.equals("Search")) {
  ResultPanel rslt = new ResultPanel();
  this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);
  this.revalidate();
}
bhavna garg
  • 270
  • 2
  • 19