Tyler
Tyler

Reputation: 39

How to print the contents of a file to a GUI

For my program one of the major things we have to do is print the contents of a .csv file to a GUI. I have figured out the GUI and sorting methods however we are having trouble figuring out how to actually print the file to the GUI. Any ideas on how to do this? Here is the code for the GUI:

 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.IOException;

 import javax.swing.*;
 public class FirstGUI2 {
 private static JTextArea textAreaMon;
 private static JTextArea textAreaTue;
 private static JTextArea textAreaWed;
 private static JTextArea textAreaThu;
 private static JTextArea textAreaFri;
 private static JTextArea description;
 private static String gE1 = "Group Exam 1";
 private static String gE2 = "Group Exam 2";
 private static String gE3 = "Group Exam 3";
 private static String gE4 = "Group Exam 4";
 private static String gE5 = "Group Exam 5";
 private static String gE6 = "Group Exam 6";
 private static String gE7 = "Group Exam 7";
 private static String gE8 = "Group Exam 8";
 private static String gE9 = "Group Exam 9";
 private Container pane;
 final static boolean fill = true;
 final static boolean weightX = true;
 final static boolean RIGHT_TO_LEFT = false;
 public static String mon = "";
 public static String tues = "";
 public static String wed = "";
 public static String thu = "";
 public static String fri = "";

 public static void addComponents(Container pane) {
    if (RIGHT_TO_LEFT)
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;

    button = new JButton("Open File");
    c.gridx = 1;
    c.gridy = 1;
    c.insets = new Insets(5, 5, 5, 5);
    pane.add(button, c);

    textAreaMon = new JTextArea(mon);
    //textAreaMon.append(mon.substring(0));
    textAreaMon.setLineWrap(true);
    textAreaMon.setEditable(false);
    c.gridx = 1;
    c.gridy = 2;
    pane.add(textAreaMon, c);


    textAreaTue = new JTextArea(tues);
    textAreaTue.setLineWrap(true);
    textAreaTue.setEditable(false);
    c.gridx = 1;
    c.gridy = 3;
    pane.add(textAreaTue, c);

    textAreaWed = new JTextArea(wed);
    textAreaWed.setLineWrap(true);
    textAreaWed.setEditable(false);
    c.gridx = 1;
    c.gridy = 4;
    pane.add(textAreaWed, c);

    textAreaThu = new JTextArea(thu);
    textAreaThu.setLineWrap(true);
    textAreaThu.setEditable(false);
    c.gridx = 1;
    c.gridy = 5;
    pane.add(textAreaThu, c);

    textAreaFri = new JTextArea(fri);
    textAreaFri.setLineWrap(true);
    textAreaFri.setEditable(false);
    c.gridx = 1;
    c.gridy = 6;
    pane.add(textAreaFri, c);

    description = new JTextArea(
            "Click the open button in order to select and import your .csv       file. Your scehdule and times will be displayed in the schedule below.");
    description.setEditable(false);
    c.gridx = 1;
    c.gridy = 0;

    pane.add(description, c);

    // attaching the file opener to the open file button
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            new InterfacePanel();
        }
    });
 }


