• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 639
  • Last Modified:

Left justify text on Combo box

I have an editable combo box with fix width and the list of strings that may longer than the width of the combo box. How can I left justify the text so what show in the combo box is the beginning of the string , not the end bit.

Thanks
0
ahTri
Asked:
ahTri
  • 4
  • 2
1 Solution
 
lwinkenbCommented:
I believe you need to use a renderer to align the text in a JComboBox.

ComboBoxRenderer renderer = new ComboBoxRenderer();
renderer.setPreferredSize(new Dimension(200, 130));
petList.setRenderer(renderer);

class ComboBoxRenderer extends JLabel
                       implements ListCellRenderer {
    public ComboBoxRenderer() {
        setOpaque(true);
        setHorizontalAlignment(LEFT);
        setVerticalAlignment(CENTER);
    }
    public Component getListCellRendererComponent(
        JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus)
    {return this;}
}

There might be an easier way to do this, but I dont know.
0
 
ahTriAuthor Commented:
you may miss understand me, or I didn't specify it properly, the data in the drop down list is already left aligned, but when you select an item, the text is set on the editable field ... then the text is displayed with the end bid.

What I want is the editable field display the begining of the field.
0
 
LonAllenCommented:
Use the setCaretPosition(int position) of the JTextComponent to move the cursor to the beginning of the editable field.  

DefaultCellEditor edt =(DefaultCellEditor)getCellEditor();

JTextComponent tc=(JTextComponent)edt.getComponent();

tc.setCaretPosition(0);

Lon Allen
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ahTriAuthor Commented:
How to get the DefaultCellEditor out of the JComboBox ?
0
 
LonAllenCommented:
Actually I was speaking in the context of a different component.  For the ComboBoxEditor you should do the following:

ComboBoxEditor cbe = myComboBox.getEditor();    JTextComponent tc=(JTextComponent)cbe.getEditorComponent();
tc.setCaretPosition(0);

This can be condensed into a single line:

((JTextComponent)myComboBox.getEditor().getEditorComponent()).setCaretPosition(0);


You could also do this in the context of a JTextField:

((JTextField)myComboBox.getEditor().getEditorComponent()).setCaretPosition(0);

Either should work.

You would do this in your code where you are editing the comboBox.


Lon
0
 
ahTriAuthor Commented:
Thanks Lon
0
 
ahTriAuthor Commented:
Thanks Lon
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now