Question

I have frame with a button and a JPanel as I named panel, I want after I clicked the button add an inner panel to my panel. But this but there is a problem with this! because after adding second panel it didn't add any other panel.

Code

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import javax.swing.JTextField;


public class DrawImages extends JFrame{
    
    int i;
    public DrawImages() {
        JButton btnNewButton = new JButton("New button");
        i = 0;
        getContentPane().add(btnNewButton, BorderLayout.SOUTH);
        setMinimumSize(new Dimension(1000,150));
        final JPanel panel = new JPanel();
        panel.setSize(995, 145);
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);
        

        final JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBounds(0, 0, 46, 14);
        panel.add(lblNewLabel);
        
        btnNewButton.addActionListener(new ActionListener() 
            public void actionPerformed(ActionEvent paramActionEvent) {
                JPanel panel_1 = new JPanel();
                //getContentPane().add(panel_1, BorderLayout.NORTH);
                panel_1.setLayout(null);
                JLabel imagelable = new JLabel(new ImageIcon("c:\\good.jpg"));
                imagelable.setBounds(70, 5, 105, 65);
                panel_1.add(imagelable);
                
                JLabel lblNewLabel_4 = new JLabel("Up Label");
                lblNewLabel_4.setBounds(5, 5, 65, 35);
                panel_1.add(lblNewLabel_4);
                
                JLabel lblNewLabel_2 = new JLabel("Down Label");
                lblNewLabel_2.setBounds(5, 25, 65, 65);
                panel_1.add(lblNewLabel_2);
                lblNewLabel.setText(""+i);
                panel_1.setBounds(5+170*i, 5, 170+170*i, 70);
                panel.add(panel_1);
                i++;
            }
        });
        panel.setMinimumSize(new Dimension(995,150));
    }

    public static void main(String[]args)
    {
        DrawImages drawImages = new DrawImages();
        drawImages.setVisible(true);
    }
}
Was it helpful?

Solution 3

Thanks for your answers but my problem is I added third arguman in every circle:

panel_1.setBounds(5+170*i, 5, 170+170*i, 70);

so my panel get bigger and bigger (event biger than my monitor) so the correct is:

panel_1.setBounds(5+170*i, 5, 170, 70);

OTHER TIPS

The problem is with the statement:

panel_1.setLayout(null);

panel_1 doesn't have any preferred size so will not appear (or will appear as a tiny dot).

Swing was designed to use layout managers. You could use GridLayout in this particular case.

Read: Doing Without a Layout Manager

I agree with Reimeus. Just to test your code, I used

panel_1.setLayout(new FlowLayout());

And I could see the panels being added without calling repaint() on the parent panel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top