Hey people, Im just wondering what type of layout you would recommend to set up a grid of labels and sliders:

This is the format that i would like them to be in.
JLabel - Jslider - JLabel
JLabel - Jslider - JLabel
Slider Name - Slider - Slider Value

With a gridLayout the sizes of space allocated to the component is the same - meaning there is an unnessaserly large amount of space for the JLabels.
A gridbagLayout also worked - however setting one up resulted in the sliders being horizontal along my screen and i need them to be vertical piled on one another. Otherwise it worked fine for what i need.

Thanks for any suggestions :)

Recommended Answers

All 4 Replies

I guess you want the sliders lined up nicely?
Try a BorderLayout with the labels in the WEST and EAST and the sliders in the CENTER.
You'll get a BorderLayout with in each of WEST, EAST and CENTER a JPanel with a GridLayout.
Each GridLayout has 3 rows and 1 column.
NORTH and SOUTH are empty, you could use those for for example a text describing the entire block or just leave them be.

Something like

contentPane = (JPanel) getContentPane();
        contentPane.setLayout(borderLayout1);
        contentPane.add(jPanel1, java.awt.BorderLayout.CENTER);
        jPanel1.add(jSlider1);
        jPanel1.add(jSlider3);
        jPanel1.add(jSlider2);
        contentPane.add(jPanel2, java.awt.BorderLayout.WEST);
        jPanel2.add(jLabel4);
        jPanel2.add(jLabel6);
        jPanel2.add(jLabel5);
        contentPane.add(jPanel3, java.awt.BorderLayout.EAST);
        jPanel3.add(jLabel1);
        jPanel3.add(jLabel3);
        jPanel3.add(jLabel2);

Swing UI design is often best done on a piece of paper using pencil and eraser.

Thats a fine idea, alas, one ive already tried with little success as the labels and sliders do not line up correctly - the labels end up being bunched together.

Thanks for the suggestion though :)

hmm, I tried it and it looks fine.
I guess you used a different layout manager somewhere, probably a FlowLayout instead of a GridLayout...

By placing a gridlayout in each of the fields of the borderlayout they will line up correctly (as the vertical space taken up by each cell will be identical for all grid layouts in this case).
That will guarantee correct allignment as components are by default centered in a cell (or rather stretched to the entire cell size which in case of components with a fixed height like labels and sliders effectively centers them).

I noticed I forgot a few lines:

jPanel1.setLayout(gridLayout1);
        jPanel2.setLayout(gridLayout2);
        jPanel3.setLayout(gridLayout3);
        gridLayout1.setRows(3);
        gridLayout2.setRows(3);
        gridLayout3.setRows(3);

Hey you were right - not so sure what i did differently. mostlikely didn't give the label panes any sort of layout let alone a grid one. :)
ta muchly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.