How to Set Focus on JTextField?

后端 未结 10 1838
感情败类
感情败类 2020-11-30 07:34

I make my game run without mouse so using pointer is not a choice. High Score menu will show when player lose.

this is my code

    highScore=new MyTe         


        
相关标签:
10条回答
  • 2020-11-30 07:56

    if is there only one Top-Level Container then last lines in GUI constructor would be for example

    .
    .
    .
    myFrame.setVisible(true);
    EventQueue.invokeLater(new Runnable() {
    
       @Override
         public void run() {
             myComponent.grabFocus();
             myComponent.requestFocus();//or inWindow
         }
    });
    
    0 讨论(0)
  • 2020-11-30 07:56

    If the page contains multiple item and like to set the tab sequence and focus I will suggest to use FocusTraversalPolicy.

    grabFocus() will not work if you are using FocusTraversalPolicy.

    Sample code

    int focusNumber = 0;
    Component[] focusList;
    focusList = new Component[] { game, move, amount, saveButton,
                printButton, editButton, deleteButton, newButton,
                settingsButton };
    
    frame.setFocusTraversalPolicy(new FocusTraversalPolicy() {
    
            @Override
            public Component getLastComponent(Container aContainer) {
                return focusList[focusList.length - 1];
            }
    
            @Override
            public Component getFirstComponent(Container aContainer) {
                return focusList[0];
            }
    
            @Override
            public Component getDefaultComponent(Container aContainer) {
                return focusList[1];
            }
    
            @Override
            public Component getComponentAfter(Container focusCycleRoot,
                    Component aComponent) {
                focusNumber = (focusNumber + 1) % focusList.length;
                if (focusList[focusNumber].isEnabled() == false) {
                    getComponentAfter(focusCycleRoot, focusList[focusNumber]);
                }
                return focusList[focusNumber];
            }
    
            @Override
            public Component getComponentBefore(Container focusCycleRoot,
                    Component aComponent) {
                focusNumber = (focusList.length + focusNumber - 1)
                        % focusList.length;
                if (focusList[focusNumber].isEnabled() == false) {
                    getComponentBefore(focusCycleRoot, focusList[focusNumber]);
                }
                return focusList[focusNumber];
            }
        });
    
    0 讨论(0)
  • 2020-11-30 07:57

    How about put jTextField.requestFocusInWindow(); into jTextField FocusLost event? Works for me have 5 controls on JPanel Soon as click on MessageBox, focus lost on jTextField. Used all the suggested codes but no luck Only above method works my case.

    0 讨论(0)
  • 2020-11-30 08:02
    public void actionPerformed(ActionEvent arg0)
    {
        if (arg0.getSource()==clearButton)
        {
            enterText.setText(null);
            enterText.grabFocus();  //Places flashing cursor on text box        
        }       
    }
    
    0 讨论(0)
  • 2020-11-30 08:02

    Try this one,

    myFrame.setVisible(true);
    EventQueue.invokeLater(new Runnable() {
    
       @Override
         public void run() {
             myComponent.grabFocus();
             myComponent.requestFocus();//or inWindow
         }
    });
    
    0 讨论(0)
  • It was not working for me when tried to use:

    JOptionPane.showConfirmDialog(...)
    

    But - I found a solution ! Very primitive, but works.

    Just jump to the field by java.awt.Robot using key "Tab". For example:

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_TAB);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_TAB);
    

    If you should press multiple times on "Tab" to get your Component you can use below method:

    GUIUtils.pressTab(3);
    

    Definition:

    public static void pressTab(int amountOfClickes)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    Robot robot = new Robot();
                    int i = amountOfClickes;
                    while (i-- > 0)
                    {
                        robot.keyPress(KeyEvent.VK_TAB);
                        robot.delay(100);
                        robot.keyRelease(KeyEvent.VK_TAB);
                    }
                }
                catch (AWTException e)
                {
                    System.out.println("Failed to use Robot, got exception: " + e.getMessage());
                }
            }
        });
    }
    

    If your Component location is dynamic, you can run over the while loop without limitation, but add some focus listener on the component, to stop the loop once arrived to it.

    0 讨论(0)
提交回复
热议问题