swing - How to add text to JTextArea? java -


I have three buttons in my program and a JTextArea is what I want to do when the user presses a button / s , So I wanted to have JTextArea to press 1 button to text it, button 2 was pressed and so on. for example.

  Jebton Button 1 = New Pocket (); Pocket button 2 = new pocket (); Pocket button 3 = new pocket (); JTextArea text = new JTextArea (); Jeffre Frame = New Gefram (); Frame.add (Button1); Frame.add (Button2); Frame.add (Button3); Frame.add (text); Frame.setVisible (true);  

What do I do, when the user presses the button 1, I want JTextArea to press the button 1 button and then press the user button 2, I want JTextArea to last To keep the text and text for button 2. So it should say something;

The button pressed 2 button pressed button 2 was pressed

and if I had more buttons, it would look like

  Button 1 pressed button 2 was pressed Button 3 pressed button 4 was pressed Button 5 Pressed button was pressed 6 was pressed  

and so on.

Add actionListener to each boat

 < Code> yourTextArea.append ("button X was pressed \ n");  

Here's a simple demo

  JFrame frame = new jframe (); Frame.setLayout (New Flow Layout ()); Last JTextArea Area = New JTextArea (2,20); . Connecting Frame.getContentPane () (field); Pocket button 1 = new pocket ("press me"); Pocket button 2 = new pocket ("press me"); Button1.addActionListener (New Exhibitist) {Public Zero Action Action (Action Event e) {area.append ("Button 1 was pressed \ n");}}); Button2.addActionListener (new executable () {Public Zero Action Perffered (Action Event e) {area.append ("Button 2 was pressed \ n");}}); . Add frame.getContentPane () (Button1); . Add Frame.getContentPane () (Button2); Frame.setSize (300,300); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.setVisible (true);  

You can also use

  yourTextArea.append ("button X was pressed \ n") instead of  

;

If you want to add a new line at the beginning of the text field


Comments