JPanel didn't show rest of it's component

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);
    }
}

Answers


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


Need Your Help

Redirect urls without extension and slash at the end

.htaccess redirect

I would like to redirect all URL's without extension ( www.mydomain.com/test ) to my homepage EXCEPT the ones which ends with slash ( www.mydomain.com/test/ ).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.