Click here to Skip to main content
15,907,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to display what is typed in the textfield when a button is clicked

What I have tried:

Java
import java.awt.*;
import java.awt.event.*;

public class Q7 {
    Q7(){
       Frame f= new Frame("Calculator");
       TextField txt1 = new TextField(15);
       Button b = new Button("Submit");
       Label lbl = new Label();
       
       b.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
               txt1.addItemListener(new ItemListener(){
                   @Override
                   public void itemStateChanged(ItemEvent e){
                       String s=txt1.getSelectedItem();
                        l2.setText(s);
                   }
               });
           }
       });
       
       f.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent we){
            System.exit(0);
           }
       });
       f.add(txt1);
       f.add(b);
       f.add(lbl);
       
       f.setLayout(new FlowLayout());
       f.setSize(400,300);
       f.setVisible(true);
       
       
    }   
    public static void main(String[] args){
      Q7 f = new Q7();   
    }
}
Posted
Updated 1-Dec-18 1:07am
v2

1 solution

Java
l2.setText(s);

You have not defined l2 anywhere. Your code would be easier to debug if you used proper meaningful names for your variables, rather than 1 or 2 letters.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900