Results 1 to 11 of 11
- 07-18-2010, 02:40 PM #1
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:
this is the whole 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; return ; // this is the problem } }
and the main classJava 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 } }
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(); } }
- 07-18-2010, 02:58 PM #2
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.
- 07-19-2010, 05:19 AM #3
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
implementation in my programJava 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; } }
can you give me something about thisJava 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; }
- 07-19-2010, 06:04 AM #4
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.
- 07-19-2010, 07:33 AM #6
-
- 07-19-2010, 07:52 AM #8
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 lìsten my command, but it just once :( .
I will apreciate any reply
Matur nuwun
- 07-19-2010, 01:23 PM #9
The overridden run() method doesn't return a value. It has to set an external variable with the value it wants to "return".the value which return from run()
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.
- 07-19-2010, 03:49 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 07-20-2010, 05:06 AM #11
Similar Threads
-
Not able to return the method value
By dmakshay2002 in forum Advanced JavaReplies: 11Last Post: 05-28-2010, 02:07 PM -
Method won't return value
By footyvino in forum New To JavaReplies: 2Last Post: 03-26-2010, 10:49 AM -
can't return a value from a method / jdbc
By tascoa in forum JDBCReplies: 3Last Post: 10-15-2009, 01:02 PM -
method that return 2 arguments
By itaipee in forum New To JavaReplies: 19Last Post: 01-12-2009, 05:36 PM -
Return value of method
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks