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

  • Home
  • SEARCH
  • 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 8067285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:14:30+00:00 2026-06-05T12:14:30+00:00

The below code is used to create a line graph. I am able to

  • 0

The below code is used to create a line graph. I am able to display the integers in the TextArea but i am not able to display the sentences above the integers in the text file. How do i rectify this program?

The screenshot of the text file is given below.

I want the first three lines of the text file also to be printed in the TextArea.

enter image description here

import java.awt.BorderLayout;   
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;

public class Pio {
  static JFrame frame1 = new JFrame("Graph");
      static String URL = null;
      static JTextArea txt = new JTextArea();
      static JPanel panel = new JPanel();
 public static void main(String[] args) throws IOException {

   JButton but = new JButton("Open file");

    ![enter image description here][2]

    panel.add(but);    


    frame1.add(panel, BorderLayout.WEST);

   frame1.setVisible(true);

frame1.setSize(1000,400);

    but.addActionListener(new ActionListener()

                        {


                         public void actionPerformed(ActionEvent e) 

                         {



                         JFileChooser chooser = new JFileChooser();

                         int ret = chooser.showDialog(null, "Open file");

                         if (ret == JFileChooser.APPROVE_OPTION)

                           {

                             File file = chooser.getSelectedFile();

                             URL = file.getAbsolutePath();

                           }

                         File f = new File(URL);

                            FileReader inputF = null;

                        try {

                    inputF = new FileReader(f);

                           }
                            catch (FileNotFoundException e1) {
                                                                e1.printStackTrace();

                                                 }

                            BufferedReader in = new BufferedReader(inputF);

                             int[] a = new int[100];

                                       int[] b = new int[100];

                                        String s=null;
                    try {

                                       s = in.readLine();

                                    } 

                                 catch (IOException e1) {

                                     e1.printStackTrace();

                                    }

                               int i = 0;

                             while (s != null && i < 100)

                                            { // your arrays can only hold 1000 ints

                                        String pair[] = s.split(" ");

                                        a[i] = Integer.parseInt(pair[0]);

                                        b[i] = Integer.parseInt(pair[1]);

                                            i++; // don't forget to increment

                                            try {

                                                s = in.readLine();

                                            }

                                          catch (IOException e1) {
                                                                                  e1.printStackTrace();
                                    }
                                }

                            try {

                                in.close();

                                    } 

                                      catch (IOException e1) {
                                                                           e1.printStackTrace();

                                }

                        System.out.println("the output of the file is " + f);

                            XYSeries data = new XYSeries("Line Graph");

                            int n = a.length;

                            for (int j = 0; j < n; j++) {

                                    data.add(a[j], b[j]);

                            }

                            XYDataset xY = new XYSeriesCollection(data);

                                 JFreeChart chart=ChartFactory.createXYLineChart(

                                      "line graph",

                                            "Cycles",

                                           "Temperature",

                                                xY,

                                           PlotOrientation.VERTICAL,

                                           true,

                                          true,

                                           true);

                               ChartPanel p=new ChartPanel(chart);

                                p.setVisible(true);

                                p.setLocation(100,100);

                                p.setSize(500,500);

                              frame1.add(p,BorderLayout.EAST);

                                         }
                        });
            }

}
  • 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-05T12:14:31+00:00Added an answer on June 5, 2026 at 12:14 pm

    Two display two componenets you can use a JSplitPane.

    While loading data into your array append it to a JTextArea (area1 in the sample code)

    final JTextArea area1 = new JTextArea();
    ...
    area1.append("X=" + a[i] + " Y=" + b[i] + "\n");
    

    Then rather than adding the chart to the frame add both the Chart and the TextArea to the JSplitPane

    ChartPanel p = new ChartPanel(chart);
    JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    frame1.add( splitpane , BorderLayout.CENTER );
    splitpane.add(p);
    splitpane.add(area1);
    pane.validate();
    frame1.validate();
    frame1.repaint();  
    

    JSplitPane in use

    To append data the the JTextArea use

      area1.append("Creating a line graph\n");
      area1.append("Selected file " + f + "\n");
      while (s != null && i < 100) { // your arrays can only hold 1000 ints
        String pair[] = s.split(" ");
        try{
          a[i] = Integer.parseInt(pair[0]);
          b[i] = Integer.parseInt(pair[1]);
          area1.append("X=" + a[i] + " Y=" + b[i] + "\n");
        } catch (NumberFormatException e) {
           area1.append(s + "\n");
        }
        try {
          s = in.readLine();
          i++; // don't forget to increment
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
      area1.append("Finished reading data\n");
      ...
    

    In this example I’m assuming that the headder will fail parseInt and (if is does) appending the String s.

    JTextArea

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

Sidebar

Related Questions

// .net 2.0 and vs2005 used. I find some code below. I am not
I have used below code to attach quckboook events // Subscribe to UI events...
Code below is used to save PostgreSql database backup from browser in Apache Mono
I had used the below code to get the row index of the UIPickerView
My below code works fine and is used to populate a <select> item with
I have used the code below to read rfid tag values. try { if
I am having a very specific problem in JQuery The code below is used
I have some problem with my cfml website. I have used the below code
I want to load wordpress in a smarty template. I used the code below
Technology Used:- Asp.Net 2.0 Code:- See Below Description:- hello code given below is working

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.