简体   繁体   中英

How to store text field value in to a variable for further use of it in java swing?

initialized a variable "input" and i want store the text in to it. so that i can perform search operation using that variable. by the below code it is not taking the variable input. thanks inadvance. please anyone help me

String input;

    jb.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae) 
        {
            input = jt.getText(); // problem occurs here
            jl.setText(input);  
        }
    });

NOTE: if you want to use the object anywhere of your program please declare it at the top of your program. Like that

import java.awt.EventQueue;

public class MyExample {

    private JFrame frame;
    String input;

You shouldn't declare type of 'input' at out of an anonymous class. In this way, you have an error like 'The final local variable input cannot be assigned, since it is defined in an enclosing type'. In order to avoid this error, please make sure that you write the code in this way,

        jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String input = jt.getText();
                jl.setText(input);
            }
        });

So you can put the input into JLabel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM