Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Dropdown menu in Java

I am working on a very simple GUI in Java.

In this GUI I want to display:

  1. A label with some text on the top of the page
  2. A JComboBox under the mentioned label
  3. A JButton under the mentioned JComboBox

Here's my code:

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Prova {

public static void main(String[] args) {

    JFrame frame = new JFrame("A Simple GUI");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocation(430, 100);

    JPanel panel = new JPanel();

    frame.add(panel);

    JLabel lbl = new JLabel("Select one of the possible choices and click OK");
    lbl.setVisible(true);

    panel.add(lbl);

    String[] choices = { "CHOICE 1","CHOICE 2", "CHOICE 3","CHOICE 4","CHOICE 5","CHOICE 6"};

    final JComboBox<String> cb = new JComboBox<String>(choices);

    cb.setVisible(true);
    panel.add(cb);

    JButton btn = new JButton("OK");
    panel.add(btn);

    }
}

Unfortunately, the result I get is

image

As you can see in the image, the label, the JComboBox and the JButton are on the same line!

Instead, I want them "stacked" as described above:

JLabel

JComboBox

JButton

I tried using the setLocation(int x, int y) method, but they always show in the same position.

Many thanks!

like image 333
NoobNe0 Avatar asked Mar 19 '14 12:03

NoobNe0


People also ask

How do you program a drop-down list in Java?

Create a Dropdown Menu Using JComboBox in Java Below, we first create the array of options to display in the dropdown list. JComboBox is a component and needs a frame to reside, so we create a JFrame object. Then, we create the JComboBox object and pass the options array as its argument in the constructor.

What is dropdown in Java?

A dropdown or pull-down menu, also known as spinner, is one of the most essential UI elements for an app. In this tutorial I'll tell you how to add one to your Android app using Java. Dropdown menus organize an app and improve the user experience. Almost every app has a dropdown menu integrated into its user interface.


2 Answers

If I've understood your question, the following code accomplishes what you are trying to do without being overly complicated:

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BoxLayout; // added code
import java.awt.Component; // added code

public class Prova {

public static void main(String[] args) {

    JFrame frame = new JFrame("A Simple GUI");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocation(430, 100);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // added code

    frame.add(panel);

    JLabel lbl = new JLabel("Select one of the possible choices and click OK");
    lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
    //lbl.setVisible(true); // Not needed

    panel.add(lbl);

    String[] choices = { "CHOICE 1", "CHOICE 2", "CHOICE 3", "CHOICE 4",
                         "CHOICE 5", "CHOICE 6" };

    final JComboBox<String> cb = new JComboBox<String>(choices);

    cb.setMaximumSize(cb.getPreferredSize()); // added code
    cb.setAlignmentX(Component.CENTER_ALIGNMENT);// added code
    //cb.setVisible(true); // Not needed
    panel.add(cb);

    JButton btn = new JButton("OK");
    btn.setAlignmentX(Component.CENTER_ALIGNMENT); // added code
    panel.add(btn);

    frame.setVisible(true); // added code

    }
}

The setLocation method is often overly complicated unless you have very specific (artistic?) goals for the layout. For this problem, an easier solution is to use a BoxLayout and specify that you want things added in the y-direction (vertically downwards.) Note that you will have to specify the dimensions of the JComboBox (and some other GUI elements that you might want to add later) to avoid a giant drop-down menu. cf. another stackoverflow post, JComboBox width

like image 162
bballdave025 Avatar answered Oct 03 '22 00:10

bballdave025


use frame.setLayout(null); this will allow you to place the Label, Button etc. where you like

like image 33
Nerakai Avatar answered Oct 03 '22 00:10

Nerakai