Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,264 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,237 people online right now. Registration is fast and FREE... Join Now!




using a JTextArea for output

 
Reply to this topicStart new topic

using a JTextArea for output

rgfirefly24
26 Apr, 2008 - 07:30 PM
Post #1

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 403



Thanked: 7 times
Dream Kudos: 150
My Contributions
ok so i have a synced multithread operation going. I am trying to get it so that output goes to a TextArea instead of to System.out. For some reason it keeps spitting erros on my creation of TextArea amoung other erros

CODE

import java.util.concurrent.*;
import java.util.concurrent.locks.*;
import java.awt.*;
import javax.swing.*;

public class TaskThreadDemo extends JFrame{
  public static void main(String[] args) {
    // Create tasks
    Runnable printA = new PrintChar('a', 100);
    Runnable printB = new PrintChar('b', 100);
    Runnable print100 = new PrintNum(100);

    // Create threads
    Thread thread1 = new Thread(printA);
    Thread thread2 = new Thread(printB);
    Thread thread3 = new Thread(print100);

    // Start threads
    thread1.start();
    thread2.start();
    thread3.start();

    Output frame = new Output();
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("lab9");
    frame.setVisible(true);

  }
}
class Output extends JPanel{
        private TextArea jtaOutput = new TextArea();


        Output(){

            jtaOutput.setLineWrap(true);
            jtaOutput.setWrapStyleWord(true);

            ScrollPane scrollPane = new ScrollPane(jtaOutput);
            setLayout(new FlowLayout());
            add(scrollPane);
        }
}

// The task for printing a specified character in specified times
class PrintChar implements Runnable
{
      private char charToPrint; // The character to print
      private int times; // The times to repeat
      private static Lock lock = new ReentrantLock();


      /** Construct a task with specified character and number of
       *  times to print the character
       */
      public PrintChar(char c, int t)
      {
        charToPrint = c;
        times = t;
      }

      /** Override the run() method to tell the system
       *  what the task to perform
       */
      public void  run()
      {
        lock.lock();
          try{
                Thread.sleep(100);
                for (int i = 0; i < times; i++)
                {
                      System.out.print(charToPrint);
                }
            }
        catch (InterruptedException ex){
            ex.printStackTrace();
        }

        finally
        {
            lock.unlock();
        }
    }
}

// The task class for printing number from 1 to n for a given n
class PrintNum implements Runnable
{
    private int lastNum;
    private static Lock lock = new ReentrantLock();

      /** Construct a task for printing 1, 2, ... i */
      public PrintNum(int n)
      {
        lastNum = n;
      }

      /** Tell the thread how to run */
      public void run()
      {
        lock.lock();
        try{
              for (int i = 1; i <= lastNum; i++)
                {
                  System.out.print(" " + i);
            }
        }
      finally{
          lock.unlock();
      }
    }
}


ok i get these errors and i'm not sure exactly why
QUOTE

H:\Advanced Java\book\TaskThreadDemo.java:24: cannot find symbol
symbol : method pack()
location: class Output
frame.pack();
^
H:\Advanced Java\book\TaskThreadDemo.java:25: cannot find symbol
symbol : method setLocationRelativeTo(<nulltype>)
location: class Output
frame.setLocationRelativeTo(null);
^
H:\Advanced Java\book\TaskThreadDemo.java:26: cannot find symbol
symbol : method setDefaultCloseOperation(int)
location: class Output
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
H:\Advanced Java\book\TaskThreadDemo.java:27: cannot find symbol
symbol : method setTitle(java.lang.String)
location: class Output
frame.setTitle("lab9");
^
H:\Advanced Java\book\TaskThreadDemo.java:38: cannot find symbol
symbol : method setLineWrap(boolean)
location: class java.awt.TextArea
jtaOutput.setLineWrap(true);
^
H:\Advanced Java\book\TaskThreadDemo.java:39: cannot find symbol
symbol : method setWrapStyleWord(boolean)
location: class java.awt.TextArea
jtaOutput.setWrapStyleWord(true);
^
H:\Advanced Java\book\TaskThreadDemo.java:41: cannot find symbol
symbol : constructor ScrollPane(java.awt.TextArea)
location: class java.awt.ScrollPane
ScrollPane scrollPane = new ScrollPane(jtaOutput);
^
7 errors

Tool completed with exit code 1


User is offlineProfile CardPM
+Quote Post


pbl
RE: Using A JTextArea For Output
26 Apr, 2008 - 09:46 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,951



Thanked: 673 times
Dream Kudos: 200
My Contributions
QUOTE

H:\Advanced Java\book\TaskThreadDemo.java:24: cannot find symbol
symbol : method pack()
location: class Output
frame.pack();
^
H:\Advanced Java\book\TaskThreadDemo.java:25: cannot find symbol
symbol : method setLocationRelativeTo(<nulltype>)
location: class Output
frame.setLocationRelativeTo(null);
^
H:\Advanced Java\book\TaskThreadDemo.java:26: cannot find symbol
symbol : method setDefaultCloseOperation(int)
location: class Output
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
H:\Advanced Java\book\TaskThreadDemo.java:27: cannot find symbol
symbol : method setTitle(java.lang.String)
location: class Output
frame.setTitle("lab9");
^

These methods: pack(), setLocationRelativeTo(), setDefaultCloseOperation() and setTitle()
apply to JFrame not to JPanel and your variable frame, despite its name holds, a JPanel not a JFrame

QUOTE

