0
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       //removing existing panels


        Panel6.setLayout(null);
        JLabel label1=new JLabel("This is panel 4");
        label1.setBackground(Color.WHITE);
        jPanel6.add(label1);
        label1.setText("this is panel 4");
        System.out.println("here i am");
        jPanel6.revalidate();


        jPanel6.repaint();

    } 

panel6 is already been created in design but at run time button is not being added in the panel dynamically

c0der
  • 18,467
  • 6
  • 33
  • 65
  • I think we need to see more of your code to determine what's wrong. For example, I'm not sure if/how `Panel6` and `jPanel6` relate. – Stefan Oct 25 '17 at 03:25
  • You mean `jLabel` is not added? – Thum Choon Tat Oct 25 '17 at 03:33
  • 2
    `Panel6.setLayout(null)` is your key problem, nothing is sizing/positioning the label, and since you’ve done away with the layout manager, revalidate will do nothing. Short answer, don’t use null layouts – MadProgrammer Oct 25 '17 at 03:37
  • https://stackoverflow.com/q/5750068/5855946 you can see this also for add component dynamically – Ganesh Patel Oct 25 '17 at 12:11

2 Answers2

0

Adding components after setting jPanel6.setLayout(null) places them one on top of the other. (I assume JPanel6 is a typo).
When jPanel6 is created assign a layout manager to it. For example:
jPanel6.setLayout(new FlowLayout());

And change jButton1ActionPerformed :

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        JLabel label1=new JLabel("This is panel 4");
        label1.setBackground(Color.WHITE);
        jPanel6.add(label1);
        revalidate(); //remove setSize to parent, if any
   }

For more information about layout managers see A Visual Guide to Layout Managers.

c0der
  • 18,467
  • 6
  • 33
  • 65
  • You still need the revalidate(). This is what invokes the layout manager, otherwise the label will still have a size of (0, 0) and there will be nothing to paint. – camickr Oct 25 '17 at 04:38
-3

You have to manually define the width and height of your label if you're adding jLabel into panel6 with absolute layout.

label1.setWidth(100);
label1.setHeight(30);
Thum Choon Tat
  • 3,084
  • 1
  • 22
  • 24
  • 3
    Or just make use of appropriate layout managers and solve all the other issues that null layouts present – MadProgrammer Oct 25 '17 at 03:39
  • @MadProgrammer some people like perfect precision. Null layout works for some. – Jacob B. Oct 25 '17 at 03:55
  • 2
    Some people have to much time to waste on reinventing the wheel. Pixel perfect layouts are an illusion – MadProgrammer Oct 25 '17 at 03:58
  • 2
    (1-) don't use a null layout. There is no reason to do so. @JacobB. `some people like perfect precision.` - so then use a layout manager. It will calculate the perfect precision every time without guess work. There is absolutely no reason to use a null layout in a question like this. – camickr Oct 25 '17 at 04:23
  • Well yeah in a question like this, I'm just saying sometimes people like to use null. Don't hate – Jacob B. Oct 25 '17 at 04:24
  • 2
    @JacobB., only because they don't know any better. – camickr Oct 25 '17 at 04:25
  • Well sure, but everyone had to start somewhere – Jacob B. Oct 25 '17 at 04:26
  • 2
    So we provide answers to provide good coding practices so you don't learn bad habbits. – camickr Oct 25 '17 at 04:27
  • 1
    Here's a good example of what happens when using `null-layout`: https://stackoverflow.com/questions/42520492/jtable-not-showing-up-on-jframe-java/42521097#42521097 @MadProgrammer might have nightmares after seeing it – Frakcool Oct 25 '17 at 05:41