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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:04:05+00:00 2026-06-15T10:04:05+00:00

I have a filereader and need to use the text for the text of

  • 0

I have a filereader and need to use the text for the text of a loop of labels. I am having trouble.I need the text in the labels to be the aray of names from setLabel method.

main class a
package build;

import java.io.IOException;
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JPanel;

public class a {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        a st = new a(); // object <<<<<<<<<<<<<<<<<<<<

        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

            JButton button;
        JLabel headlabel;
        JLabel label[] = new JLabel[10];
        JRadioButton radioButton;
        JPanel panel1, panel2, panel3, panel4;


            pane.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();

panel1 = new JPanel(new GridBagLayout());
    c.gridx = 0;
    c.gridy = 0;
    pane.add(panel1, c);
    panel2 = new JPanel(new GridBagLayout());
    c.gridx = 0;
    c.gridy = 1;
    pane.add(panel2, c);
    panel3 = new JPanel(new GridBagLayout());
    c.gridx = 0;
    c.gridy = 2;
    pane.add(panel3, c);
        for (int i = 0; i < 10; i = i+1) {
            for (int e = 0; e < 8; e = e+1) {
                label[i]= new JLabel(st.setLabel()); // return String[] stocknames <<<<<<<<<<<
                label[i].setBorder(BorderFactory.createLineBorder(Color.black));
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.ipady = 0;       //reset to default
                    c.weighty = 1.0;   //request any extra vertical space
                    c.anchor = GridBagConstraints.PAGE_END; //bottom of space
                    //c.insets = new Insets(5,5,5,5);  //top padding
                    c.gridx = e;       //aligned with button 2
                    //c.gridwidth = 100;   //2 columns wide
                    c.gridy = i;       //third row
                    panel3.add(label[i], c);
            }
        }
    }


    /**
    * Create the GUI and show it.  For thread safety,
    * this method should be invoked from the
    * event-dispatching thread.
    */
    private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("Stock Table");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Set up the content pane.
        //frame.setPreferredSize(new Dimension(600, 600));
            addComponentsToPane(frame.getContentPane());

            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }


        public static void main(String[] args) {

            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        createAndShowGUI();
                    }
            });
        }

    public String[] setLable() throws IOException {
        String file_name = "path";

        try {
            ReadFile file = new ReadFile(file_name);
            String[] aryLines = file.OpenFile();
            String[] names = new String[aryLines.length];
            String[] links = new String[aryLines.length];

            int i;
            for(i=2; i < aryLines.length; i++) {
                //System.out.println(aryLines[i]);
                int firstquote = aryLines[i].indexOf("\"")+1;
                int secondquote = aryLines[i].indexOf("\"", firstquote+1);
                int thirdquote = aryLines[i].indexOf("\"", secondquote+1)+1;
                int fourthquote = aryLines[i].indexOf("\"", thirdquote+1);
                names[i] = aryLines[i].substring(firstquote, secondquote);
                links[i] = aryLines[i].substring(thirdquote, fourthquote);


            }

        return names;
        }
        catch (IOException e) {
            System.out.println( e.getMessage() );
        }

    }

}

read file class

package build;

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class ReadFile {
    private String path;

    public ReadFile(String file_path) {
        path = file_path;
    }

    public String[] OpenFile() throws IOException {

        FileReader fr = new FileReader(path);
        BufferedReader textReader = new BufferedReader(fr);

        int numberOfLines = readLines();
        String[] textData = new String[numberOfLines];

        int i;

        for(i=0; i < numberOfLines; i++) {
            textData[i] = textReader.readLine();
        }

        textReader.close();
        return textData;

    }

    int readLines() throws IOException {

        FileReader file_to_read = new FileReader(path);
        BufferedReader bf = new BufferedReader(file_to_read);

        String aLine;
        int numberOfLines = 0;

        while ((aLine = bf.readLine()) != null) {
            numberOfLines++;
        }

        bf.close();

        return numberOfLines;

    }


}

Completed

package build;


import java.io.IOException;
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JPanel;

public class a {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        a st = new a();
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

            JButton button;
        JLabel headlabel;
        JLabel label[] = new JLabel[10];
        JRadioButton radioButton;
        JPanel panel1, panel2, panel3, panel4;


            pane.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            if (shouldFill) {
                //natural height, maximum width
                c.fill = GridBagConstraints.HORIZONTAL;
            }

        panel1 = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 0;
        pane.add(panel1, c);
        panel2 = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 1;
        pane.add(panel2, c);
        panel3 = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 2;
        pane.add(panel3, c);


