Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding Component Dynamically In Swings

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.95k
    Comment on it

    Hi All,

    If you are adding JButton ,JLabel or JComboBox etc dynamically or adding them during runtime For example

    public class Main {
    
      public JPanel buttonPanel ;
    
    
      public static void main(String[] args) {
        JFrame frame = new JFrame();
    
        int gridSize = 3; // try 4 or 5, etc. buttons are always 50x50
    
        JPanel panel = new JPanel(new GridLayout(gridSize, gridSize));
        panel.setPreferredSize(new Dimension(500, 500));
    
    
       buttonPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
       c.anchor = GridBagConstraints.CENTER;
    
        JButton button = new JButton();
        button.setPreferredSize(new Dimension(50, 50));
        buttonPanel.add(button, c);
       button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
    
          JPanel buttonPanel = new JPanel(new GridBagLayout());
           GridBagConstraints c = new GridBagConstraints();
          c.anchor = GridBagConstraints.CENTER;
    
           JButton dynamic = new JButton();
           dynamic.setPreferredSize(new Dimension(50, 50));
           buttonPanel.add(button, c);
        }
       });
        panel.add(buttonPanel);
    
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
      }
    }
    

    In Above example, when you clicked button then you added one more button to screen but when you run this example you will see nothing happened after clicking on a button. Whenever you add any component to screen then you have to revalidate and repaint that component.

    After calling revalidate and repaint function, the code will look like :-

    public class Main {
    
          public JPanel buttonPanel ;
    
    
          public static void main(String[] args) {
            JFrame frame = new JFrame();
    
            int gridSize = 3; // try 4 or 5, etc. buttons are always 50x50
    
            JPanel panel = new JPanel(new GridLayout(gridSize, gridSize));
            panel.setPreferredSize(new Dimension(500, 500));
    
    
           buttonPanel = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
           c.anchor = GridBagConstraints.CENTER;
    
            JButton button = new JButton();
            button.setPreferredSize(new Dimension(50, 50));
            buttonPanel.add(button, c);
           button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
    
              JPanel buttonPanel = new JPanel(new GridBagLayout());
               GridBagConstraints c = new GridBagConstraints();
              c.anchor = GridBagConstraints.CENTER;
    
               JButton dynamic = new JButton();
               dynamic.setPreferredSize(new Dimension(50, 50));
               buttonPanel.add(button, c);
    
    //Just call this two function to repaint buttonPanel
               buttonPanel.revalidate();
               buttonPanel.repaint();
            }
           });
            panel.add(buttonPanel);
    
            frame.setContentPane(panel);
            frame.pack();
            frame.setVisible(true);
          }
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: