Solved

Left justify text on Combo box

Posted on 2002-07-18
7
625 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
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:lwinkenb
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:ahTri
Comment Utility
How to get the DefaultCellEditor out of the JComboBox ?
0
 

Expert Comment

by:LonAllen
Comment Utility
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
Comment Utility
Thanks Lon
0
 

Author Comment

by:ahTri
Comment Utility
Thanks Lon
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now