I have a JPanel and JButton on the JFrame.
on runtime add JLabel to JPanel When click JButton.
I use of the following code:
panel.setLayout(null);
jLabel _lbl=new jLabel();
_lbl.setText("Label");
panel.add(_lbl);
panel.validate();
but no display any JLabel in JPanel.
I see you create a
JLabelcalled_lbl:but you never add it to your panel. Instead you add a new
JLabelwith no text to your panel:This would ofcourse construct an empty label which wont be visible.
Also try calling
revalidate()andrepaint()on yourJPanelinstance after adding theJLabellike so:With this you may also need to call
pack()on your frames instance so as to resize theJFrameto fit the new components.Also please never use a
null/Absolutelayout this is very bad practice (unless doing animation) and may prove to be problematic and very hard to use.Rather use a
LayoutManager:or if you only have a single component on the
JPanelsimply calladd(label);as it will stretch to theJPanelsize.UPDATE:
Here is a small sample. Simply adds
JLabels to theJPaneleach timeJButtonis pressed: