8 Replies - 342 Views - Last Post: 15 October 2012 - 09:32 AM Rate Topic: -----

#1 Gamme40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-October 11

File To Arrays in GUI

Posted 14 October 2012 - 08:43 AM

Hello,

I am a beginner programmer and am having problems with pulling information from a text file to an array with GUI.

The error i encounter is that when i have to use the BufferedReader...FileReader, i would have to use the throws IOException extension onto a class, which then informs me that i have to use a try catch statements which i have absolutely no knowledge of!

I have tried to make method and driver classes but that totally confuses me.

Please assist me!

Is This A Good Question/Topic? 0
  • +

Replies To: File To Arrays in GUI

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: File To Arrays in GUI

Posted 14 October 2012 - 08:56 AM

Here you can read about the try/catch blocks.
Basically what you need is something like this:
try(BufferedReader br = new BufferedReader(new FileReader(path))
{
   //Read in the file, and do stuff here.
}
catch(IOException ioe)
{
   //tell the program what to do if something
   //goes wrong when you read from file.
}

Was This Post Helpful? 1
  • +
  • -

#3 Gamme40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-October 11

Re: File To Arrays in GUI

Posted 14 October 2012 - 09:23 AM

Thanks for the quick response! Problem i have now is that as soon as i push the button, the program immediately gets "stuck".

Here is what i have thusfar:

class ButtonHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            try
            {
                String name[] = new String [13];
                double price[] = new double [13];
                BufferedReader infil = new BufferedReader (new FileReader ("KFCMenu.txt"));
                String line = infil.readLine ();
                while (line != null)
                {
                    for (int x = 0 ; x < 13 ; x++)
                    {
                        String field[] = line.split ("#");
                        name [x] = field [0];
                        price [x] = Double.parseDouble (field [1]);
                    }
                    infil.readLine ();
                }
                for (int x = 0 ; x < 13 ; x++)
                {
                    System.out.println (name [x] + "\tR" + price [x]);
                }
                infil.close ();

            }
            catch (IOException ioe)
            {
                System.out.println ("Program Has encountered error");
            }
        }
    }


The purpose of having the code in the actionListener is so that as soon as the button is pushed, labels showing name and price of the textfile information is supposed to be output.
Was This Post Helpful? 0
  • +
  • -

#4 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: File To Arrays in GUI

Posted 14 October 2012 - 10:56 AM

You need to update the "line" variable, you set it at the beginning, but not inside the loop, so you never escape the while-loop. At line 19, change to:
line = infil.readLine ();

Was This Post Helpful? 1
  • +
  • -

#5 Gamme40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-October 11

Re: File To Arrays in GUI

Posted 14 October 2012 - 12:32 PM

Thanks alot this helped me with the button error!! i will definitely return if theres a new problem that arises!

This post has been edited by Gamme40: 14 October 2012 - 12:34 PM

Was This Post Helpful? 0
  • +
  • -

#6 Gamme40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-October 11

Re: File To Arrays in GUI

Posted 14 October 2012 - 12:49 PM

with one fix, creates 10 more problems to tackle!

I have a new problem that arises! Ok the file contents get read into the array perfectly as i tested this with the common "System.out.println" but now i would like to rather have them output via Labels! This is proving to be difficult as my arrays etc are all located in the actionListener place.

