When the text is longer than the Swing JComboBox text box it gets centered, this method ensures they all show the end of the text - Java Swing

Java examples for Swing:JComboBox

Description

When the text is longer than the Swing JComboBox text box it gets centered, this method ensures they all show the end of the text

Demo Code


//package com.java2s;

import javax.swing.JComboBox;

import javax.swing.text.JTextComponent;

public class Main {
    /**/*  ww w  . j av a  2  s.co m*/
     * When the text is longer than the text box it gets centered, this
     * method ensures they all show the end of the text, eg the filename.
     */
    public static void setCaretPosition(JComboBox comboBox) {
        Object item = comboBox.getSelectedItem();
        if (item != null) {
            String val = item.toString();
            JTextComponent c = (JTextComponent) comboBox.getEditor()
                    .getEditorComponent();
            c.setCaretPosition(val.length());
        }
    }
}

Related Tutorials