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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:29:07+00:00 2026-06-18T07:29:07+00:00

Basically what I’m doing here is importing the contents of a text file into

  • 0

Basically what I’m doing here is importing the contents of a text file into user input fields (there are 7 of them. I run a loop to go through them for command [i]. The code works fine within the console, but it wont work for the GUI as it only inputs the last line for each field. Have you got any clue what the problem is? Here is the UI:

http://imageshack.us/f/692/21778853.jpg

As you can see, it prints into the console fine, but when I import data by clicking import on the GUI then it just puts the last command of the txt file

My code is:

    final JTextField Command[];//Create one dimensional array 
    Command = new JTextField[8];//Declare Command as a JTextField 
    for (int i = 1; i < 8; i++)//Run the loop through each selection 
    { 
        Command[i] = new JTextField(i);//Read through each text field
    }
    //Add user input fields including their absolute position on the panel
    Command[1].setText("Please enter the commands...");//Starting text
    Command[1].setBounds(121, 13, 236, 25);//Set the position of the text field
    getContentPane().add(Command[1]);//Add this text field to the content
            Command[2].setBounds(121, 47, 236, 25);/fsdfsdf
    getContentPane().add(Command[2]);

    Command[3].setBounds(121, 83, 236, 25);/fasdfasd
    getContentPane().add(Command[3]);

    Command[4].setBounds(121, 119, 236, 25);/fsfasdasdf
    getContentPane().add(Command[4]);

    Command[5].setBounds(121, 155, 236, 25);
    getContentPane().add(Command[5]);

    Command[6].setBounds(121, 191, 236, 25);
    getContentPane().add(Command[6]);

    Command[7].setBounds(121, 227, 236, 25);
    getContentPane().add(Command[7]);  

            //IMPORT FILE   
    JMenuItem Import = new JMenuItem("Import");
    File.add(Import);
    Import.addActionListener(new ActionListener() {//Call up ActionListener function
        public void actionPerformed(ActionEvent arg0) {//Event handler
            try {
                //use buffering, reading one line at a time
                //FileReader always assumes default encoding is OK!
                BufferedReader input = new BufferedReader(new FileReader("W:\\EclipsePortable\\Data\\workspace2\\" +
                                        "RobotController\\src\\RobotController\\Import commands.txt"));
                try {
                    String line = null; //not declared within while loop
                    while (( line = input.readLine()) != null) {
                        System.out.println(line);//Print the lines
                        for(int i = 1; i < 8; i++) {
                        Command[i].setText(line);
                    }
                }}
                finally {
                    input.close();
                }
            }
            catch (IOException ex){
                ex.printStackTrace();
            }
    }});`
  • 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-18T07:29:08+00:00Added an answer on June 18, 2026 at 7:29 am

    So, in your file import code you are doing this…

    for(int i = 1; i < 8; i++) {
        Command[i].setText(line);
    

    Which is applying the current line of text to ALL the fields (the same line of text for all the fields), which means by the time you finish reading the file, you’ve only got the last line of the file.

    Instead, I would keep a reference to the field index and increment in each line read

    Added Example

                    String line = null; //not declared within while loop
                    int fieldIndex = 0;
                    while (( line = input.readLine()) != null) {
                        System.out.println(line);//Print 
                        Command[fieldIndex].setText(line);
                        fieldIndex++
                        if (fieldIndex > 7) {
                            break;
                        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I wrote a script that generates a xml file based on user input.
Basically when a user enters data into a textfield on Form2 , I want
Basically I need to get older version of a file in the repository without
Basically I want to check the extended permissions the user has granted my app.
basically I want to validate whether or not a text box is a certain
Basically I need some text like: I have an ice cream cone. You are
Basically when user resizes my application's window I want application to be same size
Basically I need to take two arrays, merge them with unique values and sum
Basically, I want to be able to run multiple threads - these threads will
Basically, I have code where the user clicks a link, and then a div

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.