12

I am new to Java Swing. I have some doubt regarding adiing components dynamically in Swing.

Basically I hav one Main JPanel consisting of two sub JPanel (leftpanel and rightpanel ) which alligned horizontally.In left JPanel I hav some JButtons, when I will click on JButton I nedd to show some JLabel, JTextArea etc in right JPanel. I tried a code but its not working .When I click on the button its going inside the event listener function but JLabel I am not able to view.

I am giving my code below. Pls look at this and correct me. thanks in advance

package my;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;

    /**
     *
     * @author root
     */

    public class myAplliwithPanel extends JFrame{

        JPanel rightPanel;

        public myAplliwithPanel() {
             initGui();
        }        

        public void initGui()
        {
           JPanel mainPanel=new JPanel();
           mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));

           JPanel leftPanel=new JPanel();
           leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));

            rightPanel=new JPanel();
           rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

           JButton dbBut=new JButton("DB");
           JButton appliBut=new JButton("Appli");
           appliBut.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    JLabel label=new JLabel("dsggs");
                   rightPanel.add(label);
                }
            });

           JButton backendBut=new JButton("Backend");

           leftPanel.add(dbBut);
           leftPanel.add(appliBut);
           leftPanel.add(backendBut);    

           mainPanel.add(leftPanel);
           mainPanel.add(rightPanel);

           add(mainPanel);

            setTitle("System Manger");
            setSize(400, 400);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);


        }

    public static void main(String args[]) {
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    myAplliwithPanel myObj = new myAplliwithPanel();
                    myObj.setVisible(true);
                }
            });
        }
    }
1
  • 1
    I edided your post please revert if isn't ... Commented Jun 29, 2011 at 6:26

3 Answers 3

23

You need to call revalidate after adding (or removing) components:

rightPanel.add(label);
rightPanel.revalidate();

should do the trick.

Sign up to request clarification or add additional context in comments.

1 Comment

I also tried invalidate, repaint, getParent().invalidate() / repaint() (and both together), but it just did not do the trick. My problem was I was removing all components from a JPanel and then re-adding them and they did appear once I minimized / resized the window just as I wanted them to. revalidate() on the Panel, that you are adding components to, did the trick for me, too. Thank you!
12

call

rightPanel.revalidate();
rightPanel.repaint();

after adding

Comments

4

just add this line after you add the label

rightPanel.updateUI();

when you add any component at runtime you need to update the ui using this method

4 Comments

updateUI is to do with the look and feel, not the layout.
In Swing, the terms layout, look and feel, and UI have very specific meanings. The user interface (not the Swing UI) is composed of all three. This question is about a change to the layout (specifically, adding a new component), but your answer refers to the look and feel, hence the downvote.
@Cameron sorry dear but m not satisfy with your comment as per the answer was right and saying about the UI in that control, layout are involves.
@CameronSkinner i agree with Pratik here, the answer that he provided was in accordance to the subject of the question. maybe it didn't help in this case but others may use this answer non the less. upvoted to balance the negative score.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.