0

I have created buttons 0-9 as well as 4 text fields (JTextField). I am trying to allow the user to click on any number 0-9 to input into each text field. However, I cannot determine which field the user is clicked on. So when the user select the second field, it doesn't show any text it work only on the first field.

field = new JTextField();
    field.setHorizontalAlignment(SwingConstants.RIGHT);
    field.setText("");
    field.setBounds(12, 12, 66, 34);
    field.setBackground(Color.WHITE);
    frame.getContentPane().add(field);
    field.setColumns(10);
    field.setFont(new Font("Liberation Mono", Font.BOLD, 
20));
    field.setFocusable(true);
    field.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent arg0) {
             f=false;
        }

        @Override
        public void focusGained(FocusEvent arg0) {
             f=true;
        }
    });

    field2 = new JTextField();
    field2.setText("");
    field2.setHorizontalAlignment(SwingConstants.RIGHT);
    field2.setFont(new Font("Liberation Mono", Font.BOLD, 
20));
    field2.setColumns(10);
    field2.setBackground(Color.WHITE);
    field2.setBounds(90, 12, 66, 34);
    field2.setFocusable(true);
    field2.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent arg0) {
            f = false;
        }

        @Override
        public void focusGained(FocusEvent arg0) {
            f = true;
        }
    });
    frame.getContentPane().add(field2);

    field3 = new JTextField();
    field3.setFont(new Font("Liberation Mono", Font.BOLD, 
20));
    field3.setHorizontalAlignment(SwingConstants.RIGHT);
    field3.setBounds(199, 12, 66, 34);
    frame.getContentPane().add(field3);
    field3.setColumns(10);
    field3.setFocusable(true);
    field3.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent arg0) {
            f=false;
        }

        @Override
        public void focusGained(FocusEvent arg0) {
             f=true;
        }
    });

    field4 = new JTextField();
    field4.setText("");
    field4.setHorizontalAlignment(SwingConstants.RIGHT);
    field4.setFont(new Font("Liberation Mono", Font.BOLD, 
20));
    field4.setColumns(10);
    field4.setBackground(Color.WHITE);
    field4.setBounds(277, 12, 66, 34);
    frame.getContentPane().add(field4);
    field4.setFocusable(true);
    field4.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent arg0) {
             f = false;
        }

        @Override
        public void focusGained(FocusEvent arg0) {
             f = true;
        }
    });

The button example:

 JButton btn0 = new JButton("0");
    btn0.setFont(new Font("Tahoma",Font.BOLD,15));
    btn0.setBounds(199, 228, 80, 30);
    frame.getContentPane().add(btn0);
    btn0.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                if (f==false) {
                    String num = 
 field.getText()+btn0.getText();
                    field.setText(num);

                }else if (f==false) {
                    String num = 
 field2.getText()+btn0.getText();
                    field2.setText(num);
                }else if (f==false) {
                    String num = 
field3.getText()+btn0.getText();
                    field3.setText(num);
                }
                String 
num=textField.getText()+btn0.getText();
                textField.setText(num);
        }
    });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Yasser_1D
  • 114
  • 8

2 Answers2

2

You should use a TextAction for the ActionListener of your button. The TextAction implements a getFocusedComponent() method that will return to you the last text component that had focus.

So to append text to the last text component with focus the code would be something like:

TextAction digit = new TextAction()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        JButton button = (JButton)e.getSource();
        JTextComponent textField = getFocusedComponent();
        textField.replaceSelection( button.getText() );
    }
}

JButton button0 = new JButton("0");
button0.addActionListener(digit);
JButton button1 = new JButton("1");
button1.addActionListener(digit);

The above action is generic, so it can be used by all your buttons. It will simply append the text of the button to the last text field that had focus.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

You are only showing code fragments, but I looks like you tries to keep track of which field is selected using a boolean. I would skip the boolean and introduce a new variable

JTextField current;

That should be assigned in each focusGained method. Skip all assignments in the focusLost methods. Thus you will be able to track the current active text field. Also note that the focusLost methods will be called when the user is clicking on a button, so they should not be used.

  • you mean replace the boolean variable with JTextField cuurent ? – Yasser_1D Jan 01 '19 at 13:13
  • That's right! The single boolean won't help you know which field had focus last. – Just another Java programmer Jan 01 '19 at 13:18
  • I hope it's clear that the buttons for digits just need to operate on the current field. – Just another Java programmer Jan 01 '19 at 13:21
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). (Start with just ***two*** text fields.) 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. .. – Andrew Thompson Jan 01 '19 at 13:27
  • .. 4) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jan 01 '19 at 13:28
  • Use a `TextAction`. The functionality you are trying to implement is already implemented in the API. – camickr Jan 01 '19 at 15:35