[java] Swing/Java: How to use the getText and setText string properly

I'm trying to make input nameField appear in a Label called label1 after a Button called button1 is clicked. Right now it says: 'txt' and I understand why. But I don't know how I can use the string! Can anyone explain me what I'm doing wrong and how to use this string properly?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class thisismytest2 {
    public static void main(String[] args) {

        final JFrame frame = new JFrame();  
        JPanel panel = new JPanel();    
        JTextField nameField = new JTextField("...", 2);    
        JButton button1 = new JButton();
        final JLabel label1 = new JLabel();
        label1.setText("txt");
        label1.setVisible(false);
        String txt = nameField.getText();

        frame.add(panel);
        panel.add(button1);
        panel.add(label1);
        frame.setSize(200,200);
        frame.setVisible(true);
        panel.add(nameField);
        frame.setSize(600,400); 
        nameField.setBounds(400, 40, 400, 30);

        button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                label1.setVisible(true);
            }
        });
        }
        }

This question is related to java swing jbutton jlabel settext

The answer is


Setup a DocumentListener on nameField. When nameField is updated, update your label.

http://download.oracle.com/javase/1.5.0/docs/api/javax/swing/JTextField.html


the getText method returns a String, while the setText receives a String, so you can write it like label1.setText(nameField.getText()); in your listener.


in your action performed method, call:

label1.setText(nameField.getText());

This way, when the button is clicked, label will be updated to the nameField text.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to jbutton

Call japplet from jframe How can I check that JButton is pressed? If the isEnable() is not work? Java - Check if JTextField is empty or not how to create a window with two buttons that will open a new window How to close a GUI when I push a JButton? How to Disable GUI Button in Java How to add action listener that listens to multiple buttons Swing/Java: How to use the getText and setText string properly How to clear the JTextField by clicking JButton How do I add an image to a JButton

Examples related to jlabel

Resize a picture to fit a JLabel Align text in JLabel to the right How to center the text in a JLabel? Swing JLabel text change on the running application Swing/Java: How to use the getText and setText string properly Java: how to add image to Jlabel? How to change the size of the font of a JLabel to take the maximum size How do I set a JLabel's background color? Newline in JLabel Multiline text in JLabel

Examples related to settext

How to print a double with two decimals in Android? Swing/Java: How to use the getText and setText string properly