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 8619683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:23:57+00:00 2026-06-12T06:23:57+00:00

Im writing the content of text field to a text file: import java.awt.event.ActionListener; import

  • 0

Im writing the content of text field to a text file:

    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.SwingUtilities;
    import java.io.FileNotFoundException;
    import java.util.Formatter;
    import javax.swing.*;
    import java.util.*;
    import java.io.*;

    public class Payroll extends JFrame implements ActionListener{

    private JButton btn1;
    private JButton btn2;
    private JButton btnadd;


    // initialize the lbl with caption name is employee information.
     JLabel lbl = new JLabel("Nilai University Payroll System");

    public Payroll(){
     super("Nilai University Payroll System");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     setLayout(null);

     JLabel lblid,lblname,lblrank,lblclass,lbltype,lblpay;
     JTextField txtid,txtname,txtrank,txtclass,txtEmployeeClass,txtEmployeeType;
     JRadioButton rb1,rb2,rb3,rb4,rb5;

     lbl.setBounds(200,50,500,100);
     lbl.setHorizontalAlignment(lbl.CENTER );

     lblid = new JLabel("Employee ID: ");
     lblname = new JLabel("Employee Name: ");
     lblrank = new JLabel("Employee Rank: ");
     lblclass = new JLabel("Employee Class: ");
     lbltype = new JLabel("Class Type: ");
     lblpay = new JLabel("Employee Pay Rate: ");

  // initialize all the label which are declared in the example above with its caption name 
     lblid.setBounds(300,140,100,20);
     lblname.setBounds(300,180,100,20);
     lblrank.setBounds(300,220,100,20);
     lblclass.setBounds(300,260,100,20);
     lbltype.setBounds(300,300,100,20);
     lblpay.setBounds(300,340,100,20);

     // add all the label on the frame
     add(lblid);
     add(lblname);
     add(lblrank);
     add(lblclass);
     add(lbltype);
     add(lblpay); 

  // initialize the text field with size
     txtid=new JTextField(15);
     txtname=new JTextField(15); 
     txtclass=new JTextField(15);
     txtEmployeeClass=new JTextField(15);
     txtEmployeeType=new JTextField(15);

     // set a particular position on a screen with set bounds constructor
     txtid.setBounds(400,140,100,20);
     txtname.setBounds(400,180,100,20);
     txtclass.setBounds(400,220,100,20);
     txtEmployeeClass.setBounds(400,260,100,20);
     txtEmployeeType.setBounds(400,300,100,20);

  // add text field on a Frame
     add(txtid);
     add(txtname);
     add(txtclass);
     add(txtEmployeeClass);
     add(txtEmployeeClass);

  // initialize radio button with its caption
     rb1 = new JRadioButton("1. Excellent");
     rb2 = new JRadioButton("2. Good");
     rb3 = new JRadioButton("3. Average");
     rb4 = new JRadioButton("4. Fair");
     rb5 = new JRadioButton("5. Poor");

     // set a particular position on a Frame
     rb1.setBounds(400,220,100,20);
     rb2.setBounds(500,220,100,20);
     rb3.setBounds(600,220,100,20);
     rb4.setBounds(700,220,100,20);
     rb5.setBounds(800,220,100,20);

     // add button on a frame
     add(rb1);
     add(rb2);
     add(rb3);
     add(rb4);
     add(rb5);

     btnadd = new JButton("Add Employee");
     btnadd.setToolTipText("Click this button to add employee details to a text file.");
     btnadd.setBounds(400,320,150,20);
     add(btnadd);
     btnadd.addActionListener(this);
     btnadd.setActionCommand("Add");
    }
    private BufferedWriter output;
     public void actionPerformed(ActionEvent e){

         String cmd = e.getActionCommand();
         if(cmd.equals("Add"))
         {
            output.write("ID: "+txtid.getNume()+"\n");
         }   
     }

     public void WriteFile(){
            try {
        output = new BufferedWriter(new FileWriter("E:/EC3307/eclipse/Payroll.txt",true));        
                output.newLine();
                output.close();
            }
            catch(IOException e)
            {
                System.out.println("There was a problem:" + e);

            }
        }

  public static void main(String args[]){

      SwingUtilities.invokeLater(new Runnable(){
          @Override
          public void run()
          {
              Payroll f1=new Payroll();
              // set frame size
              f1.setSize(1000,600);
              // set frame visible true
              f1.setVisible(true);  
          }
      });
      }
     }   
  • 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-06-12T06:23:58+00:00Added an answer on June 12, 2026 at 6:23 am

    The problem is that txtid is a local variable of your Payroll constructor and not a member of the Payroll class. Simply declare txtid as variable of your Payroll class and this will allow to access it in your actionPerformed method.

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

Sidebar

Related Questions

I'm have a StringBuilder that is writing content to a file. Towards the end
A software is producing UTF-8 files, but writing content to the file that isn't
I am writing a script for renaming/copying the content of a file. I have
I'm currently writing out xml and have done the following: header (content-type: text/xml); header
All, I'm writing a web app that will receive user generated text content. Some
I've used xml type by writing (asp coding) Response.ContentType = text/xml Now xml contents
I'm writing a simple content management system. I need to store SHA1 hash values
What is the best way of writing the some content to the beginning and
I'm writing code to display html content in Qwebview, i did it by setHtml
I am writing a chrome extension that is a 'content script' I want to

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.