Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java swing dynamically adding components [duplicate]

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);
                }
            });
        }
    }
like image 889
nantitv Avatar asked Jun 29 '11 06:06

nantitv


3 Answers

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

like image 106
Pratik Avatar answered Nov 06 '22 05:11

Pratik


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

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

should do the trick.

like image 22
Cameron Skinner Avatar answered Nov 06 '22 05:11

Cameron Skinner


call

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

after adding

like image 11
StanislavL Avatar answered Nov 06 '22 04:11

StanislavL