按Tab键时在JComboBox中设置插入位置

时间:2022-08-23 22:36:32

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.

我想创建一个jcombobox,它在按下tab时将插入位置设置为0位置。即,当你的面板中有不同的组件,当你按Tab键,它选择jcombobox,并且因为它是可编辑的jcombobox,如果我在任何插入位置点击jcombobox的文本字段,它将保持在该插入位置,当我按Tab键并切换到其他组件,最后到达组合框,插入符号的位置保持不变。当我使用tab键时,我想在组合框的textfield中将插入符号的位置设置为0位置。

1 个解决方案

#1


1  

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:

您可以在获得的焦点上将Caret设置为0位置。但这也将使Caret在鼠标点击上也是如此。如果还可以吗?看到:

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 );
            }
        } );
    }
}

#1


1  

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:

您可以在获得的焦点上将Caret设置为0位置。但这也将使Caret在鼠标点击上也是如此。如果还可以吗?看到:

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 );
            }
        } );
    }
}