user2709253
user2709253

Reputation: 3

adding a jpanel into a jpanel in java

I have a Jpanel named mainPanel and another class named MyPanel that extends JPanel. I want to add MyPanel into mainPanel. I did this code and getting no result .

This is my Main class that holds mainPanel:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class Main extends JFrame {

    private JPanel contentPane;
    private final JPanel mainPanel = new JPanel();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Main() {
        inItGUI();
    }
    private void inItGUI() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 662, 417);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        mainPanel.setBounds(25, 11, 611, 341);

        contentPane.add(mainPanel);
        mainPanel.setLayout(null);

        mainPanel.add(new MyPanel());// **Here i added MyPanel**
        mainPanel.revalidate();
        repaint();
    }

}

and this is my MyPanel class that has beeb added to mainPanel earlier:

import java.awt.Component;

import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class MyPanel extends JPanel {

    private final JButton btnNewButton = new JButton("New button");
    private final JButton btnNewButton_1 = new JButton("New button");
    public MyPanel() {

        inItGUI();
    }
    private void inItGUI() {
        setLayout(null);
        btnNewButton.setBounds(60, 49, 106, 43);

        add(btnNewButton);
        btnNewButton_1.setBounds(189, 49, 89, 43);

        add(btnNewButton_1);
    }
}

Upvotes: 0

Views: 84

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Welcome to one of the wonderful pitfalls of null layout managers

If you remove the layout manager, you remove any chance of your components from being automatically size and positioned.

Basically what's happening is that MyPanel is being added to mainPanel and position 0x0 with a size of 0x0, so nothing is begin displayed.

The real solution is use an appropriate layout manager

Don't fool your self. Managing the position and size of components, especially the relationship between the components, in the field of modern computer systems is a complex one. You have to be able to take into account how each component is rendered based on the font, DPI, orientation, etc... of the current system, then take into account how that might effect other components that share the same container...You also have to monitor for changes in the hierarchy of components that you are attached to...

This is the job that layout managers provide...Swing is designed around the use of layout managers and you should have a VERY good reason to ignore them...IMHO

Upvotes: 4

Related Questions