Aligned elements in a JComboBox

T

Thomas Richter

Hi folks,

the problem at hand is how to align individual components that are part
of a JComboBox.

The JComboBox uses a custom Renderer to render its contents, and each
content consists of two strings of variable size, arranged in a JPanel.
The getListCellRendererComponent() of the renderer now returns the
JPanel containing the two components.

So far, so good. This works.

The problem is that the sizes of the two components differ througout the
items of the JComboBox, which means that the elements within the JList
in the JCombobox (or rather, its JList rendering the non-editable items)
are not aligned to each other. Of course they can't, they are individual
JPanels each with its own layout manager, and thus do not know anything
on each other.

Needless to say, this looks visually very unattractive.

So what can I do about this? I have a JComboBox with a very complex
layout and the requirement that each entry necessarily is a pair of data
types of various sizes. How can I make the elements be aware of each
other and have a common layout for all of them?

Illustration:

+----------------------------------------------+
| foofoo | barbarbarbarbarbarbarbar |
+----------------------------------------------+
| foofoofoofoofoofoofoo | bar |
+----------------------------------------------+

This is how the JComboBox currently renders. But I want it
laid out such that the separator between the two elements forming one
entry of it are underneath each other.

Any ideas?

Greetings,
Thomas
 
K

Knute Johnson

Hi folks,

the problem at hand is how to align individual components that are part
of a JComboBox.

The JComboBox uses a custom Renderer to render its contents, and each
content consists of two strings of variable size, arranged in a JPanel.
The getListCellRendererComponent() of the renderer now returns the
JPanel containing the two components.

So far, so good. This works.

The problem is that the sizes of the two components differ througout the
items of the JComboBox, which means that the elements within the JList
in the JCombobox (or rather, its JList rendering the non-editable items)
are not aligned to each other. Of course they can't, they are individual
JPanels each with its own layout manager, and thus do not know anything
on each other.

Needless to say, this looks visually very unattractive.

So what can I do about this? I have a JComboBox with a very complex
layout and the requirement that each entry necessarily is a pair of data
types of various sizes. How can I make the elements be aware of each
other and have a common layout for all of them?

Illustration:

+----------------------------------------------+
| foofoo | barbarbarbarbarbarbarbar |
+----------------------------------------------+
| foofoofoofoofoofoofoo | bar |
+----------------------------------------------+

This is how the JComboBox currently renders. But I want it
laid out such that the separator between the two elements forming one
entry of it are underneath each other.

Any ideas?

Greetings,
Thomas

Figure out the largest size of your data in each field and lay out all
the elements with the same width and layout manager.
 
T

Thomas Richter

Figure out the largest size of your data in each field and lay out all
the elements with the same width and layout manager.

Which layout manager accepts fixed widths for its components? If I use a
GridBagLayout, for example, changing the dimension of the components
within changes their alignment, and thus they'll move around even though
the layout manager stays the same.

Greetings,
Thomas
 
K

Knute Johnson

Which layout manager accepts fixed widths for its components? If I use a
GridBagLayout, for example, changing the dimension of the components
within changes their alignment, and thus they'll move around even though
the layout manager stays the same.

Greetings,
Thomas

You'll need one that follows preferred size or lay it out manually.
GridBagLayout won't work for sure. You can look back a few weeks and
there was a discussion about similar issue with text fields. Search for

'align Swing JLabels and JTextFields vertically with'

I did an example for the fellow with SpringLayout I think. I don't
think that would work here either but the idea is similar. GridLayout
would work if it would follow preferred or minimum size but I don't
think it will. You might consider extending that though or write your
own layout manager.
 
B

Bent C Dalager

(...) You might consider extending that though or write your
own layout manager.

While my own personal approach to component layout is to take out my
very big Layout Mallet and repeatedly smash it into GridBagLayout
until it does what I want(*), I think the intent of the Swing (or even
AWT) designers was that people would be writing their own specialized
layouts when they have special needs.

So presumably this is actually a practical thing to do. I would
definitely look into it for a situation like this.

Cheers,
Bent D

* There probably isn't a layout mallet big enough to make
GridBagLayout work for the problem at hand though.
 
J

John B. Matthews

Bent C Dalager said:
(...) You might consider extending that though or write your own
layout manager.

[...] I think the intent of the Swing (or even AWT) designers was
that people would be writing their own specialized layouts when they
have special needs.

So presumably this is actually a practical thing to do. I would
definitely look into it for a situation like this.

See also "Creating a Custom Layout Manager":

<http://download.oracle.com/javase/tutorial/uiswing/layout/custom.html>
 
R

Roedy Green

This is how the JComboBox currently renders. But I want it
laid out such that the separator between the two elements forming one
entry of it are underneath each other.

I have not experimented with this but see what you can do with
setBorder
setInsets

Also see what alignment commands you have.

see http://mindprod.com/jgloss/border.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
How long did it take after the car was invented before owners understood
cars would not work unless you regularly changed the oil and the tires?
We have gone 33 years and still it is rare to uncover a user who
understands computers don't work without regular backups.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top