Simple dropdown menu in Java

I am working on a very simple GUI in Java.

In this GUI, I want to display:

  • Label with text at the top of the page.
  • JComboBox under the specified label
  • JButton under the mentioned JComboBox

Here is 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); } } 

Sorry, I get the result

image

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

Instead, I want them to be β€œstacked,” as described above:

Jlabel

Jcombobox

Jbutton

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

Many thanks!

+7
java user-interface jcombobox jbutton jlabel
source share
5 answers

use frame.setLayout(null); this will allow you to put a shortcut, button, etc. wherever you like

+5
source share

You must use one of the standard Java templates (GridLayout, LinearLayout, BoxLayout)

I recommend using a grid with 1 column and 3 rows

as below

  setLayout(new GridLayout(1,3)); add(new Button("1")); add(new Button("2")); add(new Button("3")); 
+4
source share

This is due to the layout used to align the children. By default its FlowLayout, which contains all the child components in the stream, starting from left to right, and therefore, you get the above screen.

You can use a GridLayout with 3 rows and 1 column as per your requirement.

Gridlayout

All layouts

+2
source share

If I understand your question, the following code will execute what you are trying to do, not too 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 setLocation often too complicated if you do not have very specific (artistic?) Goals for the layout. For this problem, it's easier to use BoxLayout and specify that you want everything to be added in the y direction (vertically down). Note that you will need 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. cp another postoverflow column, JComboBox Width

+1
source share

Check out a few tutorials on using layout managers to find a solution. They are pretty tough though.

0
source share

All Articles