Jas
Jas

Reputation: 25

Java JComboBox not displaying

New to Java. I cant get the drop down box to display.

I originally had the code before the button, but when i ran the code (intelliJ idea), neither the drop down nor the button showed up.

stackoverflow wants additional details. Dont know what to add. I am sure its just a noobie mistake in the syntax.

package com.example.guiTest;

import javax.swing.*;
import java.awt.*;

import java.lang.Object;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.JComboBox;

public class Main {

public static void main(String[] args) {

    JFrame window = new JFrame("Position");
    window.setVisible(true);
    window.setSize(500, 500);
    window.setResizable(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    window.setLocation(dim.width/2-window.getSize().width/2, dim.height/2-window.getSize().height/2);;

    JPanel panel = new JPanel();
    panel.setLayout(null);
    window.add(panel);

    JLabel test = new JLabel("This is a test");
    test.setBounds(20,0,120,120);
    panel.add(test);

    JButton button = new JButton("Compile");
    button.setBounds(250, 250, 120, 50);
    panel.add(button);


    String[] noteArray = {"a", "b"};

    JComboBox note = new JComboBox(noteArray);
    note.setPreferredSize(new Dimension(200,130));
    note.setLocation(new Point(200,200));
    note.setEditable(true);
    note.setSelectedIndex(3);
    note.setVisible(true);

    panel.add(note);






}

}

Upvotes: 0

Views: 2307

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

You're using a null layout on your panel JPanel, and the quick answer is that null layouts require that you fully specify the size and location of all added components, that's size not preferredSize. You're setting the latter, not the former.

The better answer is not to use a null layout and setBounds(...). While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

Edit:
or you end up placing your JComboBox right over your JButton (as your code does!).

Edit:
Another issue: don't call setVisible(true) on your JFrame until adding all components to the GUI.

Edit:
And yet another issue: note.setSelectedIndex(3);
The note combo box only has 2 items in it, so why are you trying to set the selected index to 3? This is guaranteed to fail.

So for example:

import java.awt.BorderLayout;
import javax.swing.*;

public class BetterMain extends JPanel {
    private static final String[] ITEMS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    private static final int ROWS = 25;
    private static final int COLUMNS = 40;
    private JButton button = new JButton("Button");
    private JTextField textField = new JTextField(COLUMNS / 2);
    private JComboBox<String> myCombo = new JComboBox<>(ITEMS);
    private JTextArea textArea = new JTextArea(ROWS, COLUMNS);    

    public BetterMain() {
        JPanel topPanel = new JPanel();
        topPanel.add(new JLabel("This is a JLabel"));
        topPanel.add(myCombo);
        topPanel.add(textField);
        topPanel.add(button);

        JScrollPane scrollPane = new JScrollPane(textArea);

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(topPanel, BorderLayout.PAGE_START);
    }

    private static void createAndShowGui() {
        BetterMain mainPanel = new BetterMain();

        JFrame frame = new JFrame("BetterMain");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

Upvotes: 3

Related Questions