1

I cant seem to make JButtons work with 2 JTextField, My Goal is:

  1. 1.if App starts and no JTextField is focused(selected) by user : if user presses JButton,append to JTextField tf[0].

    2.if JTextField tf[0] is focused(selected) & user presses JButton b[0] than append number to tf[0].

    3.if JTextField tf[1] is focused(selected) & user presses JButton b[0] than append number to tf[1].

Here is my code, i Tried using Only ActionListener & than added the MouseListener than changed it to MouseAdapter but most of the time ,either Goal 2 or Goal 3 is achieved not both or all 3 Goals at Same Time.

package swingapp;

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;
public class SwingApp {

    JFrame CalcF;
    JPanel p1, p2, p3;
    JLabel j[] = new JLabel[3];
    JButton b[] = new JButton[12];
    JButton op[] = new JButton[4];
    JTextField tf[] = new JTextField[3];
    boolean selected = false;
    Actions a;
    Mouse m;

    SwingApp() {
        a = new Actions();
        m = new Mouse();
        CalcF = new JFrame("Calculator");
        CalcF.setLocationRelativeTo(null);
        CalcF.setSize(300, 300);
        CalcF.setLayout(null);
        CalcF.setResizable(false);
        p1 = new JPanel();
        p2 = new JPanel();
        p3 = new JPanel();
        p1.setLayout(null);
        p1.setBounds(10, 10, 275, 55);
        p1.setBackground(Color.LIGHT_GRAY);
        tf[0] = new JTextField(5);
        tf[1] = new JTextField(5);
        tf[2] = new JTextField(5);
        tf[0].setBounds(5, 25, 70, 20);
        tf[1].setBounds(85, 25, 70, 20);
        tf[2].setBounds(185, 25, 70, 20);
        tf[2].setEditable(false);
        j[0] = new JLabel("Number1");
        j[0].setFont(new Font("SanSerif", Font.BOLD, 14));
        j[0].setBounds(5, 5, 100, 20);
        j[1] = new JLabel("Number2");
        j[1].setFont(new Font("SanSerif", Font.BOLD, 14));
        j[1].setBounds(85, 5, 100, 20);
        j[2] = new JLabel("Result");
        j[2].setFont(new Font("SanSerif", Font.BOLD, 14));
        j[2].setBounds(185, 5, 100, 20);
        for (int i = 0; i < 3; i++) {
            p1.add(tf[i]);
            p1.add(j[i]);
        }
        p2.setLayout(new GridLayout(4, 4));
        p2.setBounds(85, 75, 125, 125);
        b[10] = new JButton("c");
        b[11] = new JButton("s");
        b[10].setToolTipText("Clear all Fields");
        b[11].setToolTipText("Save Your Result");
        for (int i = 9; i >= 0; i--) {

            b[i] = new JButton(String.valueOf(i));
            p2.add(b[i]);
        }
        tf[0].addMouseListener(m);
        b[0].addActionListener(a);
        p2.add(b[10]);
        p2.add(b[11]);
        p3.setBounds(15, 210, 255, 35);
        p3.setBackground(Color.lightGray);
        op[0] = new JButton("Add");
        op[1] = new JButton("Sub");
        op[2] = new JButton("Mul");
        op[3] = new JButton("Div");
        for (int i = 0; i < 4; i++) {
            p3.add(op[i]);
        }

        CalcF.add(p1);
        CalcF.add(p2);
        CalcF.add(p3);
        CalcF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CalcF.setVisible(true);

    }


    public static void main(String[] args) {
        new SwingApp();
    }

    class Mouse extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (tf[0].isFocusOwner()) {
                selected = false;
            } else if (tf[1].isFocusOwner()) {
                selected = true;
            }

        }

    }

    class Actions implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (b[0].isFocusOwner()) {
                if (selected) {
                    tf[0].setText(tf[0].getText() + "0");
                } 
            }
        }

    }

}

This is a Simple Calculator App, I am trying to make. after a day of searching I couldn't find the solution anywhere in stackoverflow or google.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jaymit Desai
  • 153
  • 1
  • 2
  • 11
  • As soon as you click on a number button, either text field loses focus. That's why calculators have one display area. Also, create each panel in a separate class. You are doing way too much in your class constructor. Don't use null layouts. Learn about [Swing layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – Gilbert Le Blanc Dec 19 '14 at 20:57
  • is there a way to do this ,handling 2 textfields – Jaymit Desai Dec 20 '14 at 17:11
  • You could have a toggle button to designate which text field to fill. Or, you could have 2 radio buttons, one for each text field. – Gilbert Le Blanc Dec 20 '14 at 17:17
  • Thanks for the tip but i managed to do it without needing to use a toggle button, i used a FocusListener to set a boolean variable to determine which textfield is selected and using the boolean variable value(true/false) i edited the appropriate JTextField. – Jaymit Desai Dec 20 '14 at 17:28
  • also Thanks for the tip on using different classes for JPanels,it totally made things more clear for me leading to me finding the answer to my problem quickly. – Jaymit Desai Dec 20 '14 at 17:33

0 Answers0