• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to update the panel with text?

 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am adding a Jlabel to a JPanel which is further added to content pane as shown below


this refers to JFrame object below:
main method
{
Container contentPane = this.getContentPane();
contentPane.setLayout(new FlowLayout());

JPanel currentPanel = new JPanel("Panel");

JLabel lbl = createLabel();
currentPanel.add(lbl);
currentPanel.setPreferredSize(new Dimension(335,100));

contentPane.add(currentPanel);

}

private JLabel createLabel()
{
JLabel jlb = new JLabel("str");
jlb.setFont(new Font("Courier New", Font.PLAIN, 12));
jlb.setForeground(Color.BLUE);
return jlb;
}


The frame appears but there is not text in it. Once I minimize the frame and then again reopen it, then only text is displayed in it. By text, I mean the JLabel component.

Could anyone advice where I am going wrong?

Thanks
 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use validate method

{yourComponent}.validate();
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your SSCCE that demonstrates the problem and don't forget to use the "Code" button so the code you post will remain formatted.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!