Samar Anand
Samar Anand

Reputation: 15

Setting Caret Position in JComboBox when Tab is pressed

I want to create a jcombobox which sets the caret position to 0 position when tab is pressed. i.e when there are different components in your panel and when you press tab, and it selects jcombobox, and since it is editable jcombobox if i clicked on textfield of jcombobox at any caret position, it will stay at that caret position, when i pressed tab and switch to other components and finally reach the combobox the position of caret remains same. I want to set the position of caret in textfield of combobox to 0 position when i use tab button.

Upvotes: 1

Views: 834

Answers (1)

oliholz
oliholz

Reputation: 7507

You can set the Caret to 0 position on focus gained. But this will set the Caret on mouse click, too. If it is ok? see:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;

public class BorderPanel extends JFrame{

    public BorderPanel() {

        JButton btn0 = new JButton( "Hilfe" );
        JButton btn1 = new JButton( "Apply" );
        JButton btn2 = new JButton( "Cancel" );
        JButton btn3 = new JButton( "OK" );

        JPanel sub1 = new JPanel(new FlowLayout( FlowLayout.TRAILING, 5, 5 ));
        JPanel sub2 = new JPanel(new FlowLayout( FlowLayout.TRAILING, 5, 5 ));

        JMenuBar mnu = new JMenuBar();
        mnu.add( new JMenu( "File" ) );
        mnu.add( new JMenu( "Insert" ) );
        mnu.add( new JMenu( "Edit" ) );
        mnu.add( new JMenu( "Help" ) );

        JComboBox<String> cmb = new JComboBox<String>();
        cmb.getEditor().getEditorComponent().addFocusListener( new FocusAdapter() {
            @Override
            public void focusGained( FocusEvent e ) {
                if(e.getSource() instanceof JTextComponent) {
                    ((JTextComponent)e.getSource()).setCaretPosition( 0 );
                }
            }
        } );

        cmb.setEditable( true );
        cmb.addItem( "Lorem" );
        cmb.addItem( "Ipsum" );
        cmb.addItem( "Dolor" );
        cmb.addItem( "Sit Amet" );

        JPanel pnl = new JPanel();
        pnl.add(cmb);

        sub1.add( btn0 );
        sub2.add( btn1 );
        sub2.add( btn2 );
        sub2.add( btn3 );

        JPanel bar = new JPanel();
        bar.setLayout( new BorderLayout( 20, 20 ) );
        bar.add( sub1, BorderLayout.WEST );
        bar.add( Box.createHorizontalGlue(), BorderLayout.CENTER );
        bar.add( sub2, BorderLayout.EAST );

        setDefaultCloseOperation( EXIT_ON_CLOSE );
        getContentPane().setLayout( new BorderLayout() );
        getContentPane().add( mnu, BorderLayout.NORTH );
        getContentPane().add( pnl, BorderLayout.CENTER );
        getContentPane().add( bar, BorderLayout.SOUTH );
        setSize( new Dimension( 600, 400 ) );
        setLocationRelativeTo( null );
    }

    public static void main( String[] args ) {
        EventQueue.invokeLater( new Runnable() {
            @Override
            public void run() {
                new BorderPanel().setVisible( true );
            }
        } );
    }
}

Upvotes: 1

Related Questions