Question

probably it is a simple mistake but I cannot figure out what is wrong. I have a class which creates a frame(MainFrame) and uses method to change panels. I have another class which has the panel described in it. However, for some reason I can see only the frame without a panel. Could anyone help me in here? I am new in MigLayout, and would be really great if you could explain my mistake..

public class MainFrame extends JFrame
{
private JPanel panel;

//getting dimensions
public static  Dimension dim = Toolkit.getDefaultToolkit().getScreenSize() ;

public MainFrame()
{
    getContentPane().setLayout(new MigLayout());
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    this.setTitle("Title");
    this.setLocation((int)dim.getWidth()/3,(int)dim.getHeight()/4);
    this.setSize(500, 500);     

    setNewPanel(new MainWindowPanel());
    this.validate();
}

public final void setNewPanel(JPanel newPanel)
{
    //to change the panel, old one has to be deleted
    if (panel != null) remove(panel);

    getContentPane().setLayout(new MigLayout());
    add(newPanel);

    //pack();
    panel = newPanel;
    this.setVisible(true);  
}
}

My panel class

   public class MainWindowPanel  extends JPanel
   {
//Label
JLabel greeting = new JLabel("Welcome:");

//Buttons
JButton helpButton = new JButton("Help?");

public MainWindowPanel() 
{

    // the layout of the main screen
    JPanel p = new JPanel(new MigLayout("fill", "[center]"));

    p.setBackground(Color.lightGray);
    p.add(greeting,  "skip 1, gaptop 40, wrap");
    greeting.setFont(times20);

    p.add(helpButton, "bottom, span, tag help");

    }
}

Thank you!!

No correct solution

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