Swing/Java: How to use the getText and setText string properly
Asked Answered
N

4

6

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);
            }
        });
        }
        }
Nicholas answered 29/3, 2011 at 18:34 Comment(3)
You just retrieved the initial text of your nameField textfield, and your txt variable won't be updated automatically since there is no binding between that variable and the text property of the textfield, which is why you need to update the txt variable in the actionPerformed method, as the answers point out. If you're interested (later on) in being able to bind elements together (much more powerful approach), then know that there are libraries to do that such as: java.net/projects/beansbindingLitotes
Thanks a lot for your answer! I appreciate it and take a good look at itNicholas
How do you get the name of a JButton so that you can check it in an IF statement???Notorious
P
7

You are setting the label text before the button is clicked to "txt". Instead when the button is clicked call setText() on the label and pass it the text from the text field.

Example:

label1.setText(nameField.getText()); 
Psychotic answered 29/3, 2011 at 18:36 Comment(6)
Thank you for your quick response, and then i can remove setText from the row above?Nicholas
yes you dont need it there unless you want it to have a default text.Narcose
So i don't even need String txt = nameField.getText ? :oNicholas
@Opoe, you only need to save the text into a String variable if you intend on using it elsewhere.Psychotic
okay thanks, the error says the nameField has to be declared final. Do i have to add final before JTextField?Nicholas
Sorry i got it :) Thank you so much for your time and help appreciate it!Nicholas
N
4

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.

Narcose answered 29/3, 2011 at 18:39 Comment(0)
G
2

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.

Gur answered 29/3, 2011 at 18:40 Comment(0)
P
0

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

Purulence answered 29/3, 2011 at 18:43 Comment(1)
this link is to an old version of Java.Psychotic

© 2022 - 2024 — McMap. All rights reserved.