Forums Register Login

keyboard input and graphic window

+Pie Number of slices to send: Send
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...
}
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
OK, thank you. That is what I thought might be the case. I will try getting keyboard input from the GUI.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1083 times.
Similar Threads
where is the main method now...where should code be placed?
My calculator needs help.
KeyListener and ActionListener error.
Swings newbee : Changing window components
Error in JButton Code
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:03:13.