Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why doesn't JTextArea execute in this piece of code?

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why doesn't JTextArea execute in this piece of code?

    Hi I have the following code:
    JFrame jframe = new JFrame ();
        .........
        JTextArea ta = new JTextArea(20,40);  
       .........
          try {
            FileReader fr = new FileReader ("indice.txt");
            BufferedReader br = new BufferedReader (fr);	
            System.out.println ("O arquivo-indice existe!");
            ta.setText ("TESTE1");
            ta.setText ("O arquivo-indice existe!");
            ta.setText ("TESTE2");
            if (fm.checkFileList ())  
              //System.out.println ("Arquivo indice nao foi modificado. Nada a fazer");  
    	  ta.setText ("Arquivo indice nao foi modificado. Nada a fazer");
            else {
              //System.out.println ("Arquivo índice foi modificado. Atualizando arquivo índice de reserva: "); 
              ta.setText ("Arquivo índice foi modificado. Atualizando arquivo índice de reserva: "); 
              fm.printFileList();
            }

    As can be seen there is a
     ta.setText ("O arquivo-indice existe!");
    and two lines above there is
    System.out.println ("O arquivo-indice existe!")
    . Or, in another words, with the same content.
    This is because I am using the JTextArea to substitute the System.out.println
    The problem it is.......that JTextArea ain't showing anything! More specifically it doesn't show anything only in this piece of code. I added
    ta.setText ("TESTE1");
    and
    ta.setText ("TESTE2");
    to test it and they both also doesn't work. The
    System.out.println ("O arquivo-indice existe!");
    that it is previous to them work as a charm. And so does
    ta.setText
    that exists inside of the if-else clauses.........so what is going on?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why doesn't JTextArea execute in this piece of code?

    I don't see where the text area is ever added to the frame.
    I don't see where the frame is ever set visible.
    The words "doesn't work" does not offer much insight to the problem. Try to explain exactly what does happen, and exactly what you want to happen. Also try to explain where in the code things seem to go unexpected.

    Does the frame even show? It is okay to post code snippets, (even encouraged to do so), but you should include enough code to get the complete picture. (In this case the code that adds ta to jframe and where jframe is setVisible)

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why doesn't JTextArea execute in this piece of code?

    Quote Originally Posted by jps View Post
    I don't see where the text area is ever added to the frame.
    I don't see where the frame is ever set visible.
    It is okay to post code snippets, (even encouraged to do so), but you should include enough code to get the complete picture. (In this case the code that adds ta to jframe and where jframe is setVisible)
    More of the code:

        JFrame jframe = new JFrame ();
        jframe.setVisible (true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        JTextArea ta = new JTextArea(20,40);  
        jframe.getContentPane().add(new JScrollPane(ta));  
        jframe.pack();
        if (args[2].equals ("bi")) {
          try {
            FileReader fr = new FileReader ("indice.txt");
            BufferedReader br = new BufferedReader (fr);	
            System.out.println ("O arquivo-indice existe!");
            ta.setText ("TESTE1");
            ta.setText ("O arquivo-indice existe!");
            ta.setText ("TESTE2");
            if (fm.checkFileList ())  
              //System.out.println ("Arquivo indice nao foi modificado. Nada a fazer");  
    	  ta.setText ("Arquivo indice nao foi modificado. Nada a fazer");
            else {
              //System.out.println ("Arquivo índice foi modificado. Atualizando arquivo índice de reserva: "); 
              ta.setText ("Arquivo índice foi modificado. Atualizando arquivo índice de reserva: "); 
              fm.printFileList();
            }

    The words "doesn't work" does not offer much insight to the problem. Try to explain exactly what does happen, and exactly what you want to happen. Also try to explain where in the code things seem to go unexpected.

    Does the frame even show?
    This code
    ta.setText ("TESTE1");
            ta.setText ("O arquivo-indice existe!");
            ta.setText ("TESTE2");

    doesn't show up in the window. The rest work as intended. Simple like that


    UPDATED: I figured out what it is the problem. It is showing only the last line. More precisely, it overwrites the last line when it receives a new one. So in my example it is gonna show what it is inside the if/else clause. But what I need to do so the frame keep the old lines?

    UPDATED2: I find out that the problem was that I was using SetText. Instead I should be using the append method

Similar Threads

  1. Converting a piece of C# code to Java (unsigned longs, etc)
    By nahkiss in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2013, 12:44 PM
  2. How do I represent any character in a piece of code?
    By 93tomh in forum Java Theory & Questions
    Replies: 3
    Last Post: July 15th, 2012, 05:28 AM
  3. Replies: 0
    Last Post: August 30th, 2011, 08:23 AM
  4. Code doesn't execute properly
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 12:36 PM
  5. [SOLVED] Detecting whether the piece of code is executing?
    By vivek1982 in forum Web Frameworks
    Replies: 4
    Last Post: March 27th, 2009, 06:17 AM