Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modal dialog

Status
Not open for further replies.

jaidex

Programmer
May 7, 2001
3
US
I'm trying to create a terminal-emulator like application. In the screen class, I need to have a method that I can call that will allow the user to type in some info and when they hit enter, return to my main code. I can't figure out a way to handle this - what I need to be able to do is the same thing that happens in the built-in dialog box, to call the method, and wait for the return. How is this accomplished?

Note: the way I STARTED to go was to sit in a sleep loop and wait for characters and scan each one, but that causes the awt / jfc to not be able to update the screen ( maybe I need a way to sit in a loop and tell java to go process everything else and then come back?).
 
Hi,

Maybe you would like to take a look at the Class KeyListener. So what you have to do is to implement KeyListener for your dialog class and within the KeyPressed method, compare the keyevent with the enter key and it should work :)

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
That isn't exactly the problem - I'm an experienced Java user already, and the problem isn't at that level. The problem is actually proving to be difficult to describe. Here's what I need to do:

TextScreen class: does all screen I/O (display & keyboard)

MainControl class: creates a TextScreen object, displays
a message on the text screen and waits for user input
from the keyboard (display's a prompt, waits for an
response). The execution of this class MUST wait for
the return from the TextScreen object's keyboard input
routine.

The problem comes when trying to have those two classes interact. I wouldn't have any problem having the TextScreen class get key input and buffer it somewhere, the problem is that I need for the MainControl class to request info from the keyboard and WAIT for the input before continuing. I've found a couple of ways to do it, but they're really messy (callbacks, etc). In my first attempt, I tried having the system sleep() while waiting for input, but that prevents the display from updating and drags the system down. What I need is a design pattern here (or at least a description of the normal way to accomplish this).

Thanks again!

Jai
 
Hi,

Sorry I misunderstood your question earlierand I hope I got your question correct this time but anyway I have a solution but whether you want to use or not or more importantly whether it works or not, its up to you. I didn't try this out yet but theortically speaking it should work.

If I didn't get your question wrong this time, you want your second panel which is the TextScreen class to read the 'enter' key and then the first screen will proceed on with what it is doing.

I have 2 solutions for this.

First solution, get your first class to implement the Thread class. This will allow you to have a run() method. Within this run() method, it will keep checking whether a variable is true or false. So when the 'enter' key is pressed, the variable will be changed accordingly and the program will carry on running. So it would be something like this :-

public class MainControl implements Thread
{
boolean flag;
public MainControl()
{
flag = false;
}

class TextScreen
{
public TextScreen()
{
}
public void keyPressed(KeyEvent e)
{
// assuming you implemented the KeyListener event as well
// if key pressed is enter
flag = true;
// close the panel if you want
}
}

public void run()
{
// make it keep on looping
while (true)
{
if (flag == true)
{
//do whatever you want here
break;
}
}
}
}

The other solution is about the same except that instead of implementing Thread and having a variable, you might want to have a method and get your subclass to call your parent class method.

Hope this helps,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Thanks! That's a LOT closer to what I was looking for - the answer is pretty simple, but it's better than any of the things I've come up with! It still has a little problem that doesn't seem necessary though - when you launch a dialog box in java, you don't have to kick off another thread (at least not manually, maybe internally it's happening), and monitor a variable. The way you suggest (which is the best I've been able to find in my search) doesn't really allow me to use a method of the TextScreen class to ask the question, but I could make it a part of my Globals class...

Thanks a lot! (If you have any further thoughts, please let me know!)

Jai
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top