if a gui have 2 text fields and there are 10 buttons(0-9) to input numbers in text fields then how it will choose that which field get input. this i problem in my gui i write 2 methods but not working my code is below

public void actionPerformed(ActionEvent e){

        if(e.getSource()==b1){

            if(op1.isValidateRoot())
                op1.setText(op1.getText()+"1");
            else if(op2.isValidateRoot())
                op2.setText(op2.getText()+"1");
        }
}

it is not working and op1and op2 are two JTextField what will be the solution

has it occurred to you that maybe neither if expression is true?
then there is no text set. just remove the if statements, or add an additional

else{
op1.setText("invalid input");
}

after your last if. it should work just fine.

about your question:

"how it will choose that which field get input. this i problem in my gui i write 2 methods but not working my code is below"

'it' doesn't get to choose which field gets new text. it's your code that determines which field is updated.
"i write 2 methods"

if statements are not methods.

"not working" see, this is about the least informational post one can make.
"not working" might mean a lot of things:
1. a logical error, you made a mistake in your code, and you get a result that differs from what you expect.
2. a compile time error, and your code doesn't even compile, but produces a compile time error, then you should tell us about that
3. a run time error, your code compiles and runs, but crashes.
this 'll also produce either an error message or stacktrace.

from just a snippet of your code, and a vague description of your problem, it's impossible for us to decide which it is.

ok this is my full code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MiniButton implements ActionListener{

    private JFrame frame;
    private JButton plus,mul;
    private JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
    private JLabel fOper,sOper,answer;
    private JTextField op1,op2,ans;

    public void initGUI(){

        frame=new JFrame("Mini Calculator");
        plus=new JButton("+");
        mul=new JButton("*");
        b0=new JButton("0");
        b1=new JButton("1");
        b2=new JButton("2");
        b3=new JButton("3");
        b4=new JButton("4");
        b5=new JButton("5");
        b6=new JButton("6");
        b7=new JButton("7");
        b8=new JButton("8");
        b9=new JButton("9");
        fOper=new JLabel("First Value");
        sOper=new JLabel("Second Value");
        answer=new JLabel("Answer");
        op1=new JTextField(19);
        op2=new JTextField(19);
        ans=new JTextField(19);

        //set size of components

        plus.setSize(new Dimension(90,40));
        mul.setSize(new Dimension(90,40));
        b0.setSize(new Dimension(90,40));
        b1.setSize(new Dimension(90,40));
        b2.setSize(new Dimension(90,40));
        b3.setSize(new Dimension(90,40));
        b4.setSize(new Dimension(90,40));
        b5.setSize(new Dimension(90,40));
        b6.setSize(new Dimension(90,40));
        b7.setSize(new Dimension(90,40));
        b8.setSize(new Dimension(90,40));
        b9.setSize(new Dimension(90,40));

        fOper.setSize(100,50);
        sOper.setSize(100,50);
        answer.setSize(80,50);

        op1.setSize(130,20);
        op2.setSize(130,20);
        ans.setSize(130,20);

        //set locations of components

        b1.setLocation(30,170);
        b2.setLocation(180,170);
        b3.setLocation(330,170);
        b4.setLocation(30,220);
        b5.setLocation(180,220);
        b6.setLocation(330,220);
        b7.setLocation(30,270);
        b8.setLocation(180,270);
        b9.setLocation(330,270);
        b0.setLocation(180,320);

        fOper.setLocation(50,20);
        sOper.setLocation(50,60);
        answer.setLocation(50,100);

        op1.setLocation(150,35);
        op2.setLocation(150,75);
        ans.setLocation(150,115);

        Container c=frame.getContentPane();
        c.setBackground(Color.green);
        c.setLayout(null);

        c.add(b1);
        c.add(b2);
        c.add(b3);
        c.add(b4);
        c.add(b5);
        c.add(b6);
        c.add(b7);
        c.add(b8);
        c.add(b9);
        c.add(b0);

        c.add(fOper);
        c.add(sOper);
        c.add(answer);

        c.add(op1);
        c.add(op2);
        c.add(ans);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,400);
        frame.setVisible(true);

        b0.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){

        if(e.getSource()==b1){

            if(op1.isValidateRoot())
                op1.setText(op1.getText()+"1");
            else if(op2.isValidateRoot())
                op2.setText(op2.getText()+"1");
        }
        else if(e.getSource()==b2){


        }
        else if(e.getSource()==b3){


        }
        else if(e.getSource()==b4){


        }
        else if(e.getSource()==b5){


        }
        else if(e.getSource()==b6){


        }
        else if(e.getSource()==b7){


        }
        else if(e.getSource()==b8){


        }
        else if(e.getSource()==b9){


        }
        else if(e.getSource()==b0){


        }
    }
    public static void main(String ss[]){

        MiniButton mc=new MiniButton();
        mc.initGUI();
    }
}

this is incomplete

Hi,

I don't think isValidateRoot() means what you think it means; I don't think you need it here. You have two JTextField, one for the inputs, the other for the result, so what's the problem? All buttons will use the same JTextField.

basically i want to use only three fields so first text field get first operand and second text field get second operand and answer will show in third text field when user click on any operator button. I think now it will clear to you what i want to do. now problem is that how i connect my gui button pad with first text field and second text field for input. I know user can input values with keyboard but i want to use my own button pad in my gui.

anybody can ans my question

from what I understand, when you click b2 for example, you want ur op1 to be 2? If so, in actionPerformed, the if statements .. for example

if source was b2
    if op1 is empty , op1 = 2;
    else op2 = 2;

and so on until you fill it for all of them.
At the end when all if else statements are checked, you can do operations the input or just write the answers out to the third text field

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.