Results 1 to 4 of 4
  1. #1
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default writing to text Area through Class

    Hey

    i am reading a text file and all the functions that do this are in a class and i want to write this text to a text area. this is my code -

    public void FileShow()
    {
    try {
    BufferedReader brU = new BufferedReader(new FileReader("/Desktop/read.txt"));
    do {
    textAreaUsers.setText(brU.readLine());
    } while (brU.read() != -1);
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "IOException");
    }
    }

    and the text area is in the jframe

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dilpreet28 View Post
    Hey

    i am reading a text file and all the functions that do this are in a class and i want to write this text to a text area. this is my code -

    public void FileShow()
    {
    try {
    BufferedReader brU = new BufferedReader(new FileReader("/Desktop/read.txt"));
    do {
    textAreaUsers.setText(brU.readLine());
    } while (brU.read() != -1);
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "IOException");
    }
    }

    and the text area is in the jframe

    You are setting the text in your JTextArea to the most recently read line from your file; therefore you end up with the last line read. Also what is that brU.read() call doing there? It is snatching away single characters from every line except the first line. Read the API for the JTextArea class and see how you can append text to it. Also change your control flow because as it is now it is incorrect.

    kind regards,

    Jos

  3. #3
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    here is the code now, i changed it because i figured out it wasn't appending but i still can't get my class to write text to textArea, putting it in the main class works but i want a separate class for it

    try {
    String lineUser;
    BufferedReader brU = new BufferedReader(new FileReader("/Desktop/read.txt"));
    while ((lineUser = brU.readLine()) != null) {
    textAreaUsers.append(lineUser + System.getProperty("line.separator"));
    }
    brU.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "IOException");
    }

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dilpreet28 View Post
    here is the code now, i changed it because i figured out it wasn't appending but i still can't get my class to write text to textArea, putting it in the main class works but i want a separate class for it
    So (an object of) your other class should at least have a reference to your JTextArea, otherwise it doesn't know where to put the text it has been reading. Maybe you can pass that JTextArea as a parameter to a constructor of your other class or maybe you can pass it as a parameter to the method that actually does the reading. It's all just programming, there's no magic involved.

    kind regards,

    Jos

Similar Threads

  1. under line text area data
    By anilkumar_vist in forum New To Java
    Replies: 1
    Last Post: 02-25-2010, 03:37 PM
  2. Text Area Outline
    By Psyclone in forum AWT / Swing
    Replies: 4
    Last Post: 02-07-2010, 06:12 AM
  3. eol in text area....
    By Nicholas Jordan in forum Networking
    Replies: 0
    Last Post: 09-14-2008, 10:59 PM
  4. Text Area problem
    By mcal in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 09:42 PM
  5. textfiled and text area to UTF-16?
    By Mr tuition in forum AWT / Swing
    Replies: 0
    Last Post: 12-04-2007, 12: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
  •