469,299 Members | 2,069 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

Aligning Items in JComboBox

I have a JComboBox with a list of numbers, from 1 digit to 5 digits.
Numbers with more than 3 digits have a comma in them.

I've been aligning them with leading spaces. Is there any simple and quick
way to align all the items in a JComboBox with the right edge of the box
(which, in this case, would mean all columns keep the same place value)?

Thanks!

Hal
Jul 17 '05 #1
3 4514
I've not tried this out but it might work...

myCombo.setRenderer(new DefaultListCellRenderer(){
public Component getListCellRendererComponent(
JList l,Object o,int i,boolean s, boolean f){
Component c=super.getListCellRendererComponent(l,o,i,s,f);
if(c instanceof JLabel)
((JLabel)c).setHorizontalTextPosition(SwingConstan ts.RIGHT);
}
});

If it doesn't, try changing "DefaultListCellRenderer" to
"javax.swing.plaf.basic.BasicComboBoxRenderer" ? Or look at the docs
for these classes.

Hal Vaughan <ha*@thresholddigital.com> wrote in message news:<nlB0c.152794$jk2.594715@attbi_s53>...
I have a JComboBox with a list of numbers, from 1 digit to 5 digits.
Numbers with more than 3 digits have a comma in them.

I've been aligning them with leading spaces. Is there any simple and quick
way to align all the items in a JComboBox with the right edge of the box
(which, in this case, would mean all columns keep the same place value)?

Thanks!

Hal

Jul 17 '05 #2
sorry - and "return c" afterwards!
Jul 17 '05 #3
S Manohar wrote:
I've not tried this out but it might work...

myCombo.setRenderer(new DefaultListCellRenderer(){
public Component getListCellRendererComponent(
JList l,Object o,int i,boolean s, boolean f){
Component c=super.getListCellRendererComponent(l,o,i,s,f);
if(c instanceof JLabel)
((JLabel)c).setHorizontalTextPosition(SwingConstan ts.RIGHT);
}
});

If it doesn't, try changing "DefaultListCellRenderer" to
"javax.swing.plaf.basic.BasicComboBoxRenderer" ? Or look at the docs
for these classes.
Thanks! I'll see how it works. If it doesn't, it also gives me a starting
point, so I can read up on DefaultListCellRenderer().

Hal

Hal Vaughan <ha*@thresholddigital.com> wrote in message
news:<nlB0c.152794$jk2.594715@attbi_s53>...
I have a JComboBox with a list of numbers, from 1 digit to 5 digits.
Numbers with more than 3 digits have a comma in them.

I've been aligning them with leading spaces. Is there any simple and
quick way to align all the items in a JComboBox with the right edge of
the box (which, in this case, would mean all columns keep the same place
value)?

Thanks!

Hal


Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by - ions | last post: by
1 post views Thread by Robert | last post: by
2 posts views Thread by Dave | last post: by
2 posts views Thread by jerico | last post: by
2 posts views Thread by thesti | last post: by
2 posts views Thread by javatech007 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.