Results 1 to 9 of 9
  1. #1
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default add FileReader to GUI

    Hi, is there an easy way to add FileReader to this GUI? I just want the program to read a specific file and thne print the first line of the file in the text field.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
     
    
    public class Navigation extends JFrame implements ActionListener
    {
      
      JButton myButton = new JButton("Forward");
      JButton myButton2 = new JButton("Go Back");
     
      
      JTextArea myText = new JTextArea("My text");
     
      JPanel bottomPanel = new JPanel();
     
      
      JPanel holdAll = new JPanel();
     
      
      public Navigation()
      {
        bottomPanel.setLayout(new FlowLayout());
        
        bottomPanel.add(myButton);
        bottomPanel.add(myButton2);
     
        holdAll.setLayout(new BorderLayout());
        holdAll.add(bottomPanel, BorderLayout.SOUTH);
        holdAll.add(myText, BorderLayout.CENTER);
     
        getContentPane().add(holdAll, BorderLayout.CENTER);
     
        myButton.addActionListener(this);
        myButton2.addActionListener(this);
       
     
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      }
     
     
      public static void main(String[] args)
      {
        Navigation myApplication = new Navigation();
     
        myApplication.setLocation(10, 10);
        myApplication.setSize(300, 300);
     
        // Display!
        myApplication.setVisible(true);
      }
     
      
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() == myButton)
          myText.setText("A button click");
        
        else
          myText.setText("E ...?");
    
        if (e.getSource() == myButton2)
          myText.setText("going forward");
    
       else
          myText.setText("E ...?");
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You would read in a file just like any other Java application, with file IO. The main difference to me is that you should probably read the file in a background thread so as not to freeze the GUI while it's being read, though you may get away without one if the file is very short. A SwingWorker would work nicely here. This tutorial describes SwingWorkers and also a bit about concurrency issues in Swing. It's recommended reading for any Swing coder: Concurrency in Swing

  3. #3
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Thanks,

    I have been trying to add the FileReader as I would in a program without GUI ...but I'm doing something wrong. It's probably very obvious to someone good at programming...but as a newbie to programming I just don't know where I keep going wrong.

    Java Code:
    /*
     import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class MyText extends JFrame {
    
        private Toolkit toolkit;
    
    
    public static int LineCount = 0; //Counts the lines.
       public static String[] content;  
       
       public static void ReadFile() throws Exception { 
    
          BufferedReader TextFileReader = new BufferedReader(new FileReader("./info.txt")); 
    
          String c;
          String t;
    
          t = "Blank";
          while((c = TextFileReader.readLine()) != null) {  
             LineCount++;
             t += ","+ c;
          }
    
          t = t.replaceAll("Blank,",""); 
          content = new String[LineCount];
          content = t.split(",");
          TextFileReader.close(); 
          
          
          for(int i = 0; i < 7; i++)  //Keep printing as long as i is smaller than 7, which is what I need for my text file.
              System.out.println(content[i]); //prints the first part of my list of names.
             
       }
    
    
        public MyText() {
    
            setTitle("Name and Address");
            setSize(300, 400);
    
            toolkit = getToolkit();
            Dimension size = toolkit.getScreenSize();
            setLocation((size.width - getWidth())/2, (size.height - getHeight())/2);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel();
            getContentPane().add(panel);
    
    	panel.setLayout(null);
    
    
            JButton beep = new JButton("Next");
            beep.setBounds(150, 60, 80, 30);
            beep.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    toolkit.beep();
    
    System.out.println("Next");
    
    
                }
            });
    
           JButton close = new JButton("Back");
           close.setBounds(50, 60, 80, 30);
           close.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent event) {
                   toolkit.beep();
    
    System.out.println("Go back");
    
              }
           });
    
            panel.add(beep);
            panel.add(close);
    
        }
    
        public static void main(String[] args) {
    
            MyText buttons = new MyText();
            buttons.setVisible(true);
    
        }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I have been trying to add the FileReader as I would in a program without GUI ...but I'm doing something wrong. It's probably very obvious to someone good at programming...but as a newbie to programming I just don't know where I keep going wrong.
    Can you elaborate further on exactly how things are going wrong? Does it not compile, and if so what errors? Do you see any run-time exceptions? If so, what are they and what line? Does your program die? Does it compile and run, but just not show any text?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's an example that uses the JTextArea's ability to read in with a Reader object. Again, if the text is small, the reading can be done without fuss, but if the file is large, do the reading in a background thread such as can be done with a SwingWorker object.

    This program tries to read in a file called Findings.txt that is located in the same directory as the class files (via getResource()).

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.URISyntaxException;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class MyText2
    {
      private static final String TEXT_FILE = "Findings.txt";
      private JPanel mainPanel = new JPanel();
      private JTextArea area = new JTextArea(30, 60);
      
      public MyText2()
      {
        readInFile(area, TEXT_FILE);
        area.setWrapStyleWord(true);
        area.setLineWrap(true);
        
        mainPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        mainPanel.add(new JScrollPane(area));
      }
    
      private void readInFile(JTextArea myArea, String fileName)
      {
        BufferedReader br = null;
        
        try
        {
          br = new BufferedReader(
              new FileReader(
                  new File(getClass().getResource(fileName).toURI())));
          if (br != null)
          {
            myArea.read(br, fileName);
          }
        }
        catch (FileNotFoundException e)
        {
          e.printStackTrace();
        }
        catch (URISyntaxException e)
        {
          e.printStackTrace();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("MyText2");
        frame.getContentPane().add(new MyText2().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  6. #6
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    My program does compile, but it doesn't print the part of the file I want it to print....in fact, it doesn't print anything at all.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by VinTiger View Post
    My program does compile, but it doesn't print the part of the file I want it to print....in fact, it doesn't print anything at all.
    Time to do some debugging. Does any of the code that reads the file get called? You should be able to check this simply.

  8. #8
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Thanks, I've tried everything but it just won't work. It's the part that does the filereading and printing I just can't seem to work in this program.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, do you call the method that reads in the file? I didn't see that you did that in your posted code.

Similar Threads

  1. Replies: 5
    Last Post: 02-02-2010, 10:06 AM
  2. FileReader / Buffered Reader
    By sepaht in forum New To Java
    Replies: 9
    Last Post: 07-10-2008, 09:05 PM
  3. Help with filereader in java
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 10:55 AM

Posting Permissions

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