Problem to input character from JButton into different JTextField
Hi,
I have 2 JButtons and 2 JTextField, each JButton hold a character.
The following code is only allow to input character for one JTextField, does any idea to make it for different JTextField?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JTest {
JFrame frame = new JFrame("Keyboard");
JPanel pnlMain = new JPanel();
JButton btnA = new JButton("A");
JButton btnB = new JButton("B");
JTextField txtA = new JTextField();
JTextField txtB = new JTextField();
StringBuffer strBuffer;
public JTest() {
pnlMain.setLayout(new GridLayout(2,2,2,2));
pnlMain.add(txtA);
pnlMain.add(txtB);
pnlMain.add(btnA);
pnlMain.add(btnB);
btnA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
strBuffer = new StringBuffer(txtA.getText());
strBuffer.append(command);
txtA.setText(strBuffer.toString());
}
});
btnB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
strBuffer = new StringBuffer(txtA.getText());
strBuffer.append(command);
txtA.setText(strBuffer.toString());
}
});
Container contentPane = frame.getContentPane();
contentPane.add(pnlMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
new JTest();
}
}
Thanks a lot for helping.
[1709 byte] By [
arjensena] at [2007-11-27 9:42:42]

# 1
if you click button a it display A on txtA and if you click button b it display B on txtB...is that what you want?
# 2
Hi,Actually, for example when i focus on txtA, after clicking the button the character will store into the txtA. On when i focus on txtB, the character will store into txtB. Got any idea? Thanks.
# 3
so basically if I focus on txtA and click button A or button B it will display on txtA, and if I focus on txtB and click button A or button B it will display on txtB..am I right? just want to clarify.
# 4
yup, you are right. do you have any solution?
My solution is create another global variable then to store the id or name that can uniquely identifies a textfield because cannot directly get the textfield on focus, because the focus will be lost when click on the button so need to store the latest "on focus textfield's Id/name" into a global variable. Any idea how to capture the last focus? Or do you have a better solution?
# 5
yah you need to place global variable because if you click the buttons it losses focus...
i modified your code and added focus listener.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class help {
JFrame frame = new JFrame("Keyboard");
JPanel pnlMain = new JPanel();
JButton btnA = new JButton("A");
JButton btnB = new JButton("B");
JTextField txtA = new JTextField();
JTextField txtB = new JTextField();
StringBuffer strBuffer;
int point; // declare global
public help() {
pnlMain.setLayout(new GridLayout(2,2,2,2));
pnlMain.add(txtA);
pnlMain.add(txtB);
pnlMain.add(btnA);
pnlMain.add(btnB);
btnA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(point == 1){
strBuffer = new StringBuffer(txtA.getText());
strBuffer.append(command);
txtA.setText(strBuffer.toString());
}else{
strBuffer = new StringBuffer(txtB.getText());
strBuffer.append(command);
txtB.setText(strBuffer.toString());
}
}
});
btnB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(point == 1){
strBuffer = new StringBuffer(txtA.getText());
strBuffer.append(command);
txtA.setText(strBuffer.toString());
}else{
strBuffer = new StringBuffer(txtB.getText());
strBuffer.append(command);
txtB.setText(strBuffer.toString());
}
}
});
txtA.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
point = 1;
}
public void focusLost(FocusEvent e) {
}
});
txtB.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
point = 2;
}
public void focusLost(FocusEvent e) {
}
});
txtA.requestFocus();
Container contentPane = frame.getContentPane();
contentPane.add(pnlMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
new help();
}
}
# 6
hi,
your codes work pretty fine, thanks a lot.
by the way, if got 10 buttons and 10 textfields, then issit have to set if else statement for 10 times for each button? Does it got any shorter way since the code will be very long? lolx~ But i think it can't right since each txtfield needs to assign a point value. Or can we use CaretListener?
Message was edited by:
arjensen
# 7
so can do something like this to make you code short.
btnA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch(point){
case 1:
myMethod(command,txtA); break;
case 2:
myMethod(command,txtB); break;
}
}
});
private void myMethod(String command, JTextField txt){
strBuffer = new StringBuffer(txt.getText());
strBuffer.append(command);
txt.setText(strBuffer.toString());
}
# 8
If wan to seperate textFields and Buttons to different class file, issit any way to do it?
For example
JTest file
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JTest {
JFrame frame = new JFrame("Keyboard");
JPanel pnlMain = new JPanel();
JTextField txtA = new JTextField();
JTextField txtB = new JTextField();
JTextField txtC = new JTextField();
JTextField txtD = new JTextField();
JTextField txtAll;
//int point; // declare global
public JTest() {
pnlMain.setLayout(new GridLayout(2,2,2,2));
pnlMain.add(txtA);
pnlMain.add(txtB);
pnlMain.add(txtC);
pnlMain.add(txtD);
txtA.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
//point = 1;
txtAll = txtA;
//new JTestButton(txtAll);
}
public void focusLost(FocusEvent e) {
}
});
txtB.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
//point = 2;
txtAll = txtB;
//new JTestButton(txtAll);
}
public void focusLost(FocusEvent e) {
}
});
txtC.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
//point = 3;
txtAll = txtC;
//new JTestButton(txtAll);
}
public void focusLost(FocusEvent e) {
}
});
txtD.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
//point = 4;
txtAll = txtD;
//new JTestButton(txtAll);
}
public void focusLost(FocusEvent e) {
}
});
txtA.requestFocus();
Container contentPane = frame.getContentPane();
contentPane.add(pnlMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
new JTest();
}
}
JTestButton file
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JTestButton {
JFrame frame = new JFrame("Keyboard");
JPanel pnlMain = new JPanel();
JButton btnA = new JButton("A");
JButton btnB = new JButton("B");
JTextField txtField;
StringBuffer strBuffer;
public JTestButton(JTextField txtAll) {
txtField = txtAll;
pnlMain.setLayout(new GridLayout(2,1,2,2));
pnlMain.add(btnA);
pnlMain.add(btnB);
btnA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
strBuffer = new StringBuffer(txtField.getText());
strBuffer.append(command);
txtField.setText(strBuffer.toString());
}
});
btnB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
strBuffer = new StringBuffer(txtField.getText());
strBuffer.append(command);
txtField.setText(strBuffer.toString());
}
});
Container contentPane = frame.getContentPane();
contentPane.add(pnlMain);
frame.pack();
frame.setVisible(true);
}
}
My problem how we use the JTest file call the JTestButton file, because we can't always call the JTestButton when different txtField is focus. Got any idea to seperate JTextField and JButton into different class file?
