Search code examples
javaswingjbuttonjtextfield

how FocusEvent input field and use button


I have multiple text fields and buttons. When a text field is selected, text should be added to it when a button is pressed but nothing is inserted with bellow code. what i have missed. thanks for your help.

    public class ButtonExample_Extended extends JFrame implements  ActionListener {
    public JPanel createContentPane (){
    buttonPanel = new JPanel();
    buttonPanel.setLayout(null);
    buttonPanel.setLocation(10, 50);
    buttonPanel.setSize(1370, 770);
    totalGUI.add(buttonPanel);

    B9 = new JButton("9");
    B9.setLocation(1190, 570);
    B9.setSize(50, 50);
    B9.addActionListener(this);
    buttonPanel.add(B9);

    JPasswordField passwordField = new JPasswordField(20);
    passwordField.setLocation(900,565);
    passwordField.setSize(120,30);
    buttonPanel.add(passwordField);
    }

  private JTextComponent selectedTextField;

    // TextFields onFocus event
    private void a33FocusGained(java.awt.event.FocusEvent evt) {
        selectedTextField = (JTextComponent) evt.getSource();
    }

    // action for button

        public void actionPerformed (ActionEvent evt) {
        if (evt.getSource() == B9)
        selectedTextField.setText( selectedTextField.getText() + "9" );


    }
    }

with above code i expected to insert 9 to textPasswordField but it doesn't.


Solution

  • Are you sure that

    private void a33FocusGained(java.awt.event.FocusEvent evt) {
        selectedTextField = (JTextComponent) evt.getSource();
    }
    

    is ever called? I guess your class should implement FocusListener and add something like

    passwordField.addFocusListener(this);
    
    @Override
    public void focusGained(FocusEvent e) {
        selectedTextField = (JTextComponent) e.getSource();
    }
    
    @Override
    public void focusLost(FocusEvent e) {
        selectedTextField = null;
    }
    

    This is a sample code of what you should do (if I understood you correctly), note that at first you need to set cursor to the password field and after that the button will work, however you can see the bad side of this approach in the focusLost method

    public class Snippet implements ActionListener, FocusListener {
    public JFrame totalGUI = new JFrame();
    private JPanel buttonPanel;
    private JButton B9;
    
    public Snippet() {
        createContentPane();
    }
    
    public void createContentPane() {
        buttonPanel = new JPanel(new GridBagLayout());
    
        B9 = new JButton("9");
        B9.addActionListener(this);
        buttonPanel.add(B9);
    
        JPasswordField passwordField = new JPasswordField(20);
        passwordField.setSize(120, 30);
        passwordField.addFocusListener(this);
        buttonPanel.add(passwordField);
    
        totalGUI.getContentPane().add(buttonPanel);
        totalGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        totalGUI.pack();
    }
    
    private JTextComponent selectedTextField;
    
    @Override
    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == B9 && selectedTextField != null)
            selectedTextField.setText(selectedTextField.getText() + "9");
    }
    
    public static void main(String[] args) {
        new Snippet().totalGUI.setVisible(true);
    }
    
    @Override
    public void focusGained(FocusEvent e) {
        if(e.getSource() instanceof JTextComponent)
            selectedTextField = (JTextComponent) e.getSource();
    }
    
    @Override
    public void focusLost(FocusEvent e) {
        // when you push the button the text field will lose focus
        // selectedTextField = null;
    }
    

    }