How to Add text to JTextArea

Im creating a programme using java. I want the user to enter some text, then push the button so the text entered shows in the label. However, I have 2 problems. First, the text are isn´t displaying when I execute the app. Second, I don´t know how to allow the user to type in the area. Im new in java so that´s why Im asking. Here is the code. Thank you.

import javax.swing.*;
import java.awt.event.*;



class Boton extends JFrame implements ActionListener {
    JButton boton;
    JTextArea textArea = new JTextArea();
    JLabel etiqueta = new JLabel();


    public Boton() {
        setLayout(null);
        boton = new JButton("Escribir");
        boton.setBounds(100, 150, 100, 30);
        boton.addActionListener(this);
        add(boton);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == boton) {
            try {
                String texto = textArea.getText();
                etiqueta.setText(texto);
                Thread.sleep(3000);
                System.exit(0);
            } catch (Exception excep) {
                System.exit(0);
            }
        }
    }
}

public class Main{
    public static void main(String[] ar) {
        Boton boton1 =new Boton();
        boton1.setBounds(0,0,450,350);
        boton1.setVisible(true);
        boton1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Upvotes: 0

Views: 1115

Answers (2)

Problems:

  1. You never add the JTextArea into your GUI, and if it doesn't show, a user cannot directly interact with it.
  2. You are calling Thread.sleep on the Swing event thread, and this will put the entire application to sleep, meaning the text that you added will not show.
  3. Other issues include use of null layouts and setBounds -- avoid doing this.

Solutions:

  1. Set the JTextArea's column and row properties so that it sizes well.
  2. Since your JTextArea's text is going into a JLabel, a component that only allows a single line of text, I wonder if you should be using a JTextArea at all. Perhaps a JTextField would work better since it allows user input but only one line of text.
  3. Add the JTextArea to a JScrollPane (its viewport actually) and add that to your GUI. Then the user can interact directly with it. This is most easily done by passing the JTextArea into a JScrollPane's constructor.
  4. Get rid of the Thread.sleep and instead, if you want to use a delay, use a Swing Timer. check out the tutorial here

For example:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.KeyEvent; 
import javax.swing.*;

public class Main2 {
    public static void main(String[] args) {
        // create GUI in a thread-safe manner
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        BotonExample mainPanel = new BotonExample();
        JFrame frame = new JFrame("GUI");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

class BotonExample extends JPanel {
    private JLabel etiqueta = new JLabel(" ");
    private JButton boton = new JButton("Escribir");
    
    // jtext area rows and column properties
    private int rows = 5;
    private int columns = 30;
    private JTextArea textArea = new JTextArea(rows, columns);
    
    public BotonExample() {
        // alt-e will activate button
        boton.setMnemonic(KeyEvent.VK_E);
        boton.addActionListener(e -> {
            boton.setEnabled(false); // prevent button from re-activating
            String text = textArea.getText();
            etiqueta.setText(text);
            
            // delay for timer
            int delay = 3000;
            Timer timer = new Timer(delay, e2 -> {
                // get current window and dispose ofit
                Window window = SwingUtilities.getWindowAncestor(boton);
                window.dispose();
            });
            timer.setRepeats(false);
            timer.start();  // start timer
        });
        
        // create JPanels to add to GUI
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
        topPanel.add(new JLabel("Etiqueta:"));
        topPanel.add(etiqueta);
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(boton);
        
        JScrollPane scrollPane = new JScrollPane(textArea);
        
        // use layout manager and add components
        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.PAGE_START);
        add(scrollPane, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);
        
    }
}

Upvotes: 3

Chris
Chris

Reputation: 1

textarea.setText("Text");  // this will insert text into the text area
textarea.setVisable(true); // this will display the text area so you can type in it
textarea.setSize(500,500); // set size of the textarea so it actually shows

The user should be able to type in the TA when it is displayed and just do a getText to pull the text

Upvotes: -1

Related Questions