java - Adding JPanel from one class to a JPanel in a different class -


i have frame jframe i'm adding gui jpanel consists of multiple different jpanels , 1 of in different class.

the problem i'm unable add other classes jpanel gui jpanel no errors nothing displayed

purchasestock.java

    guic.gridx=0;     guic.gridy=0;     gui.add(stockdropdown.getp(), guic);      guic.gridx=1;     guic.gridy=0;     gui.add(quantityp, guic);      guic.insets = new insets(30,0,0,0);     guic.gridwidth=2;     guic.gridx=0;     guic.gridy=1;     gui.add(checkout, guic);      frame.add(gui);     frame.pack(); //size components      frame.setresizable(false);     frame.setvisible(true); 

stackdropdown.java

public class stockdropdown {      private static jpanel ui = new jpanel(new borderlayout());     private jbutton bt = new jbutton("tsfdsfds");     private jpanel top = new jpanel();     private jpanel middle = new jpanel();       public stockdropdown(){        ui.add("north", top);        top.add(bt);     }     public static jpanel getp(){         return ui;     }  } 

update:

purchasestock.java

public class purchasestock extends jframe implements actionlistener {     jframe frame = new jframe();      jpanel gui = new jpanel(new gridbaglayout());     gridbagconstraints guic = new gridbagconstraints();      public purchasestock(){         frame.settitle("update stock");         frame.setdefaultcloseoperation(jframe.dispose_on_close);           guic.gridx=0;         guic.gridy=0;         gui.add(new stockdropdown(), guic);           frame.add(gui);         frame.pack(); //size components          frame.setresizable(false);         frame.setvisible(true);    }   } 

stackdropdown.java

package stock;  import java.awt.borderlayout; import javax.swing.jbutton; import javax.swing.jpanel;  public class stockdropdown extends jpanel{      private jpanel ui = new jpanel(new borderlayout());     private jbutton bt = new jbutton("tsfdsfds");     private jpanel top = new jpanel();     private jpanel middle = new jpanel();       public stockdropdown(){         ui.add("north", top);        top.add(bt);      }   } 

update 2:

stockdropdown.java

package stock;  import java.awt.borderlayout; import java.awt.dimension; import java.awt.image; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jlabel; import javax.swing.jpanel;  public class stockdropdown extends jpanel{     private jcombobox itemsdropdown = new jcombobox();      private jpanel top = new jpanel();     private jpanel middle = new jpanel();     jbutton t = new jbutton("tt");      public stockdropdown(){         add("north", top);        add("center", middle);        top.add(itemsdropdown);         for(string key : stockdata.getstock().keyset())  {          itemsdropdown.additem(stockdata.getname(key));        }        middle.add(t);      }       private imageicon setimagesize(string path) {         imageicon image1 = new imageicon(path);         image image2 = image1.getimage().getscaledinstance(100,100,0);         return new imageicon(image2);     };  } 

you other jpanel you've added nothing since components added in constructor , stockdropdown constructor never called. i'd rid of static fields , methods unless plan create true valid static factory method, 1 calls appropriate constructor.

e.g.,

import javax.swing.*;  public class stockdropdown extends jpanel {     private jbutton bt = new jbutton("tsfdsfds");     private jpanel top = new jpanel();     private jpanel middle = new jpanel();      // make private if using factory method     private stockdropdown(){        add("north", top);        top.add(bt);     }      // public static factory method     public static stockdropdown getinstance() {         stockdropdown ui = new stockdropdown();         return ui;     } } 

note if stockdropdown extends jpanel, rid of ui variable, because it, you're adding components container not being displayed:

public class stockdropdown extends jpanel {     private jbutton bt = new jbutton("tsfdsfds");     private jpanel top = new jpanel();     private jpanel middle = new jpanel();      public stockdropdown() {         setlayout(new borderlayout());         top.add(bt);         // add("north", top);         add(top, borderlayout.page_start);     } } 

Comments