Solved

Left justify text on Combo box

Posted on 2002-07-18
7
630 Views
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.

Thanks
0
Comment
Question by:ahTri
[X]
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
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:lwinkenb
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));
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
 

Author Comment

by:ahTri
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.
0
 

Accepted Solution

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

tc.setCaretPosition(0);

Lon Allen
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

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

Expert Comment

by:LonAllen
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();
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
 

Author Comment

by:ahTri
ID: 7202215
Thanks Lon
0
 

Author Comment

by:ahTri
ID: 7202220
Thanks Lon
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

688 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