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
I made a Minimal, Reproducible Exampleâ„¢ with your code trying to imitate what I think you're trying to accomplish:
(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:
Not sure if that's what you were looking for.
A few notes:
nullLayout. I usedFlowLayoutto 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 usesAbsoluteLayoutbut auto-generates the layout code. (what you see in this pic took 10 seconds literally)