Solved

Left justify text on Combo box

Posted on 2002-07-18
7
629 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 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