0

I am trying to open up a file and add it to a textarea, but the textfile wont work not sure what exactly isnt working but i know that the compiler reaches the function/method where i implement the read in file code. Here is the code

public class PictureAndButton extends JFrame implements ActionListener
{

private JMenuItem menuOptionOne = new JMenuItem("Lägg till text", KeyEvent.VK_L);
private JTextField textFalt  = new JTextField();


public PictureAndButton()
{
menuInfo.add(menuOptionOne);
menuOptionOne.addActionListener(this);
textField.addActionListener(this);    
setSize(350, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);       
}

public void actionPerformed(ActionEvent e)
{
  if(e.getSource() == menuOptionOne)
    {
        readInFile(textField.getText());
    }
   }    

private void readInFile(String hej) 
{
   try
   {
       BufferedReader inFile = new BufferedReader(new FileReader(hej));  
       while(true)
       {
           String rad = inFile.readLine();
          if(rad == null)
           break;
           textArea.append(rad);       

       }
   }
   catch(IOException e){}
}

public static void main(String[] args)
    {
        PictureAndButton peanutButter = new PictureAndButton();
    }

}

I am running it on Eclipse and I am not getting any errors, the input text in the textfield doesnt change at all. Nothing is added to the text area. Really need some help on this

3
  • 1.) You have an infinite loop. 2.) You do not call readInFile() anywhere in your code. Commented Jun 5, 2012 at 8:17
  • Oh sorry. I inputted the wrong part of the code Commented Jun 5, 2012 at 8:21
  • A couple of advices; when you are giving a code example, it usually helps to name your variables to something that gives a clue about what they are, in English. For instance changing: rad -> line, hej -> file, "lägg till text" -> "add text" would help people understand what you are trying to do with a brief look and ultimately help you get useful answers. Commented Jun 5, 2012 at 8:34

2 Answers 2

2

I am trying to open up a file and add it to a textarea, ..

Use JTextComponent.read(Reader,Object) instead.

e.g.

textArea.read(new FileReader(hej), hej);
Sign up to request clarification or add additional context in comments.

Comments

1

Add some System.out.println(); to debug, like:

if(e.getSource() == menuOptionOne)
{
    System.out.println("Trying to load file: "+textField.getText());
    readInFile(textField.getText());
}

And more, use that try with and message inside the catch to know when something fails.

 try
 {
   BufferedReader inFile = new BufferedReader(new FileReader(hej));  
   while(true)
   {
       String rad = inFile.readLine();
      if(rad == null)
       break;
       textArea.append(rad);       

   }
} catch(IOException e){ System.out.println("Problem on loading file .Because: "e.getMessage();
     }

3 Comments

The edits do not make it an answer. It would make for a great comment.
@Claudiu C i received the message from the constructor, but nothing from the Exception..?
It says: Trying to load file:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.