JComboBox does not appear in JPanel

46 views Asked by At
public void preferencesScreen(){
    preferencesScreen = new JPanel();
    preferencesScreen.setLayout(null);
    preferencesScreen.setBounds(0,0,500,1000);

    backButton = new JButton("<-");
    slider = new JSlider(JSlider.HORIZONTAL,0,100,50);

    restrictionDropdown = new JComboBox<>(user.getPreferences().allergies);
    restrictionDropdown.setLocation(200,300);
    restrictionDropdown.setSize(100,100);
    restrictionDropdown.addActionListener(actionListener);
    restrictionDropdown.setEditable(true);
    preferencesScreen.add(restrictionDropdown);




    slider.setMajorTickSpacing(5);
    slider.setPaintTicks(true);

    backButton.setBounds(20,30,50,40);
    slider.setBounds(300,140,160,50);

    backButton.addActionListener(actionListener);

    preferencesScreen.add(backButton);
    preferencesScreen.add(slider);

    frame.setVisible(true);



}

The restrictionDropdown will not appear on the screen along with the other two components. The class I am working out of extends JComponent. Additionally, when I try to add the combobox to the frame directly, it appears and takes up the entire frame despite setting a size and location.

I've tried changing the border layout to flow, I've tried using setSize and setLocation instead of setBounds, I tried changing the order of the method calls to the combobox, I tried declaring the combobox inside the given method. I tried using the frame instead of the panel itself(see above). None of these except for the last thing caused the combobox to appear

1

There are 1 answers

0
ryvantage On

I made a Minimal, Reproducible Exampleâ„¢ with your code trying to imitate what I think you're trying to accomplish:

public class NewClass1 {

    public static void main(String[] args) {
        NewClass1 newClass = new NewClass1();
        newClass.frame = new JFrame();
        newClass.preferencesScreen();
        newClass.frame.add(newClass.preferencesScreen);
        newClass.frame.pack();
        newClass.frame.setLocationByPlatform(true);
        newClass.frame.setVisible(true);
        newClass.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    JPanel preferencesScreen;
    JButton backButton;
    JSlider slider;
    JComboBox<String> restrictionDropdown;
    String[] allergies = new String[] {"Dust", "Oak"};
    ActionListener actionListener;
    JFrame frame;

    // method name should describe actions ("showPreferencesScreen", etc), not objects
    public void preferencesScreen() {

        // variable name is same as method name -- confusing...
        preferencesScreen = new JPanel();
        preferencesScreen.setLayout(new FlowLayout());
//        preferencesScreen.setBounds(0, 0, 500, 1000);

        backButton = new JButton("<-");
        slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);

        //restrictionDropdown = new JComboBox<>(user.getPreferences().allergies);
        restrictionDropdown = new JComboBox<>(new DefaultComboBoxModel<>(allergies));
        restrictionDropdown.setLocation(200, 300);
        restrictionDropdown.setSize(100, 100);
        restrictionDropdown.addActionListener(actionListener);
        restrictionDropdown.setEditable(true);
        preferencesScreen.add(restrictionDropdown);

        slider.setMajorTickSpacing(5);
        slider.setPaintTicks(true);

        backButton.setBounds(20, 30, 50, 40);
        slider.setBounds(300, 140, 160, 50);

        backButton.addActionListener(actionListener);

        preferencesScreen.add(backButton);
        preferencesScreen.add(slider);

        //frame.setVisible(true); // you don't want this here
    }
}

(notice the code is copy/paste executable -- just create a NewClass1.java file, paste the code, and run -- makes it easy to communicate these problems/solutions).

That code produces this:

enter image description here

Not sure if that's what you were looking for.

A few notes:

  1. Don't use a null Layout. I used FlowLayout to keep your example very simple, but if I'm looking to make a quick UI I almost always use Netbeans 8.2's Swing UI builder (which uses AbsoluteLayout but auto-generates the layout code. (what you see in this pic took 10 seconds literally) enter image description here
  2. Check my other comments for some suggestions on how you can improve.
  3. Swing is great (my favorite UI tool), but it's old and on its way out. So use it to learn but don't make production applications with it.