By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

Aligning Items in JComboBox

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
sorry - and "return c" afterwards!
Jul 17 '05 #3

P: n/a
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.