user3227258
user3227258

Reputation: 37

How to add JLabel at run time in Swing?

I am making am window application on Java. I want to add label name at run time in my Swing application. How can I do this using Java Swing?

public class Component1 extends JPanel {

   Component1() {
      JLabel label = new JLabel("dd");
      label.setBounds(370, 340, 150, 20);
     // label.setText("labeVVl");
      add(label);
}

 public static void main(String[] args)
 {
    // create frame
    JFrame frame = new JFrame();
    final int FRAME_WIDTH = 800;
    final int FRAME_HEIGHT = 600;
    // set frame attributes
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setTitle("My Frame");
    frame.setVisible(true);
    Component1 Com = new Component1();
    Component add = frame.add(Com);
}
}

Upvotes: 1

Views: 2198

Answers (1)

mKorbel
mKorbel

Reputation: 109813

  1. this code should be works by add revalidate() and repaint() as notifiers for LayoutManager

  2. don't to use NullLayout, use default FlowLayout implemented for JPanel in API do that the same way

  3. see Initial Thread

  4. for example

Upvotes: 5

Related Questions