4

I would like to position a JComboBox in the way illustrated in the picture below. I was wondering if there is easy code to achieve this?

enter image description here

Roman C
  • 49,761
  • 33
  • 66
  • 176
Marcello
  • 423
  • 1
  • 5
  • 12

3 Answers3

3

use an appropriate layout manager, e.g. BoxLayout. for more information, please see How to Use BoxLayout.

mre
  • 43,520
  • 33
  • 120
  • 170
3

Centered Combos

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

public class CenteredCombos {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                String[] values = {"Dog","Cat"};
                // put the controls in a single column, with a little space.
                JPanel controlPanel = new JPanel(new GridLayout(0,1,5,5));
                for (int ii=0; ii<4; ii++) {
                    controlPanel.add(new JComboBox(values));
                }

                // the GUI as seen by the user (without frame)
                // The GBL is used to center the controlPanel
                JPanel gui = new JPanel(new GridBagLayout());
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));
                gui.add(controlPanel);

                // Make the BG panel more clear..
                gui.setBackground(Color.WHITE);

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

you could use MigLayout with row and column constraints like [max][min][max] - would divide your root panel into a 3x3 grid with "greedy" borders, and in the middle area (the one thats min/min) drop a JPanel with a vertical flow layout, into which you could add your components

radai
  • 23,949
  • 10
  • 71
  • 115