I'm learning JAVA GUI
How Can I add a value from my JButton(1,2,3,[...]) to a specific JTextField with click?
Example:
First> Click to JTextField1
2nd> click on JButton 3
How can I add value JButton 3 to my JTextField selected?
I know how to put a JButton value in a single JTextField, but I don't know how to do it in several different JTextField
I saw some related posts, but I can't implement them in my code, maybe because I don't understand English very well and I can end up confusing some things
I'm trying this:
package jamilzin.magicsquare;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MagicSquare implements ActionListener {
//declarando
JFrame frame;
JPanel panel,panelFunctions;
JButton[] numbers = new JButton[10];
JTextField[] positions = new JTextField[9];
JButton[] functions = new JButton[2];
void magicSquare(){
frame = new JFrame("Quadrado Magico");
frame.setSize(500,500);
panel = new JPanel();
panelFunctions = new JPanel();
numbers[1] = new JButton("1");
numbers[2] = new JButton("2");
numbers[3] = new JButton("3");
numbers[4] = new JButton("4");
numbers[5] = new JButton("5");
numbers[6] = new JButton("6");
numbers[7] = new JButton("7");
numbers[8] = new JButton("8");
numbers[9] = new JButton("9");
functions[0]= new JButton("Limpar");
functions[1]= new JButton("Sair");
for(int i=1;i<10;i ){
numbers[i].addActionListener(this);
numbers[i].setBackground(Color.lightGray);
}
for(int i=0; i<9;i ){
positions[i] = new JTextField();
positions[i].addActionListener(this);
}
panel.setBounds(0, 0, 500, 400);
panel.setLayout(new GridLayout(6,3,0,0));
panelFunctions.setBounds(50,410,400,30);
panelFunctions.setLayout(new GridLayout(1,2,250,0));
panel.add(positions[0]);
panel.add(positions[1]);
panel.add(positions[2]);
panel.add(positions[3]);
panel.add(positions[4]);
panel.add(positions[5]);
panel.add(positions[6]);
panel.add(positions[7]);
panel.add(positions[8]);
panel.add(numbers[1]);
panel.add(numbers[2]);
panel.add(numbers[3]);
panel.add(numbers[4]);
panel.add(numbers[5]);
panel.add(numbers[6]);
panel.add(numbers[7]);
panel.add(numbers[8]);
panel.add(numbers[9]);
panelFunctions.add(functions[0]);
panelFunctions.add(functions[1]);
frame.setLayout(null);
frame.setVisible(true);
frame.add(panel);
frame.add(panelFunctions);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==positions[1]){
positions[1].setText("5");
// positions[1].setText(e.getActionCommand());
}
}
public static void main(String[] args) {
MagicSquare quadrado = new MagicSquare();
quadrado.magicSquare();
}
}
CodePudding user response:
You'll want to have a look at How to Write a Focus Listener
Basically, you need to attach a FocusListener
to each text field. When a text field gains focus, you will want to set a property to the instance of the JTextField
which gained focus. When you tap a button, you use this property to set the text (assumming it's not null
)
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField[] positions = new JTextField[9];
private JButton[] numbers = new JButton[10];
private JTextField lastFocusedField;
public TestPane() {
JPanel textFieldPane = new JPanel(new GridLayout(3, 3));
for (int index = 0; index < positions.length; index ) {
positions[index] = new JTextField(10);
positions[index].addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
lastFocusedField = (JTextField)e.getSource();
}
});
textFieldPane.add(positions[index]);
}
JPanel buttonGridPane = new JPanel(new GridLayout(3, 3));
for (int index = 0; index < numbers.length; index ) {
numbers[index] = new JButton(Integer.toString(index));
numbers[index].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (lastFocusedField != null) {
lastFocusedField.setText(e.getActionCommand());
}
}
});
}
for (int index = 0; index < 9; index ) {
buttonGridPane.add(numbers[index]);
}
JPanel numbersPane = new JPanel(new GridLayout(2, 1));
numbersPane.add(buttonGridPane);
numbersPane.add(numbers[9]);
GridBagConstraints gbc = new GridBagConstraints();
setLayout(new GridBagLayout());
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(textFieldPane, gbc);
add(numbersPane, gbc);
}
}
}
Now, you need to make some decisions, when you tap a button should you reset the "last focused text field" or not. Because otherwise, you can keep tapping buttons and the last focused text field will keep updating.
CodePudding user response:
@Override
public void actionPerformed(ActionEvent e) {
JButton btnPressed = (JButton)e.getSource();
for(int i=0; i<numbers.length;i ){
if(numbers[i] == btnPressed){
positions[i-1].setText(btnPressed.getText());
}
}
}