Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7184719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:15:31+00:00 2026-05-28T18:15:31+00:00

I want to read a value from textfied in Java but I am not

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:15:32+00:00Added an answer on May 28, 2026 at 6:15 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read an appSettings value from the c:\Program Files\Microsoft SQL Server\MSSQL.4\Reporting Services\ReportManager\Web.config
I want to read all cell value from excel sheet using Apache POI and
I want to read the value from my DatePicker and store it in my
I want to read excel display value, not value that excel internally saves. Problem
I want to read/extract the value from HSSFComment. I can access the HSSFComment by
For example, if I want to read the middle value from magic(5) , I
In my grails application I want to read some values from properties file and
I want to read an specific xml node and its value for example <customers>
I have a text file. I want read that file. But In that if
I want to read the contents of a URL but don't want to hang

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.