Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Left justify text on Combo box

Posted on 2002-07-18
Last Modified: 2007-11-27
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.

Question by:ahTri
  • 4
  • 2

Expert Comment

ID: 7163910
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));

class ComboBoxRenderer extends JLabel
                       implements ListCellRenderer {
    public ComboBoxRenderer() {
    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.

Author Comment

ID: 7164019
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.

Accepted Solution

LonAllen earned 50 total points
ID: 7164952
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();


Lon Allen
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 7195094
How to get the DefaultCellEditor out of the JComboBox ?

Expert Comment

ID: 7196189
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();

This can be condensed into a single line:


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


Either should work.

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


Author Comment

ID: 7202215
Thanks Lon

Author Comment

ID: 7202220
Thanks Lon

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question