Eljay
Eljay

Reputation: 951

Reading a value from Textfield

I want to read a value from textfied in Java but I am not able to read it Here is my code

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

public class TextField extends JDialog {
  TextField() {
    JFrame frm = new JFrame("SAMPLE PROGRAM");
    frm.setBounds(150,150,420,400);
    frm.setLayout(null);
    Container content = frm.getContentPane();
    content.setBackground(Color.cyan);
    JTextField text = new JTextField();
    text.setBounds(70,25,100,30);
    JButton button1, button2; 
    button1 = new JButton("PROGRAMMER");
    button2 = new JButton("USER");
    button1.setBounds(270,25,120,50);
    button2.setBounds(270,90,120,50);
    button1.addActionListener(new ButtonHandler());
    button2.addActionListener(new ButtonHandler());
    frm.add(button1);
    frm.add(button2);
    frm.add(text);
    frm.setVisible(true);
    frm.setResizable(false);
  }

  public static void main(String[] args) {
    new TextField();
  }
  class ButtonHandler implements ActionListener { 
    public void actionPerformed(ActionEvent e) {     
      String str = new String();     
      str = e.getActionCommand();    
      System.out.println(" " + str);
    }
  }  
}

I tried the following methods

1.In the Class Textfield i used this method under button2.addactionlistener.It gave an error

Cannot refer to a non-final variable text inside an inner class defined in a different method

button1.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae) {  
    if(text.getText().equals("joe")) 

2.In the class ButtonHandler

it says that text cannot be resolved

What Method should i use to read the textfield and in which class should in read it

Upvotes: 1

Views: 22170

Answers (2)

mKorbel
mKorbel

Reputation: 109815

1) if you rename (possible conflict with with AWT API with name TextField) and remove JDialog, because it is never used

public class TextField extends JDialog { TextField(){

to

public class MyTextField { public MyTextField(){

2) and change the main method

public static void main(String[] args) {
    new TextField();
}

to

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            private final JTabbedPane jtp = new JTabbedPane();

            @Override
            public void run() {
                MyTextField textField = new MyTextField();
            }
        });
    }

3) remove all chars >

4) add DefaultCloseOparation for JFrame, otherwise your program will stay in the memory until your PC is restarted or switched off

5) remove all un_Swing methods and use a LayoutManager

Upvotes: 2

Russell
Russell

Reputation: 805

String str = text.getText();

this should work if you replace

str = e.getActionCommand();

with

 String str = text.getText();

If that doesn't work, you might have to create the JTextField as a static JTextField or add the final modifier.

PS: next time, only put in the necessary code, not the JFrame or JButtons, it just makes reading the code easier.

Upvotes: 1

Related Questions