//creates the frame and showing the GUI to the user
 public static void makeGUI(){
    JFrame frame = new JFrame("Final Exam Scheduler");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addComponents(frame.getContentPane());

    frame.pack();
    frame.setVisible(true);
 }

 public static void printToGUI(){
    //JText Fields

    //textAreaMon
    mon= "Monday: \n \n 8:00 - 10:00:\t Group Exam 1 \n 10:15-12:15:\t Group    Exam 2 \n 12:45 - 2:45:\t";
    while(!ArrayListsForClasses.time1.isEmpty()){
        for(int i = 0; i < ArrayListsForClasses.time1.size(); i++){
            mon += ArrayListsForClasses.time1.get(i);
            mon += ", ";
        }
    }
    mon += "\n 3:00 - 5:00:\t";
    while(!ArrayListsForClasses.time2.isEmpty()){
        for(int i = 0; i < ArrayListsForClasses.time2.size(); i++){
            mon += ArrayListsForClasses.time2.get(i);
            mon += ", ";
        }
    }
    mon += "\n 5:30 - 7:30:\t Group Exam 3 \n \n 7:45 - 9:45:\t";
    while(!ArrayListsForClasses.time3.isEmpty()){
        for(int i = 0; i < ArrayListsForClasses.time3.size(); i++){
            mon += ArrayListsForClasses.time3.get(i);
            mon += ", ";
        }
    }
    mon+= "\n \n";


    // textAreaTues
    tues = "Tuesday: \n \n 8:00 - 10:00: \t";
    while (!ArrayListsForClasses.time4.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time4.size(); i++) {
            tues += ArrayListsForClasses.time4.get(i);
            tues += ", ";
        }
    }
    tues += "\n 10:15 - 12:15:\t Group Exam 4 \n \n 12:45 - 2:45:\t ";
    while (!ArrayListsForClasses.time5.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time5.size(); i++) {
            tues += ArrayListsForClasses.time5.get(i);
            tues += ", ";
        }
    }
    tues += "\n 3:00 - 5:00:\t ";
    while (!ArrayListsForClasses.time6.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time6.size(); i++) {
            tues += ArrayListsForClasses.time6.get(i);
            tues += ", ";
        }
    }
    tues += "\n 5:30 - 7:30:\t ";
    while (!ArrayListsForClasses.time7.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time7.size(); i++) {
            tues += ArrayListsForClasses.time7.get(i);
            tues += ", ";
        }
    }
    tues += "\n 7:45 - 9:45:\t ";
    while (!ArrayListsForClasses.time8.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time8.size(); i++) {
            tues += ArrayListsForClasses.time8.get(i);
            tues += ", ";
        }
    }

    //TextAreaWed
    wed = "Wednesday \n \n 8:00 - 10:00:\t";
    while(!ArrayListsForClasses.time9.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time9.size(); i++) {
            wed += ArrayListsForClasses.time9.get(i);
            wed += ", ";
        }
    }
    wed += "\n 10:15-12:15:\t";
    while(!ArrayListsForClasses.time10.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time10.size(); i++) {
            wed += ArrayListsForClasses.time10.get(i);
            wed += ", ";
        }
    }
    wed += "\n 12:45 - 2:45 :\t Group Exam 5 \n 3:00 - 5:00:\t";
    while(!ArrayListsForClasses.time11.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time11.size(); i++) {
            wed += ArrayListsForClasses.time11.get(i);
            wed += ", ";
        }
    }
    wed += "\n 5:30 - 7:30:\t";
    while(!ArrayListsForClasses.time12.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time12.size(); i++) {
            wed += ArrayListsForClasses.time12.get(i);
            wed += ", ";
        }
    }
    wed += "\n 7:45 - 9:45:\t";
    while(!ArrayListsForClasses.time13.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time13.size(); i++) {
            wed += ArrayListsForClasses.time13.get(i);
            wed += ", ";
        }
    }

    //TextAreaThurs
    thu = "Thursday \n \n 8:00 - 10:00:\t";
    while(!ArrayListsForClasses.time14.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time14.size(); i++) {
            thu += ArrayListsForClasses.time14.get(i);
            thu += ", ";
        }
    }
    thu += "\n 10:15-12:15 :\t Group Exam 6 \n 12:45 - 2:45:\t";
    while(!ArrayListsForClasses.time15.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time15.size(); i++) {
            thu += ArrayListsForClasses.time15.get(i);
            thu += ", ";
        }
    }
    thu += "\n 3:00 - 5:00:\t";
    while(!ArrayListsForClasses.time16.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time16.size(); i++) {
            thu += ArrayListsForClasses.time16.get(i);
            thu += ", ";
        }
    }
    thu += "\n 5:30 - 7:30:\t Group Exam 7 \n 7:45 - 9:45:\t";
    while(!ArrayListsForClasses.time17.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time17.size(); i++) {
            thu += ArrayListsForClasses.time17.get(i);
            thu += ", ";
        }
    }

    //TextAreaFri
    fri = "Friday \n \n 8:00 - 10:00:\t Group Exam 8 \n 10:15-12:15:\t";
    while(!ArrayListsForClasses.time18.isEmpty()) {
        for (int i = 0; i < ArrayListsForClasses.time18.size(); i++) {
            fri += ArrayListsForClasses.time18.get(i);
            fri += ", ";
        }
    }
    fri += "\n 12:45 - 2:45:\t Group Exam 9";

 }

 public static void main(String[] args){
     javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            makeGUI();
        }
     });

  }

}

