user3153014
user3153014

Reputation: 318

Create JPanel with component in it

I am trying to create JPanel having some JLabels in it, to which I want to pass value by constructor of JPanel. As below.

class MyPanel extends JPanel
 {

 private JLabel lbl;

     public MyPanel(String val)
{
   super();
   lbl=new JLabel(val);
   this.add(lbl);
}
 } 

And while using this Panel MyPanel mp=new MyPanel("value");

But I am not able to see the Label inside the Panel.

What may be the problem? Is there any other way to do this?

Upvotes: 1

Views: 99

Answers (4)

Paul Samsotha
Paul Samsotha

Reputation: 208964

It's possible, you are trying to add multiple components to your frame without specifying BorderLayout poistions. The JFrame has a default BorderLayout. It not specified, all component will be added to the BorderLayout.CENTER position. Each position can only hold one component. So only the last component you add will be visible.

  • You could either set another Layout Manager to the frame,

  • or add the components with specified positions like

    frame.add(new MyPanel(), BorderLayout.CENTER);
    frame add(anotherPanel, BorderLayout.SOUTH);
    
  • Also make sure to call .pack() on the frame, so all preferred sizes are respected. Avoid using .setSize() if that's what you're doing.


EDIT

Works for me. Maybe you forgot to pass a String to the constructor. Run this test

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test3 {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MyPanel("Hello, World!"));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    static class MyPanel extends JPanel {

        private JLabel lbl;

        public MyPanel(String val) {
            super();
            lbl = new JLabel(val);
            this.add(lbl);
        }
    }
}

enter image description here

Upvotes: 2

user3197401
user3197401

Reputation: 3

For JPanel the default is BorderLayout, So there is no need to set layout. You shold add the label to content pane.. Try This

{

JLabel lb1 = new JLabel(val);
getContentPane().add(val);

}

Upvotes: 0

Klausos Klausos
Klausos Klausos

Reputation: 16050

Probably you need to repaint() or validate() your JPanel after adding new JLabel in it dynamically.

lbl=new JLabel(val);
this.add(lbl);
this.validate();

or

lbl=new JLabel(val);
this.add(lbl);
this.repaint();

Also use setSize(getPreferedSize()) and setLocation():

lbl.setSize(lbl.getPreferredSize());
lbl.setLocation(50, 20);

Finally you should use proper layout i.e. BorderLayout or GridLayout, etc.

Upvotes: 1

StanislavL
StanislavL

Reputation: 57381

You should define a LayoutManager for the MyPanel

e.g.

setLayout(new BorderLayout());
this.add(lbl, BorderLayout.CENTER);

Upvotes: 1

Related Questions