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);
                }
            });
        }
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
nantitv
  • 131
  • 1
  • 1
  • 3

3 Answers3

23

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

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

should do the trick.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
  • 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! – Igor Jun 19 '17 at 16:46
12

call

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

after adding

StanislavL
  • 56,971
  • 9
  • 68
  • 98
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

Pratik
  • 30,639
  • 18
  • 84
  • 159
  • `updateUI` is to do with the look and feel, not the layout. – Cameron Skinner Jun 29 '11 at 06:42
  • 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 Skinner Jun 29 '11 at 19:43
  • @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. – Pratik Jun 30 '11 at 05:01
  • @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. – BigFatBaby Mar 13 '12 at 15:24