Higeath
Higeath

Reputation: 561

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

I have frame JFrame to which I'm adding gui JPanel which consists of multiple different JPanels and one of which is in a different class.

The problem is I'm unable to add that other classes JPanel to my gui JPanel I get no errors but nothing is 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 to 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 to 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);
    };

}

Upvotes: 1

Views: 475

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You do get the other JPanel but you've added nothing to it since components are added in its constructor and the StockDropdown constructor is never called. I'd get rid of the static fields and methods unless you plan to create a true valid static factory method, one that calls the 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 this 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 that if StockDropdown extends JPanel, get rid of the ui variable, because with it, you're adding components to a container that is 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);
    }
}

Upvotes: 2

Related Questions