I'm trying to make a small application where U input ID (with help of JButton -from 0 to 9 - ) and then pass the number that have been pressed to putText-Method and then display it in JTextField-the problem is that every time I press new number the one I pressed before disapears:s . Could anyone help me with that pls?
public class IdPanel extends JPanel {
private JTextField idField;
private JLabel idLabel;
public IdPanel() {
setLayout(new GridBagLayout());
setPreferredSize(new Dimension(500, 70));
idField = new JTextField(20);
idField.setFont(new Font("Serif", Font.PLAIN, 20));
idLabel = new JLabel("Enter ID:");
idLabel.setFont(new Font("Serif", Font.PLAIN, 20));
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.insets = new Insets(9, 9, 9, 9);
gc.anchor = GridBagConstraints.CENTER;
add(idLabel, gc);
gc.gridx = 1;
gc.anchor = GridBagConstraints.CENTER;
add(idField, gc);
}
public void putText(String number) {
idField.setText(number);
}
}
Here is an example with 9 buttons:
try: