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 http://www.java-forums.org/java-2d/3...ompt-java.html. 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:
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:
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
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();
}
}