Results 1 to 9 of 9
  1. #1
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Transferring Output to GUI

    I have been working through a problem, and I have working what I need to work. However, it prints out to the command line and I want to output to a GUI. I have set up the GUI and everything seems fine there, the problem is when I try to change the output that was coming through the command prompt to a JTextField. I am getting the following error.

    The field DataAnalyzerGUI.dataOutput is not visible


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class DataAnalyzerGUI extends JFrame
    {
      private JPanel contentPanel;
      private JButton btnExit;
      private JButton btnClose;
      private JButton btnFBdata;
      private JButton btnYahoo;
      private JButton btnCisco;
      private JLabel lblWelcome;
      private JLabel lblExplain;
      private JTextField dataOutput;
      private JButton highClose;
      private JButton lowClose;
      private JButton highOpen;
      private JButton lowOpen;
      private JButton highest;
      private JButton lowest;
      private JButton difference;
      public int fileType;
      
      private void add(Container con, Component widget, int left, int top, int width, int height)
      {
        widget.setBounds(left, top, width, height);
        con.add(widget);
      }
      
      public void GUIsetup()
      {
        contentPanel = (JPanel)getContentPane();
        contentPanel.setLayout(null);
        setTitle("NASDAQ Data Collection");
        setSize(1200, 600);
        setLocation(new Point (1, 1));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        
        lblWelcome = new JLabel("Welcome to the NASDAQ Data Reporting Centre");
        add(contentPanel, lblWelcome, 60, 10, 1100, 60);
        lblWelcome.setFont(new Font("Serif", Font.BOLD, 48));
        
        lblExplain = new JLabel("Please select a company to view reports");
        add(contentPanel, lblExplain, 60, 100, 300, 20);
        lblExplain.setFont(new Font("Serif", Font.ITALIC, 16));
        
        btnFBdata = new JButton("FaceBook");
        add(contentPanel, btnFBdata, 150, 150, 150, 30);
        
        btnYahoo = new JButton("Yahoo");
        add(contentPanel, btnYahoo, 150, 300, 150, 30);
        
        btnCisco = new JButton("Cisco");
        add(contentPanel, btnCisco, 150, 450, 150, 30);
        
        dataOutput = new JTextField();
        add(contentPanel, dataOutput, 650, 100, 500, 400);
        
        btnExit = new JButton("Exit");
        add(contentPanel, btnExit, 10, 500, 100, 30);
        
        lowClose = new JButton ("Lowest Closing Value");
        add(contentPanel, lowClose, 300, 150, 200, 30);
        lowClose.setVisible(false);
        
        lowOpen = new JButton ("Lowest Opening Value");
        add(contentPanel, lowOpen, 300, 200, 200, 30);
        lowOpen.setVisible(false);
        
        highClose = new JButton ("Highest Closing Value");
        add(contentPanel, highClose, 300, 250, 200, 30);
        highClose.setVisible(false);
        
        highOpen = new JButton ("Highest Opening Value");
        add(contentPanel, highOpen, 300, 300, 200, 30);
        highOpen.setVisible(false);
        
        highest = new JButton ("Ten Highest Opening Values");
        add(contentPanel, highest, 300, 350, 200, 30);
        highest.setVisible(false);
        
        lowest = new JButton ("Ten Lowest Opening Values");
        add(contentPanel, lowest, 300, 400, 200, 30);
        lowest.setVisible(false);
        
        difference = new JButton ("Difference from Open-Close");
        add(contentPanel, difference, 300, 450, 200, 30);
        difference.setVisible(false);
        
        btnClose = new JButton ("Close File");
        add(contentPanel, btnClose, 300, 500, 200, 30);
        btnClose.setVisible(false);
        
        btnFBdata.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            fileType = 1;
            btnFBdata.setVisible(false);
            btnYahoo.setVisible(false);
            btnCisco.setVisible(false);
            
            lowClose.setVisible(true);
            lowOpen.setVisible(true);
            highOpen.setVisible(true);
            highClose.setVisible(true);
            highest.setVisible(true);
            lowest.setVisible(true);
            difference.setVisible(true);
            btnClose.setVisible(true);
          }
        });
        
        btnYahoo.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            fileType = 2;
            btnFBdata.setVisible(false);
            btnYahoo.setVisible(false);
            btnCisco.setVisible(false);
            
            lowClose.setVisible(true);
            lowOpen.setVisible(true);
            highOpen.setVisible(true);
            highClose.setVisible(true);
            highest.setVisible(true);
            lowest.setVisible(true);
            difference.setVisible(true);
            btnClose.setVisible(true);
          }
        });
        
        btnCisco.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            fileType = 3;
            btnFBdata.setVisible(false);
            btnYahoo.setVisible(false);
            btnCisco.setVisible(false);
            
            lowClose.setVisible(true);
            lowOpen.setVisible(true);
            highOpen.setVisible(true);
            highClose.setVisible(true);
            highest.setVisible(true);
            lowest.setVisible(true);
            difference.setVisible(true);
            btnClose.setVisible(true);
          }
        });    
            
        btnClose.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            btnFBdata.setVisible(true);
            btnYahoo.setVisible(true);
            btnCisco.setVisible(true);
            
            lowClose.setVisible(false);
            lowOpen.setVisible(false);
            highOpen.setVisible(false);
            highClose.setVisible(false);
            highest.setVisible(false);
            lowest.setVisible(false);
            difference.setVisible(false);
            btnClose.setVisible(false);
          }
        });
        
        lowClose.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
        
        lowOpen.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
        
        highOpen.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
        
        highClose.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });  
        
        highest.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
        
        lowest.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });    
        
        difference.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            //System.exit(0);
          }
        });
      
        btnExit.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
      }
      
      public static void main (String[] args)
      {
        DataAnalyzerGUI aAnalyzer = new DataAnalyzerGUI();
        aAnalyzer.GUIsetup();
        
        //This creates an instance of the Calculations object
        Calculations aCalculations = new Calculations();
        
        //This calls the method fileCheck in the ReadFiles Class
        aCalculations.fileCheck();
        
        //This calls the method findRows in the ReadFiles Class
        aCalculations.findRows();
        
        //This calls the method printArray in the ReadFiles Class
        aCalculations.convertFile();
        
        //This calls the method convertFile in the ReadFiles Class through the Calculations Class
        //aCalculations.printArray();
        
        //This calls the method createDateArray in the Calculations Class    
        aCalculations.createDateArray();
        
        //This calls the method createDataArray in the Calculations Class
        aCalculations.createDataArray();
        
        //This calls the method printArray in the Calculations Class
        //aCalculations.printNewArray();
        
        //This calls the method performCalculations in the Calculations Class
        //aCalculations.performCalculations();
      }
    }
    Java Code:
    import java.nio.file.*;
    import static java.nio.file.AccessMode.*;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.StringTokenizer;
    
    
    public class ReadFiles extends DataAnalyzerGUI
    {
      public static int numberOfRows;
      public static int rowNumber = 0;
      public static int columnNumber = 0;
      public static int newRowNumber = 0;
      public static int newColumnNumber = 0;
      public String [][] myFBfile = new String [431][7];
    
      //This method attempts to read a file to ensure that it is available to be used by the rest of the program.
      public void fileCheck()
      {
        if (fileType == 1)
        {
          Path filePath = Paths.get("C:\\Brad\\Downloads\\FB.csv");
          //System.out.println("Path is " + filePath.toString());
          dataOutput.setText("Your file is being accessed. Please wait.");
          try
          {
            filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
            System.out.println("File can be read and executed");
          }
        
          catch (IOException e)
          {
            System.out.println ("File cannot be used for this application");
          }
        }
        else
            if(fileType == 2)
        {
          Path filePath = Paths.get("C:\\Brad\\Downloads\\Yahoo.csv");
          System.out.println("Path is " + filePath.toString());
        
          try
          {
            filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
            System.out.println("File can be read and executed");
          }
        
          catch (IOException e)
          {
            System.out.println ("File cannot be used for this application");
          }
        }
        else
          if (fileType == 3)
        {
          Path filePath = Paths.get("C:\\Brad\\Downloads\\CISCO.csv");
          System.out.println("Path is " + filePath.toString());
        
          try
          {
            filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
            System.out.println("File can be read and executed");
          }
        
          catch (IOException e)
          {
            System.out.println ("File cannot be used for this application");
          }
        }
        
      }
      //This method reads a file, then tokenizes it to find the number of rows in the file.
      public void findRows()
      {
        String filePath = ("C:\\Brad\\Downloads\\FB.csv");
        
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(filePath));
          StringTokenizer st = null;
          System.out.println("Your file is being analysed. This may take several minutes.");
    
          while((filePath = br.readLine()) != null)
          {
            rowNumber++;
                    
            st = new StringTokenizer(filePath, ",");
            columnNumber = 0;
          }
        }
        
        catch (FileNotFoundException e)
        {
          System.out.println ("Number of rows for this file cannot be found");
        } 
        
        catch (IOException e) 
        {
          System.out.println ("Number of rows for this file cannot be found");
        }
            
        System.out.println("Total Rows: " + (rowNumber));
      }
      
      //This method converts the file into an array to be used by the rest of the program.
      public void convertFile()
      {
        String convertFilePath = ("C:\\Brad\\Downloads\\FB.csv");
        //The fields for the file are: Date,Open,High,Low,Close,Volume,Adj Close
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(convertFilePath));
          StringTokenizer st = null;
          System.out.println("Your file is being converted. This may take several minutes.");
    
          while((convertFilePath = br.readLine()) != null)
          {
            st = new StringTokenizer(convertFilePath, ",");
    
            while(st.hasMoreTokens())
            {
              myFBfile[newRowNumber][newColumnNumber] = st.nextToken();
              newColumnNumber++;          
              //this is a test to make sure the array is working properly
              //System.out.println (newRowNumber +" "+ newColumnNumber +" "+ myFBfile[newRowNumber][newColumnNumber]);
            }
            newRowNumber++;
            newColumnNumber = 0;
          }
        }       
        catch (FileNotFoundException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        } 
        catch (IOException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
        
      }
      
      //This method prints out all of the data from the array
      public void printArray()
      {
        for (int x = 0; x <= rowNumber; x++)
        {
          for(int y = 0; y <= 6; y++)
          {
            System.out.print (x +" "+ y +" "+ myFBfile[x][y]);
          }
          System.out.println();
        }
      }
      
      //This method returns the array
      public void returnArray()
      {
        //return myFBfile;
      }
    }

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Transferring Output to GUI

    Hi,

    you cannot access a field inside a class which is private from outside the class.

    You could either make it public (which is often not the preferred design) or you could create a public function which takes care of everything (That way other classes do not depend on your exact implementation. So you could change the control with some other control without causing trouble).

    With kind regards,

    Konrad

  3. #3
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Transferring Output to GUI

    Thanks so much. I love easy solutions.

  4. #4
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Transferring Output to GUI

    As per usual, the solution to one problem leads to another. I am trying to call a method in a separate class using the command button in the program. I have tried moving the code that was calling the method in the main method to the method that controls the command button. Any ideas?

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Transferring Output to GUI

    Can you post your current code?

    Thank you.

    Konrad

  6. #6
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Transferring Output to GUI

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     * DataAnalyzer Class
     * This class instantiates the methods from the ReadFiles Class and Calculations Class.
     * @author Brad Strassburger
     * @version 1 March 31, 2014
     */
    public class DataAnalyzerGUI extends JFrame
    {
      private JPanel contentPanel;
      private JButton btnExit;
      private JButton btnClose;
      private JButton btnFBdata;
      private JButton btnYahoo;
      private JButton btnCisco;
      private JLabel lblWelcome;
      private JLabel lblExplain;
      public JTextField dataOutput;
      private JButton highClose;
      private JButton lowClose;
      private JButton highOpen;
      private JButton lowOpen;
      private JButton highest;
      private JButton lowest;
      private JButton difference;
      public int fileType;
      
      private void add(Container con, Component widget, int left, int top, int width, int height)
      {
        widget.setBounds(left, top, width, height);
        con.add(widget);
      }
      
      public void GUIsetup()
      {
        contentPanel = (JPanel)getContentPane();
        contentPanel.setLayout(null);
        setTitle("NASDAQ Data Collection");
        setSize(1200, 600);
        setLocation(new Point (1, 1));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        
        lblWelcome = new JLabel("Welcome to the NASDAQ Data Reporting Centre");
        add(contentPanel, lblWelcome, 60, 10, 1100, 60);
        lblWelcome.setFont(new Font("Serif", Font.BOLD, 48));
        
        lblExplain = new JLabel("Please select a company to view reports");
        add(contentPanel, lblExplain, 60, 100, 300, 20);
        lblExplain.setFont(new Font("Serif", Font.ITALIC, 16));
        
        btnFBdata = new JButton("FaceBook");
        add(contentPanel, btnFBdata, 150, 150, 150, 30);
        
        btnYahoo = new JButton("Yahoo");
        add(contentPanel, btnYahoo, 150, 300, 150, 30);
        
        btnCisco = new JButton("Cisco");
        add(contentPanel, btnCisco, 150, 450, 150, 30);
        
        dataOutput = new JTextField();
        add(contentPanel, dataOutput, 650, 100, 500, 400);
        
        btnExit = new JButton("Exit");
        add(contentPanel, btnExit, 10, 500, 100, 30);
        
        lowClose = new JButton ("Lowest Closing Value");
        add(contentPanel, lowClose, 300, 150, 200, 30);
        lowClose.setVisible(false);
        
        lowOpen = new JButton ("Lowest Opening Value");
        add(contentPanel, lowOpen, 300, 200, 200, 30);
        lowOpen.setVisible(false);
        
        highClose = new JButton ("Highest Closing Value");
        add(contentPanel, highClose, 300, 250, 200, 30);
        highClose.setVisible(false);
        
        highOpen = new JButton ("Highest Opening Value");
        add(contentPanel, highOpen, 300, 300, 200, 30);
        highOpen.setVisible(false);
        
        highest = new JButton ("Ten Highest Opening Values");
        add(contentPanel, highest, 300, 350, 200, 30);
        highest.setVisible(false);
        
        lowest = new JButton ("Ten Lowest Opening Values");
        add(contentPanel, lowest, 300, 400, 200, 30);
        lowest.setVisible(false);
        
        difference = new JButton ("Difference from Open-Close");
        add(contentPanel, difference, 300, 450, 200, 30);
        difference.setVisible(false);
        
        btnClose = new JButton ("Close File");
        add(contentPanel, btnClose, 300, 500, 200, 30);
        btnClose.setVisible(false);
        
        btnFBdata.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            fileType = 1;
            btnFBdata.setVisible(false);
            btnYahoo.setVisible(false);
            btnCisco.setVisible(false);
            
            lowClose.setVisible(true);
            lowOpen.setVisible(true);
            highOpen.setVisible(true);
            highClose.setVisible(true);
            highest.setVisible(true);
            lowest.setVisible(true);
            difference.setVisible(true);
            btnClose.setVisible(true);
            aCalculations.fileCheck();
          }
        });
        
        btnYahoo.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            fileType = 2;
            btnFBdata.setVisible(false);
            btnYahoo.setVisible(false);
            btnCisco.setVisible(false);
            
            lowClose.setVisible(true);
            lowOpen.setVisible(true);
            highOpen.setVisible(true);
            highClose.setVisible(true);
            highest.setVisible(true);
            lowest.setVisible(true);
            difference.setVisible(true);
            btnClose.setVisible(true);
          }
        });
        
        btnCisco.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            fileType = 3;
            btnFBdata.setVisible(false);
            btnYahoo.setVisible(false);
            btnCisco.setVisible(false);
            
            lowClose.setVisible(true);
            lowOpen.setVisible(true);
            highOpen.setVisible(true);
            highClose.setVisible(true);
            highest.setVisible(true);
            lowest.setVisible(true);
            difference.setVisible(true);
            btnClose.setVisible(true);
          }
        });    
            
        btnClose.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            btnFBdata.setVisible(true);
            btnYahoo.setVisible(true);
            btnCisco.setVisible(true);
            
            lowClose.setVisible(false);
            lowOpen.setVisible(false);
            highOpen.setVisible(false);
            highClose.setVisible(false);
            highest.setVisible(false);
            lowest.setVisible(false);
            difference.setVisible(false);
            btnClose.setVisible(false);
          }
        });
        
        lowClose.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
        
        lowOpen.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
        
        highOpen.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
        
        highClose.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });  
        
        highest.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
        
        lowest.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });    
        
        difference.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            //System.exit(0);
          }
        });
      
        btnExit.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            System.exit(0);
          }
        });
      }
      
      public static void main (String[] args)
      {
        DataAnalyzerGUI aAnalyzer = new DataAnalyzerGUI();
        aAnalyzer.GUIsetup();
        
        //This creates an instance of the Calculations object
        Calculations aCalculations = new Calculations();
        
        //This calls the method fileCheck in the ReadFiles Class
        //aCalculations.fileCheck();
        
        //This calls the method findRows in the ReadFiles Class
        //aCalculations.findRows();
        
        //This calls the method printArray in the ReadFiles Class
        //aCalculations.convertFile();
        
        //This calls the method convertFile in the ReadFiles Class through the Calculations Class
        //aCalculations.printArray();
        
        //This calls the method createDateArray in the Calculations Class    
        //aCalculations.createDateArray();
        
        //This calls the method createDataArray in the Calculations Class
        //aCalculations.createDataArray();
        
        //This calls the method printArray in the Calculations Class
        //aCalculations.printNewArray();
        
        //This calls the method performCalculations in the Calculations Class
        //aCalculations.performCalculations();
      }
    }
    Java Code:
    import java.nio.file.*;
    import static java.nio.file.AccessMode.*;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.StringTokenizer;
    
    /**
     * ReadFiles Class
     * This class executes all of the methods called bt DataAnalyzer.
     * @author Brad Strassburger
     * @version 1 March 31, 2014
     */
    
    public class ReadFiles extends DataAnalyzerGUI
    {
      public static int numberOfRows;
      public static int rowNumber = 0;
      public static int columnNumber = 0;
      public static int newRowNumber = 0;
      public static int newColumnNumber = 0;
      public String [][] myFBfile = new String [431][7];
    
      //This method attempts to read a file to ensure that it is available to be used by the rest of the program.
      public void fileCheck()
      {
        if (fileType == 1)
        {
          Path filePath = Paths.get("C:\\Brad\\Downloads\\FB.csv");
          //System.out.println("Path is " + filePath.toString());
          dataOutput.setText("Your file is being accessed. Please wait.");
          try
          {
            filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
            System.out.println("File can be read and executed");
          }
        
          catch (IOException e)
          {
            System.out.println ("File cannot be used for this application");
          }
        }
        else
            if(fileType == 2)
        {
          Path filePath = Paths.get("C:\\Brad\\Downloads\\Yahoo.csv");
          System.out.println("Path is " + filePath.toString());
        
          try
          {
            filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
            System.out.println("File can be read and executed");
          }
        
          catch (IOException e)
          {
            System.out.println ("File cannot be used for this application");
          }
        }
        else
          if (fileType == 3)
        {
          Path filePath = Paths.get("C:\\Brad\\Downloads\\CISCO.csv");
          System.out.println("Path is " + filePath.toString());
        
          try
          {
            filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
            System.out.println("File can be read and executed");
          }
        
          catch (IOException e)
          {
            System.out.println ("File cannot be used for this application");
          }
        }
        
      }
      //This method reads a file, then tokenizes it to find the number of rows in the file.
      public void findRows()
      {
        String filePath = ("C:\\Brad\\Downloads\\FB.csv");
        
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(filePath));
          StringTokenizer st = null;
          System.out.println("Your file is being analysed. This may take several minutes.");
    
          while((filePath = br.readLine()) != null)
          {
            rowNumber++;
                    
            st = new StringTokenizer(filePath, ",");
            columnNumber = 0;
          }
        }
        
        catch (FileNotFoundException e)
        {
          System.out.println ("Number of rows for this file cannot be found");
        } 
        
        catch (IOException e) 
        {
          System.out.println ("Number of rows for this file cannot be found");
        }
            
        System.out.println("Total Rows: " + (rowNumber));
      }
      
      //This method converts the file into an array to be used by the rest of the program.
      public void convertFile()
      {
        String convertFilePath = ("C:\\Brad\\Downloads\\FB.csv");
        //The fields for the file are: Date,Open,High,Low,Close,Volume,Adj Close
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(convertFilePath));
          StringTokenizer st = null;
          System.out.println("Your file is being converted. This may take several minutes.");
    
          while((convertFilePath = br.readLine()) != null)
          {
            st = new StringTokenizer(convertFilePath, ",");
    
            while(st.hasMoreTokens())
            {
              myFBfile[newRowNumber][newColumnNumber] = st.nextToken();
              newColumnNumber++;          
              //this is a test to make sure the array is working properly
              //System.out.println (newRowNumber +" "+ newColumnNumber +" "+ myFBfile[newRowNumber][newColumnNumber]);
            }
            newRowNumber++;
            newColumnNumber = 0;
          }
        }       
        catch (FileNotFoundException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        } 
        catch (IOException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
        
      }
      
      //This method prints out all of the data from the array
      public void printArray()
      {
        for (int x = 0; x <= rowNumber; x++)
        {
          for(int y = 0; y <= 6; y++)
          {
            System.out.print (x +" "+ y +" "+ myFBfile[x][y]);
          }
          System.out.println();
        }
      }
      
      //This method returns the array
      public void returnArray()
      {
        //return myFBfile;
      }
    }

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Transferring Output to GUI

    Hi,

    The class Calculations is missing. That is a reason why I cannot compile your code.

    One error that I see inside your code is:
    You create a local variable aCalculations inside your static main function. This variable is only available in there.
    So it is not available inside your public void actionPerformed(ActionEvent e)!

    And I just want to mention, that your design seems a little strange. It is a common pattern to divide between business data classes and GUI classes. Your ReadFiles class derives from your GUI class which shouldn't be required.
    So instead of that I would suggest to do something of this:
    a) You could simply create inside the GUI an instance of ReadFiles. Your GUI could implement some interface that you introduce (e.g. with function addOutput) and ReadFiles gets a setter/getter for such an instance e.g. getOutput/setOutput). So your code dataOutput.setText could be changed to output.addOuput(...).
    b) You could use a swing listener so your ReadFile could provide events and the GUI could simply receive them.
    c) The Observer/Obvervable Pattern could be used.

    All this has the advantage that your business logic does not depend on your GUI (and also vice versa) which has multiple advantages:
    - the business logic classes might be reused by other classes.
    - business logic / GUI can be changed without taking care about the other stuff. (So the example could be that you replace the JTextBox with some other control or other changes.)

    With kind regards,

    Konrad

  8. #8
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Transferring Output to GUI

    Konrad
    I can see what you are trying to say, but I cannot translate that into something that actually works. Is there any chance that you could provide a very brief example to show what you are describing?

  9. #9
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Transferring Output to GUI

    I just made a quick example with an interface which is quite simple to understand. The objects I am using are quite dumb:
    - I just have a JFrame that implements my OutputReceiver interface (maybe name should have been OutputReceivable so it ends with able? I am still mainly working inside the .Net world with other naming standards.) It does not contain anything and when it receives some output, it simply sets the Title.
    - My BusinessLogic is simply a Runnable that is counting and sends some output the a OutputReceiver which must be passed with the constructor.
    - main can be found inside my SimpleFrame. The frame is created and shown and then the BusinessLogic is created with this SimpleFrame instance. Then a thread is created so the counting starts. Then we wait for that thread to end and dispose the window.

    Interface:
    Java Code:
    public interface OutputReceiver {
    		public void receiveOutput(String data);
    }
    SimpleFrame:
    Java Code:
    import javax.swing.JFrame;
    
    
    public class SimpleFrame extends JFrame implements OutputReceiver {
    
    	/**
    	 * serialVersionUID because this class is serializable.
    	 */
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * Main method that simply creates a new Frame which should display some output.
    	 * @param args Arguments are not used.
    	 */
    	public static void main(String[] args) {
    		// First we create a new instance of our Frame.
    		JFrame window = new SimpleFrame();
    		window.setSize(300,100);
    		window.setVisible(true);
    		
    		// Now we generate some BusinessLogic element and start the thread
    		BusinessLogic logic = new BusinessLogic((OutputReceiver) window);
    		Thread thread = new Thread(logic);
    		thread.start();
    		try {
    			thread.join();
    		}
    		catch (Exception ex) {
    			System.out.println("An exception occured while waiting for thread: " + ex.getMessage());
    		}
    		window.dispose();
    	}
    
    	/**
    	 * Receives some output to be displayed.
    	 */
    	@Override
    	public void receiveOutput(String data) {
    		// In here you could make with the data whatever you want. We just set the title so the example is really simple!
    		this.setTitle(data);
    	}
    }
    BusinessLogic:
    Java Code:
    /**
     * BusinessLogic
     * 
     * This is a simple class which implements Runnable and simply produces some output.
     */
    public class BusinessLogic implements Runnable {
    
    	private final int MAX_COUNT = 10;
    
    	private OutputReceiver receiver;
    
    	public BusinessLogic(OutputReceiver receiver) {
    		this.receiver = receiver;
    	}
    
    	@Override
    	public void run() {
    		for (int i=1; i <= MAX_COUNT; i++) {
    			sendMessage("Counted to " + i);
    
    			// Wait a second.
    			try {
    				Thread.sleep(1000);
    			}
    			catch (Exception ex) {
    				sendMessage("An Exception occured: " + ex.getMessage());
    			}
    		}
    	}
    	
    	/**
    	 * Send a message to the receiver.
    	 * 
    	 * @param message
    	 * Message to send.
    	 */
    	private void sendMessage(String message) {
    		if (receiver != null) {
    			receiver.receiveOutput(message);
    		}
    	}
    }
    I hope this helps regarding the first point.

    If you want to see an example for the Observer pattern, then I would suggest
    Observer Design Pattern in Java – Example Tutorial

    With kind regards,

    Konrad

Similar Threads

  1. Transferring files to cellphone
    By Toll in forum Advanced Java
    Replies: 15
    Last Post: 10-03-2013, 10:03 PM
  2. Transferring a file using Sockets
    By SerbianSergeant in forum Networking
    Replies: 1
    Last Post: 09-08-2011, 01:16 AM
  3. Netbeans errors when transferring file
    By dbashby in forum New To Java
    Replies: 7
    Last Post: 04-15-2009, 03:41 PM
  4. Transferring a .txt file from 1 location to another
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-15-2009, 05:22 AM
  5. Transferring Over Network
    By qwerty in forum Advanced Java
    Replies: 2
    Last Post: 08-01-2008, 01:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •