2

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);

                                         }
                        });
            }

}
User689
  • 101
  • 3
  • 8
  • 1
    Is it really necessary to include all that file loading/parsing code in your question ? You basically want to combine a `JFreeChart` (or a `ChartPanel`) with a `JTextArea`, and you post a lot of code which has nothing to do with that question – Robin Jun 11 '12 at 10:30
  • @Robin I thought it would be easy to understand. I want to place the frame to place all the components on it. – User689 Jun 11 '12 at 10:42
  • 1
    *"thought it would be easy to understand"* 2 x ASCII art (or simple drawing) of the GUI is easy to understand. The 1st should show the GUI at minimum size, the 2nd should show it with extra width & height - to illustrate where extra space is assigned. – Andrew Thompson Jun 11 '12 at 11:19

2 Answers2

4
  1. there nothing about JTextArea, use constructor JTextArea(int rows, int columns)

  2. put JTextArea to the JScrollPane, this JScrollPane put or the EAST or WEST area

  3. put JPanel with JButton to the SOUTH or NORTH area

  4. if ChartPanel doesn't returns any PrefferedSize (I doubt that), or this value isn't correct, then set for own PrefferedSize, put ChartPanel to the CENTER area

  5. as you saw there any code line about setSize, JFrame has implemented BorderLayout in the API

  6. use pack() instead of setSize()

  7. setLocation() if required

  8. last code lines in the ActionListener (your code logics) must be

    frame.validate(); frame.repaint(); frame.pack();

  9. in this form (your code logics) is Swing GUI during FileIO unresponsible for Mouse and Key Events, wrap FileIO to the Runnable#Tread or use SwingWorker for redirecting this long and hard task to the background task

  10. create ChartPanel as local variable (as JFrame) and first code line in the ActionListener must be

.

frame.remove(ChartPanel);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
3

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

GrahamA
  • 5,875
  • 29
  • 39
  • How do I also print some lines of text in the TextArea before the **X** and **Y** values? – User689 Jun 11 '12 at 13:05
  • @user1448671: use `append()`. – trashgod Jun 11 '12 at 13:53
  • @trashgod The Above screenshot is exactly how it looks. But along with **X** and **Y** values. I want to print a few sentences from the same text file before the **X** and **Y** values. – User689 Jun 12 '12 at 05:18
  • @user1448671: Sorry if I'm overlooking something, but you can call `append()` repeatedly, as shown [here](http://stackoverflow.com/a/3245805/230513). – trashgod Jun 12 '12 at 06:20
  • @GrahamA I want the sentences before the integer columns in the text file to be printed in the TextArea. How do I do that? – User689 Jun 12 '12 at 09:14
  • @user1448671 if you could mock up the layout you would like and add it to your origional question I'll try to ammend my example for you – GrahamA Jun 12 '12 at 09:29
  • @GrahamA I want in the the above format. I want a few lines to be printed in the TextArea before the integers get printed. I have to specify what has to be printed while using append. – User689 Jun 12 '12 at 11:26
  • @user1448671 did you try the code I have included in the above example? I'm using a 'try ... catch' to read a headder from the data file – GrahamA Jun 12 '12 at 11:35