aspose file tools*
The moose likes Threads and Synchronization and the fly likes keyboard input and graphic window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "keyboard input and graphic window" Watch "keyboard input and graphic window" New topic
Author

keyboard input and graphic window

Bob Butcher
Greenhorn

Joined: Feb 03, 2005
Posts: 5
I am a relative newbie to Java, and have a program that runs in a dos window that reads data. It creates a graphic window that displays the data. This all works fine except that I need to use a readLine () method in my dos window and when the graphic window is created it has the focus.

I want to make the dos window the active window, but have not found the right combination, other than a mouse click to get back to the dos window.

I also tried creating the graphic display in a thread implementing Runnable and sending the data to the thread, which also worked, but had the same problem with focus. I tried sleep() and wait() methods in the thread, but that didn't work either. I think the answer is using focus, but was confused by just how to implement this. Please be specific in your answer if possible.

class Test implements WindowListener, ActionListener {
...define static variables...
static JFrame jf=new JFrame ();
static JPanel jp=new JPanel ();
...create some labels and add to jp...
...add window events to jf...
jf.setVisible (true);
while (true) {
getData ();
updateDisplay ();
}
static void getData () {
...create BufferedReader br...
data=br.readLine ();
return data;
}
updateDisplay () {
label1.setText (data);
...send additional data to other labels in jp...
}
...window event methods, etc...
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Bob,

Welcome to JavaRanch!

So if I understand you correctly, you want to start a program from the command line, do some typing there, then the program launches a GUI, which you interact with, and then you want to bring that command line window back to the front? Sorry to say, but no can do. That window isn't owned by Java, and Java can't touch it.

Now, the right thing to do is to move the command-line input into the GUI; instead of typing at the command line, the users should type into the GUI window itself from the very moment the app is launched.


[Jess in Action][AskingGoodQuestions]
Bob Butcher
Greenhorn

Joined: Feb 03, 2005
Posts: 5
OK, thank you. That is what I thought might be the case. I will try getting keyboard input from the GUI.
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
Hi Bob,
I've got a little suggetion for a newbie that helped me a lot.
Get an IDE that supports visual development.
I like netbeans (www.netbeans.org) free ;-)
It'll let you create your GUI by just dragging and dropping components.
The real purpose of this sort of environment is rapid development, but I find it extremly useful for a newbie whose kinda stuck and doesn't quiet know what to do. You can built your GUI and then go back and examine what should be fairly nicely laid out, well structured code. It's important not to be lazy and just let the IDE do all the work, go back and make yourself understand what's going on, but some of these event handling structures in java are kinda deep dodo and it sure is nice to have somebody write out code that does exactly what you want and let you go back and disect it. You can also set breakpoints in your code and debug it and see exactly what's going on. Anyway just my 2 cents worth.

Dave
Bob Butcher
Greenhorn

Joined: Feb 03, 2005
Posts: 5
Thanks for the tips. I finally got it working today by inserting a JTextField in my JPanel. The JTextField is designed to read from the keyboard and sends an ActionListener command on CR/LF, and the resulting string can be read using jtf.getText () and nulled out using jtf.setText (""). I will try the free software as well.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Dave Robbins:
You can built your GUI and then go back and examine what should be fairly nicely laid out, well structured code.


I have to strongly urge you to consider an alternative view here. The truth is that examining code generated by an IDE is believed by most experts to be a horrifyingly bad way to learn Java programming. Machine-generated code doesn't need to be neat, comprehensible, or maintainable. It doesn't have to follow even the most basic rules of good style. So in general, it doesn't. If you learn to code by copying what you see in IDE-generated code, you'll have learned to write ugly, nonidiomatic, impossible to modify, nonportable, scary cruft!

Better to bite the bullet and learn. There are plenty of excellent books, and also lots of free tutorials.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: keyboard input and graphic window