Simple Dropdown menu in Java
Asked Answered
H

7

15

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!

Harley answered 19/3, 2014 at 12:47 Comment(0)
G
6

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

Gytle answered 19/3, 2014 at 12:56 Comment(2)
@Harley yeah you didn't tell all the objects on your panel where to go use .setBounds(yPosition,xPosition,Hieght,Width)Gytle
oops it's actually .setBounds(yPosition,xPosition,width,height), for example lbl.setBounds(10,10,300,10);Gytle
L
5

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

Lenten answered 23/6, 2016 at 18:44 Comment(0)
G
4

You should use one of Java standard Layout (GridLayout, LinearLayout, BoxLayout)

I recommend to use a grid layout with 1 column and 3 rows

like below

  setLayout(new GridLayout(1,3));
         add(new Button("1"));
         add(new Button("2"));
         add(new Button("3"));
Gaudreau answered 19/3, 2014 at 12:56 Comment(0)
C
3

It is due to the Layout which is used to align the children. By default its FlowLayout which lays all the child components in a flow starting from left to right and hence you getting the above display.

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

GridLayout

All Layouts

Cloistered answered 19/3, 2014 at 12:52 Comment(2)
so there is no way to simply display them one up the other 0_o?Harley
Definitely there is. ANd those are the layout managers which act as a container in which all the components reside. So you will have to choose a suitable container to suit your need. Its not that difficult. Try them and you will be glad to see the end result and many more things you can do using LayoutManagers. :)Cloistered
I
1

Study some tutorials on using layout managers, that's where you'll find the solution. They are pretty tough though.

Inefficacious answered 19/3, 2014 at 12:50 Comment(0)
W
0

You should use either BoxLayout, GridBagLayout or GroupLayout to display your components under each other.

with GridBagLayout you can put components in a grid of cells.
while in GroupLayout you can work on a vertical layout.

from my experience making using Panel.setLayout(null) won't help a lot as if you are using frame.pack() which makes the window shrink to it's content size the frame won't consider that there is content and it would shrink to the minimum.
but still setting the layout to null would make you capable of putting the components manually at desired x and y values.

Willhite answered 16/11, 2022 at 1:46 Comment(0)
L
-1

use Panel.setLayout(null); and use setBounds to every component to place them according to your choice eg:

JButton btn=new JButton("Press"); 
btn.setBounds(10, 10, 100, 20);//where(x, y, length, height)
JPanel.add(btn);//this is just a snippet, you will have to declare an object
//for the JPanel:)
Leadbelly answered 30/6, 2020 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.