Results 1 to 11 of 11
  1. #1
    udinulis's Avatar
    udinulis is offline Member
    Join Date
    Jul 2010
    Location
    earth
    Posts
    8
    Rep Power
    0

    Default how to return a value from runnable method

    help me please...
    this is follow up from my program earlier about making command prompt in java how to make command prompt in java. i want to make my program could print some text in jtextArea after i hit "ENTER" key. but i can't return string from run method. here the snippet:
    Java Code:
     public void run() {
        while (true) {
    	  JTextArea tes = new JTextArea();
          logicConsoleClass log = new logicConsoleClass(mArea);
          log.reaksi();
          String code =log.readLine();
          System.out.println(code);
          panggil = code;
          return ;  // this is the problem
    	}
      }
    this is the whole program:

    Java Code:
     /* Created on Jul 7, 2005 by @author Tom Jacobs
     *
     */
    //package tjacobs;
    
    import java.awt.event.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.TextEvent;
    import java.awt.event.TextListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.Reader;
    
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    
    public class logicConsoleClass extends Reader implements Runnable  {
    
      JTextArea mArea;
      Object mKeyLock = new Object();
      Object mLineLock = new Object();
      String mLastLine;
      String panggil;
      int mLastKeyCode = 1;
      String sisip= "localhost> ";
    
      public logicConsoleClass(JTextArea area) {
        super();
    	this.mArea = area;
    
      }
    
      public void run() {
        while (true) {
    	  JTextArea tes = new JTextArea();
          logicConsoleClass log = new logicConsoleClass(mArea);
          log.reaksi();
          String code =log.readLine();
          System.out.println(code);
          panggil = code;
          return ;  // this is the problem
    	}
      }
    
      public void tulis(JTextArea tx){
      this.mArea=tx;
      mArea.append(panggil);
      }
    
      void reaksi(){
        mArea.addKeyListener(new KeyAdapter(){
          public void keyPressed(KeyEvent ke) {
    	    mLastKeyCode = ke.getKeyCode();
    		synchronized(mKeyLock) {
    		  mKeyLock.notifyAll();
    		}
    		if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
    		  String txt = mArea.getText();
    		  int idx = txt.lastIndexOf('\n', mArea.getCaretPosition() - 1);
    		  mLastLine = txt.substring(idx != -1 ? idx : 0, mArea.getCaretPosition());//txt.length());
    		  synchronized(mLineLock) {
    		    mLineLock.notifyAll();
    		  }
    		}
    	  }
        });
    
      }
    
      public void keyReleased(KeyEvent ke) {
    
      }
    
      public void keyTyped(KeyEvent ke) {
    
      }
    
      public int read(char[] arg0, int arg1, int arg2) throws IOException {
        throw new IOException("Not supported");
      }
    
      public String readLine() {
        synchronized(mLineLock) {
    	  try {
    	    mLineLock.wait();
    	  }
    	  catch (InterruptedException ex) {
    	  }
    	}
    	return mLastLine;
      }
    
      public int read() {
        synchronized(mKeyLock) {
    	  try {
    	    mKeyLock.wait();
    	  }
    	  catch (InterruptedException ex) {
    	  }
    	}
    	return mLastKeyCode;
      }
    
      public void close() throws IOException {
        // TODO Auto-generated method stub
      }
    
    }
    and the main class

    Java Code:
    /** created on jul 18, 2010
    * author: udinulis
    * */
    
    
    import java.awt.event.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.TextEvent;
    import java.awt.event.TextListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.Reader;
    
    import javax.swing.*;
    
    class cobaPanggil extends JFrame{
      private JTextArea teks = new JTextArea();
      private JTextField bayangan = new JTextField();
      private logicConsoleClass log = new logicConsoleClass(teks);
      String hahai;
    
      public cobaPanggil(){
        super("judul teks");
        setSize(250,250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);
        getContentPane().add(bayangan);
        bayangan.setBounds(50,10,100,20);
        getContentPane().add(teks);
        teks.setBounds(50,50,100,100);
        setVisible(true);
      }
    
      public void komponen(){
        log.reaksi();
        log.run();
        log.tulis(teks);
      }
    
      public static void main(String args[]) {
        cobaPanggil cp = new cobaPanggil();
        cp.komponen();
      }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Who is calling the run() method? If you returned something, who receives it?
    You need to find a way to pass the value to the enclosing class.

  3. #3
    udinulis's Avatar
    udinulis is offline Member
    Join Date
    Jul 2010
    Location
    earth
    Posts
    8
    Rep Power
    0

    Unhappy

    i am using main classes to call the run method. value from run will be used to append in jtextarea. i have trying like this but the result is null

    Java Code:
    class MyThread extends Thread
    {
        private SomeType initVal;
        private SomeType retVal;
        
        public MyThread (SomeType initVal)
        {
            this.initVal = initVal;
        }
        
        public void run()
        {
            ...
            retVal = ...;
        }
        
        public SomeType getRetVal()
        {
            return retVal;
        }
    }
    implementation in my program
    Java Code:
    public void run() {
        while (true) {
    	  JTextArea tes = new JTextArea();
          logicConsoleClass log = new logicConsoleClass(mArea);
          log.reaksi();
          String code =log.readLine();
          System.out.println(code);
          panggil = code;
         
    	}
      }
    
    public String pgl(){
    return panggil;
      
    }
    can you give me something about this

  4. #4
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Ok,
    when I run your program it did started. And looking at it, I think what you want is, type some text in TextField and when pressed entered that text to be shown is TextArea, which is just below. Is that how supposed to be? If it is...

    use KeyListener in textField not in textArea.

    What I mean is when I type something in textarea and press enter then same line is printed below. So your program is running, but at the wrong end.
    -Regards

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

    Default

    Quote Originally Posted by Prajin View Post
    Ok,
    when I run your program it did started. And looking at it, I think what you want is, type some text in TextField and when pressed entered that text to be shown is TextArea, which is just below. Is that how supposed to be? If it is...

    use KeyListener in textField not in textArea.

    What I mean is when I type something in textarea and press enter then same line is printed below. So your program is running, but at the wrong end.
    -Regards
    No, if that were the problem then you'd add an ActionListener to the JTextField, not a KeyListener.

    But regardless, we're all just guessing since I think that most of us don't understand what the original poster is trying to achieve, so to the original poster I will suggest that if he doesn't get a helpful reply seen, he greatly clarify the problem so that we can fully understand it.

    Luck.

  6. #6
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, if that were the problem then you'd add an ActionListener to the JTextField, not a KeyListener.
    Yup ActionListener will do.
    Like: -
    Java Code:
    if (ke.getKeyCode() == KeyEvent.VK_ENTER){
    //-- ActionListener
    }
    What I was referring , that he was using KeyListener, and it's just good for one use(press enter I get output and press next time nothing.)

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

    Default

    Quote Originally Posted by Prajin View Post
    Yup ActionListener will do.
    Like: -
    Java Code:
    if (ke.getKeyCode() == KeyEvent.VK_ENTER){
    //-- ActionListener
    }
    What I was referring , that he was using KeyListener, and it's just good for one use(press enter I get output and press next time nothing.)
    Sorry, but your post is not clear to me. You would not wrap an action listener inside of a key listener, but would use an ActionListener by itself. I see no reason for a key listener at all here and would strongly discourage its use here.

  8. #8
    udinulis's Avatar
    udinulis is offline Member
    Join Date
    Jul 2010
    Location
    earth
    Posts
    8
    Rep Power
    0

    Default

    Thank's to prajin which have try my hideous code, and fubarable the moderator. Like my post before, i want to make some command prompt program in java. So u know... Cmd doesn't have jtextfield yet :) . Actually the value which return from run() will be used to invoke other process, not only to print in jtext area and my program have ĺsten my command, but it just once :( .
    I will apreciate any reply
    Matur nuwun

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    the value which return from run()
    The overridden run() method doesn't return a value. It has to set an external variable with the value it wants to "return".
    Then the problem is how to tell the other thread that the value has been "returned" (ie has been stored in the external variable) What does the other thread(not the run code) do with the "returned value"? There needs to be some communication between the threads.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    As I wrote before: nobody pays attention to the java.util.concurrent package and its classes/interfaces; read the API documentation for the Callable interface and e.g. the Executors class.

    kind regards,

    Jos

  11. #11
    udinulis's Avatar
    udinulis is offline Member
    Join Date
    Jul 2010
    Location
    earth
    Posts
    8
    Rep Power
    0

    Default

    Thank's you all. I have achieve what i want. Thank u norm for your advice. I am litle confused about return mechanism yesterday. Instead i am using jtextfield which called in costructor to get the value.

Similar Threads

  1. Not able to return the method value
    By dmakshay2002 in forum Advanced Java
    Replies: 11
    Last Post: 05-28-2010, 03:07 PM
  2. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 11:49 AM
  3. Replies: 3
    Last Post: 10-15-2009, 02:02 PM
  4. method that return 2 arguments
    By itaipee in forum New To Java
    Replies: 19
    Last Post: 01-12-2009, 06:36 PM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:23 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
  •