-1

I have 2 jbuttons which is the jButton1 and jButton2 and also have jTextField1. Now, i want to happen that whenever i set a text in my jTextField1 and press the jButton1, jButton2 text will change to jTextField1 text. But the text will be nothing in the button like blank text "". Please help thanks :(

Heres my code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    
     jButton2.setText(jTextField1.getText());
    
}
    
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • see this post hope this will help https://stackoverflow.com/questions/1386782/jbutton-needs-to-change-jtextfield-text – Umair Mubeen Nov 04 '20 at 06:43

1 Answers1

1

try this code:

JFrame frame = new JFrame("test");
frame.setLayout(new BorderLayout());

JTextField txtfield = new JTextField();
frame.add(txtfield, BorderLayout.NORTH);

Panel p = new Panel();

JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");

p.add(btn1);
p.add(btn2);

btn1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        btn2.setText(txtfield.getText());
    }
});

frame.add(p, BorderLayout.CENTER);

frame.setVisible(true);
frame.setSize(480, 320);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36