Upvotes: 1

Views: 1639

Answers (3)

camickr
camickr

Reputation: 324118

I have figured out the GUI

Actaully, the design of your code is all wrong. You should NOT be using static variables and static methods. In general, the only static method should be the method to create the GUI. All other application code should be defined in a class so any method can access the variables of your class.

we have to do is print the contents of a .csv file to a GUI

To me the word "print" means to use a printer to print something to a piece of paper. I'm guessing you really want to display the contents of a file in a GUI.

Below is a simple example that shows how to read and write a file. The contents of the file will be displayed in a text area. Note you need to write to the file first before you can read the file.

I suggest you start with this basic code and rewrite your class so you can get rid of all the static variables and methods:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;

class TextAreaLoad extends JPanel
{
    private JTextArea edit;

    public TextAreaLoad()
    {
        setLayout( new BorderLayout() );

        edit = new JTextArea(30, 60);
        add(new JScrollPane(edit), BorderLayout.NORTH);

        JButton read = new JButton("Read TextAreaLoad.txt");
        read.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileReader reader = new FileReader( "TextAreaLoad.txt" );
                    BufferedReader br = new BufferedReader(reader);
                    edit.read( br, null );
                    br.close();
                    edit.requestFocus();
                }
                catch(Exception e2) { System.out.println(e2); }
            }
        });

        add(read, BorderLayout.LINE_START);

        JButton write = new JButton("Write TextAreaLoad.txt");
        write.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileWriter writer = new FileWriter( "TextAreaLoad.txt" );
                    BufferedWriter bw = new BufferedWriter( writer );
                    edit.write( bw );
                    bw.close();
                    edit.setText("");
                    edit.requestFocus();
                }
                catch(Exception e2) { System.out.println(e2); }
            }
        });

        add(write, BorderLayout.LINE_END);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("TextArea Load");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TextAreaLoad());
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

Upvotes: 1

Anto
Anto

Reputation: 4305

I may be wrong, but inside the actionPerformed() method, you should write the following code

 String myCsvFile = "mySCVFile.csv"; //you can modify this based on where the file is located 
 BufferedReader br = null;  
 String line = "";  

 try {  

   br = new BufferedReader(new FileReader(myCsvFile));  
   while ((line = br.readLine()) != null) {  
     String[] columns = line.split(",");  //you may do extra tweeks in here based on how you want to display the data
     for(int x=0; x <columns.length(); x++){
         description.append(columns[x]);  //I guess this is the textarea you want to display the content of the CSV file

     }

   }  

 } catch (FileNotFoundException e) {  
  e.printStackTrace();  
 } catch (IOException e) {  
  e.printStackTrace();  
 } finally {  
  if (br != null) {  
    try {  
      br.close();  
    } catch (IOException e) {  
      e.printStackTrace();  
    }  
  }  
}  

Upvotes: 0

Jochem Kleine
Jochem Kleine

Reputation: 186

You have yet to set the String value with the imported data to the TextAreas. What you did do, was give the JTextAreas a value equal to the initial value of strings like mon, like so:

textAreaMon = new JTextArea(mon);

However, that does not mean textAreaMon will be updated if mon is updated; it simply matters when the view is initialized.

Add method calls to your file-import button listener that contain methods that do something like this

textAreaMon.setText(mon); 

This should properly update the GUI every time a file is imported.

Upvotes: 0

Related Questions