Reputation: 831
I am new to java programming.I want to perform a task such that when a button is pressed i.e from 0-9 shown on my JFrame in attached code,the value of that button must be assigned to the JField selected before the button was pressed.How to do so?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class calci2 extends JFrame implements ActionListener {
JFrame f1;
JPanel p;
JButton b[] = new JButton[10];
JButton btnadd;
JButton btnmul;
JButton btndiv;
JButton btnsub;
public static JTextField t1;
JTextField t2;
JTextField t3;
JLabel no1;
JLabel no2;
JLabel res;
calci2() {
f1 = new JFrame();
p = new JPanel();
t1 = new JTextField(15);
t2 = new JTextField(15);
t3 = new JTextField(15);
no1 = new JLabel("Enter 1st number");
no2 = new JLabel("Enter 2nd number");
res = new JLabel(" Result is ");
btnadd = new JButton("ADD");
btnmul = new JButton("MUL");
btndiv = new JButton("DIV");
btnsub = new JButton("SUB");
for (int i = 0; i < 10; i++) {
b[i] = new JButton("" + i);
}
btnadd.addActionListener(this);
btnmul.addActionListener(this);
btndiv.addActionListener(this);
btnsub.addActionListener(this);
for (int i = 0; i < 10; i++) {
b[i].addActionListener(this);
}
p.add(no1);
p.add(t1);
p.add(no2);
p.add(t2);
p.add(res);
p.add(t3);
p.add(btnadd);
p.add(btnmul);
p.add(btndiv);
p.add(btnsub);
for (int i = 0; i < 10; i++) {
p.add(b[i]);
}
this.add(p);
}
public static void main(String args[]) {
calci2 c = new calci2();
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setSize(300, 300);
c.setVisible(true);
c.setResizable(false);
c.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
String s1 = new String(t1.getText());
String s2 = new String(t2.getText());
String s3 = new String();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
if (str.equals("ADD")) {
int c = a + b;
s3 = String.valueOf(c);
t3.setText(s3);
}
else if (str.equals("SUB")) {
int c = a - b;
s3 = String.valueOf(c);
t3.setText(s3);
}
else if (str.equals("MUL")) {
int c = a * b;
s3 = String.valueOf(c);
t3.setText(s3);
}
else if (str.equals("DIV")) {
int c = a / b;
s3 = String.valueOf(c);
t3.setText(s3);
}
}
};
Upvotes: 0
Views: 3831
Reputation: 369
To check which text field is now selected, you can create two booleans in the class for t1 and t2, i don't think you need third one for t3
let's say :
boolean t1_selected = false;
boolean t2_selected = false;
Then add another listener in the Const. to the text fields which is the focus listener, focus listener will fire when the text field get focused, then you can change the boolean of this text to true there
t1.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
t1_selected = true;
t2_selected = false;
}
});
t2.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
t1_selected = false;
t2_selected = true;
}
});
And now for the buttons,you need to check for the source of the event in the function actionPerformed, use e.getSource() instead of e.getActionCommand() for all buttons.
for example:
if(e.getSource() == this.b[0]){
if(t1_selected)
{
t1.setText("0");
}
if(t2_selected)
{
t2.setText("0");
}
}
else if(e.getSource() == this.b[1]){
if(t1_selected)
{
t1.setText("1");
}
if(t2_selected)
{
t2.setText("1");
}
}
//rest of cases
also don't put these line at the begin of the function
String s1 = new String(t1.getText());
String s2 = new String(t2.getText());
String s3 = new String();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
they will through exception if the text fields are empty, put them only inside the case of ADD, SUB, DIV and MUL
for example :
else if (e.getSource() == btnadd) {
String s1 = new String(t1.getText());
String s2 = new String(t2.getText());
String s3 = new String();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
int c = a + b;
s3 = String.valueOf(c);
t3.setText(s3);
}
BTW you have to indent your code before posting, it's your job to make your question a clean one
Upvotes: 1