        String[] labels = st.setLabel();
        for (int i = 0; i < 10; i = i+1) {
            for (int e = 0; e < 8; e = e+1) {
                label[i]= new JLabel(labels[i]);
                label[i].setBorder(BorderFactory.createLineBorder(Color.black));
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.ipady = 0;       //reset to default
                    c.weighty = 1.0;   //request any extra vertical space
                    c.anchor = GridBagConstraints.PAGE_END; //bottom of space
                    //c.insets = new Insets(5,5,5,5);  //top padding
                    c.gridx = e;       //aligned with button 2
                    //c.gridwidth = 100;   //2 columns wide
                    c.gridy = i;       //third row
                    panel3.add(label[i], c);
            }
        }




    }


    /**
    * Create the GUI and show it.  For thread safety,
    * this method should be invoked from the
    * event-dispatching thread.
    */
    private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("Stock Table");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Set up the content pane.
        //frame.setPreferredSize(new Dimension(600, 600));
            addComponentsToPane(frame.getContentPane());

            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }

        public String[] setLabel() {
            String file_name = "path";
            String[] names = null;
            try {
                ReadFile file = new ReadFile(file_name);
                String[] aryLines = file.OpenFile();
                String[] links = new String[aryLines.length];
                names = new String[aryLines.length];

                int i;
                for(i=2; i < aryLines.length; i++) {
                        //System.out.println(aryLines[i]);
                        int firstquote = aryLines[i].indexOf("\"")+1;
                        int secondquote = aryLines[i].indexOf("\"", firstquote+1);
                        int thirdquote = aryLines[i].indexOf("\"", secondquote+1)+1;
                        int fourthquote = aryLines[i].indexOf("\"", thirdquote+1);
                        names[i] = aryLines[i].substring(firstquote, secondquote);
                        links[i] = aryLines[i].substring(thirdquote, fourthquote);
                }
        }catch (IOException e) {
                System.out.println( e.getMessage() );
            }
            return names;

        }

        public static void main(String[] args) {


         // get the values of x & y from class A
        int x = ReadWrite.getX();
        int y = ReadWrite.getY();
        // print them out
        //System.out.println("x = " + x + ", y = " + y);
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
        });
    }
}
  • 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-15T10:04:06+00:00Added an answer on June 15, 2026 at 10:04 am

    Move the declaration of String[] names in setLable() before try block and similarly move the return statement after catch e.g.

        public String[] setLable() {
          String file_name = "path";
          String[] names = null;
          try {
            ReadFile file = new ReadFile(file_name);
            String[] aryLines = file.OpenFile();
            String[] links = new String[aryLines.length];
            names = new String[aryLines.length];
    
            int i;
            for(i=2; i < aryLines.length; i++) {
                //System.out.println(aryLines[i]);
                int firstquote = aryLines[i].indexOf("\"")+1;
                int secondquote = aryLines[i].indexOf("\"", firstquote+1);
                int thirdquote = aryLines[i].indexOf("\"", secondquote+1)+1;
                int fourthquote = aryLines[i].indexOf("\"", thirdquote+1);
                names[i] = aryLines[i].substring(firstquote, secondquote);
                links[i] = aryLines[i].substring(thirdquote, fourthquote);
            }
           }catch (IOException e) {
            System.out.println( e.getMessage() );
           }
           return names;
        }
    

    Please note: If there is any exception before the population of names, then it will returnnull. Also I removed the throws as you are handling the exception(printing the message).

    Lastly, method name doesn’t sound appropriate. It should be better named as getLables or getLableNames.

    You need to use the String[] of lables to set the the string as:

      String [] lables = st.setLabel();//change to getLables
      for (int i = 0; i < 10; i = i+1) {
        for (int e = 0; e < 8; e = e+1) {
          label[i]= new JLabel(lables[i]);//use string array index to set the string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple text reading code for Visual Basic: Dim fileReader As String
Hello I am trying to call a method (fileReader) that is using openFileInput(filename) from
I need to write a simple terminal-based program that should, Read some text from
I need help setting up Jadriens FileReader.js . I have set up everything as
To use code I have written for performing calculations, I need to read in
I have a huge text file (207 MB, 4 million lines) and I need
I need to load from text file text which I will be replacing in
I have a (ANSI) text file (filters.txt) on my computer, on each line is
I would like to call an R script from Java. I have done google
I have some jar files that need to read files at startup. These files

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.