H:\Advanced Java\book\TaskThreadDemo.java:38: cannot find symbol
symbol : method setLineWrap(boolean)
location: class java.awt.TextArea
jtaOutput.setLineWrap(true);
^
H:\Advanced Java\book\TaskThreadDemo.java:39: cannot find symbol
symbol : method setWrapStyleWord(boolean)
location: class java.awt.TextArea
jtaOutput.setWrapStyleWord(true);
^

The methods setLineWrap() and setWrapStyle()
apply to Swing JTextArea not to AWT TextArea

QUOTE

H:\Advanced Java\book\TaskThreadDemo.java:41: cannot find symbol
symbol : constructor ScrollPane(java.awt.TextArea)
location: class java.awt.ScrollPane
ScrollPane scrollPane = new ScrollPane(jtaOutput);
^

And I guess it is the same problem with ScroolPane and JScroolPane

This post has been edited by pbl: 26 Apr, 2008 - 09:52 PM
User is offlineProfile CardPM
+Quote Post

rgfirefly24
RE: Using A JTextArea For Output
27 Apr, 2008 - 03:17 AM
Post #3

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 403



Thanked: 7 times
Dream Kudos: 150
My Contributions
ok new problem. I got everything set up and able to compile. The JTextArea shows on the JFrame now; however, the output doesnt go to the screen.
new code=
CODE

import java.util.concurrent.*;
import java.util.concurrent.locks.*;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JTextArea.*;

public class TaskThreadDemo extends JFrame{
  TaskThreadDemo(){
      outputPanel p1 = new outputPanel();
        setLayout(new FlowLayout());

        add(p1);
  }

  public static void main(String[] args) {


    // Create tasks
    Runnable printA = new PrintChar('a', 100);
    Runnable printB = new PrintChar('b', 100);
    Runnable print100 = new PrintNum(100);

    // Create threads
    Thread thread1 = new Thread(printA);
    Thread thread2 = new Thread(printB);
    Thread thread3 = new Thread(print100);

    // Start threads
    thread1.start();
    thread2.start();
    thread3.start();


    TaskThreadDemo frame = new TaskThreadDemo();
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("lab9");
    frame.setVisible(true);
    frame.setSize(500,500);

  }


}
class Output{
        char input1;
        int input2;
        String out = " ";

        Output(){
        }


        public Output(char text, int n)
        {
            this.input1 = text;
            this.input2 = n;
        }

        public void setOutput(char text, int n){
            String internal = " ";


            if (text != '\\')
            {
                internal = Character.toString(text);
                out +=internal;
                internal = " ";
            }
            if (n != 0)
            {
                internal = Integer.toString(n);
                out += internal;
                internal = " ";
            }
            this.out = out;
        }
        public String getOutput(){
            return out;
        }
}

class outputPanel extends JPanel{
    Output outtext = new Output();

    String text = outtext.getOutput();

    outputPanel()
    {
        JTextArea jtaOutput = new JTextArea(text,10,20);
        jtaOutput.setLineWrap(true);
        jtaOutput.setWrapStyleWord(true);
        jtaOutput.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(jtaOutput);
        setLayout(new FlowLayout());
        add(scrollPane);
    }
}
// The task for printing a specified character in specified times
class PrintChar implements Runnable
{

      Output output = new Output();
      private char charToPrint; // The character to print
      private int times; // The times to repeat
      private static Lock lock = new ReentrantLock();


      /** Construct a task with specified character and number of
       *  times to print the character
       */
      public PrintChar(char c, int t)
      {
        charToPrint = c;
        times = t;
      }

      /** Override the run() method to tell the system
       *  what the task to perform
       */
      public void  run()
      {
        lock.lock();
          try{
                Thread.sleep(100);
                for (int i = 0; i < times; i++)
                {
                      output.setOutput(charToPrint, 0);
                }
            }
        catch (InterruptedException ex){
            ex.printStackTrace();
        }

        finally
        {
            lock.unlock();
        }
    }
}

// The task class for printing number from 1 to n for a given n
class PrintNum implements Runnable
{
    Output output = new Output();
    private int lastNum;
    private static Lock lock = new ReentrantLock();

      /** Construct a task for printing 1, 2, ... i */
      public PrintNum(int n)
      {
        lastNum = n;
      }

      /** Tell the thread how to run */
      public void run()
      {
        lock.lock();
        try{
              for (int i = 1; i <= lastNum; i++)
                {
                  output.setOutput('\\', i);
            }
        }
      finally{
          lock.unlock();
      }
    }
}



This post has been edited by rgfirefly24: 27 Apr, 2008 - 04:20 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Using A JTextArea For Output
27 Apr, 2008 - 09:53 AM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,951



Thanked: 673 times
Dream Kudos: 200
My Contributions
CODE

class outputPanel extends JPanel{
    Output outtext = new Output();

    String text = outtext.getOutput();

    outputPanel()
    {
        JTextArea jtaOutput = new JTextArea(text,10,20);
        jtaOutput.setLineWrap(true);
        jtaOutput.setWrapStyleWord(true);
        jtaOutput.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(jtaOutput);
        setLayout(new FlowLayout());
        add(scrollPane);
    }
}


I do not see anywhere in your code where you are writting into your JTextArea
Actually if I look at your outputPanel constructor you:
- build jtaOutput
- put it on the scroolpane
but as jtaOutput is a local variable to the constructor it will not be accessible by anybody else when the constructor exits


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 12:19PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month