How do i go about displaying the labels under the Second Tabbed Pane?? here is what i have so far:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class GUI_PHASE3_1 extends JFrame
{
    public GUI_PHASE3_1 ()
    {
        //This will create the title you see in the upper left of the window
        setTitle ("Courier-IT Like a Boss");
        setSize (300, 550); //set size so the user can "see" it
        setResizable (false);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        //Here we are creating the object
        JTabbedPane jtp = new JTabbedPane ();

        //This creates the template on the windowed application that we will be using
        getContentPane ().add (jtp);

        JPanel jp1 = new JPanel (); //This will create the first tab
        JPanel jp2 = new JPanel (); //This will create the second tab
        JPanel jp3 = new JPanel ();
        JPanel jp4 = new JPanel ();

        //This creates a non-editable label, sets what the label will read
        //and adds the label to the first tab

        JLabel label1 = new JLabel ("Name of Client:");
        JTextField text1 = new JTextField (20);

        JLabel label2 = new JLabel ("Delivery Address Details");

        JLabel label3 = new JLabel ("Street Address:");
        JTextField text3 = new JTextField (20);

        JLabel label4 = new JLabel ("Town/Suburb:");
        JTextField text4 = new JTextField (20);

        JLabel label5 = new JLabel ("Date of Delivery:");
        JTextField text5 = new JTextField (20);

        JLabel space1 = new JLabel ("______________________________");

        JLabel label6 = new JLabel ("Contact Details of Client");
        JLabel label7 = new JLabel ("Contact Number 1:");
        JLabel label8 = new JLabel ("Contact Number 2:");
        JLabel label9 = new JLabel ("Additional Number:");
        JLabel label10 = new JLabel ("Comments:");
        JTextField text7 = new JTextField (20);
        JTextField text8 = new JTextField ("Optional", 20);
        JTextField text9 = new JTextField ("Optional", 20);
        JTextArea text10 = new JTextArea ("Optional", 2, 20);
        JLabel space2 = new JLabel ("______________________________");


        jp1.add (label1);
        jp1.add (text1);

        jp1.add (label2);
        jp1.add (space1);

        jp1.add (label3);
        jp1.add (text3);

        jp1.add (label4);
        jp1.add (text4);

        jp1.add (label5);
        jp1.add (text5);

        jp1.add (label6);
        jp1.add (space2);

        jp1.add (label7);
        jp1.add (text7);

        jp1.add (label8);
        jp1.add (text8);

        jp1.add (label9);
        jp1.add (text9);

        jp1.add (label10);
        jp1.add (text10);

        //This adds the first and second tab to our tabbed pane object and names it
        jtp.addTab ("KFC", jp1);
        jtp.addTab ("Menu", jp2);
        jtp.addTab ("Order Details", jp3);
        jtp.addTab ("Confirm", jp4);

        //This creates a new button called "Bring up Menu" and adds it to the second tab
        JButton menuCall = new JButton ("Bring Up Menu");
        jp2.add (menuCall);

        JLabel od = new JLabel ("Order Details:");
        JLabel space3 = new JLabel ("______________________________");
        JLabel nameL = new JLabel (text1.getText ());

        jp3.add (od);
        jp3.add (space3);
        jp3.add (nameL);

        //This is an Action Listener which reacts to clicking on
        //the test button called "Press"
        ButtonHandler phandler = new ButtonHandler ();
        menuCall.addActionListener (phandler);
        setVisible (true); //otherwise you won't "see" it
    }


    //This is the internal class that defines what the above Action Listener
    //will do when the test button is pressed.
    class ButtonHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            try
            {
                String[] name = new String [13];
                double[] price = new double [13];
                int size = 0;

                BufferedReader infil = new BufferedReader (new FileReader ("KFCMenu.txt"));
                String line = infil.readLine ();
                while (line != null)
                {
                    String field[] = line.split ("#");
                    name [size] = field [0];
                    price [size] = Double.parseDouble (field [1]);


                    size++;
                    line = infil.readLine ();
                }
                for (int x = 0 ; x < 13 ; x++)
                {

                }
                infil.close ();

            }
            catch (IOException ioe)
            {

            }
        }
    }


    //example usage
    public static void main (String[] args)
    {
        GUI_PHASE3_1 tab = new GUI_PHASE3_1 ();
    }
}


Hope you can help me out! :cry2:
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: File To Arrays in GUI

Posted 14 October 2012 - 01:31 PM

Declare the array(s) to give them the visibility you need. If they need to be used by (visible to) an instance of the class GUI_PHASE3_1, then make them instance variables:
public class GUI_PHASE3_1 extends JFrame
{
    String[] name;
    double[] price;

    public GUI_PHASE3_1 ()
    {

// . . . the rest

}

and then initialize the instance variables in the constructor or later, as appropriate.
Was This Post Helpful? 0
  • +
  • -

#8 Gamme40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-October 11

Re: File To Arrays in GUI

Posted 15 October 2012 - 06:31 AM

Hey!

Thanks for the response! i have placed the variable arrays outside the gui class but and have tried this to output the labels, the code compiles but it throws a null pointer exception!

Also if i try to access the "price[x]" array ANYWHERE it throws me an error saying that no applicable overload was found for the contructor of type ... why does it do this for the price array and not the name array?!
for (int x = 0; x< 13; x++)
        {
            JLabel oLabel = new JLabel (name [x]);
            JLabel oLabel2 = new JLabel (price [x]);
            jp3.add (oLabel);
        }


Thanks in advance for all the help, you've been great!

This post has been edited by Gamme40: 15 October 2012 - 06:32 AM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: File To Arrays in GUI

Posted 15 October 2012 - 09:32 AM

Never a good idea to add JComponent to a Component on the fly
Fill your JPanel with empty JLabel
then instead of creating JLabel on the fly as you do, simply do a setText() on these labels based on what you just read
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1