How should JVM/Swing GUI application show console? - java

The application creates and displays various Swing widgets, and also writes debugging messages to System.out. If I start it as java -jar ...jar then I see it, but if I click on jar file in GUI I don't see the console. How to make it show console to user explicitly, e.g. on reaction to "View -> Debug output" menu item?
Expecting something that will pop up cmd.exe window on Windows, xterm/... on Linux, but it may be Swing window as well. How to do it the easily?

First don't use System.out to log, use some Logging framework to write logs in some file and to display the debugging logs open a new JDialog with JTextArea or JTextPane in it. Read the log file content and display that in the textArea. This way it will also solve platform dependency.

I think that you looking for CTRL + SHIFT + F1 works if is GUI visible here you can see output to the console, with Tree hierarchy of JComponents

AFAIK on Windows the console would be displayed on the first console output (i.e. System.out.printXXX), while on Linux it must be executed from console itself to do so. I'd rather create a separate window with a JTextArea (or something similar) to display the debug output. Normal users won't care about / need it, right?

Related

How to handle Eclipse console input and output (Java)?

I have a Java application run eclipse, user could input and output by "console"; in the meantime, I want to use another application to handle the "console", for example, another Java application to input "text string" into console, and then output saved into file. I am sorry firstly for the confusing question, do someone have any ideas?
You can run two applications at the same time using using the console. If you look in the console pane, there's a button on the top right that looks like a little compute monitor (Display Selected Console). When running two applications, you can toggle the console view for each application.

How do I view error messages for my Java applet?

While trying to answer a applet question, the OP didn't know how to view error messages for his applet. It's been a while since I did this myself and I didn't find any step-by-step instructions for doing it, so I am posting a question and answer here for future generations.
Windows:
Double-click on the Java icon in the System tray next to the clock in the lower right corner. This will open the Java Control Panel.
Click on the Advanced tab.
Click on the + next to "Java Console" then click on "Show the Console".
The Java Console will display any stacktraces showing error messages from thrown Exceptions. You can also add System.out.println() calls to your applet code to print your own custom messages in the console.

Java: Clear console of NetBeans, through my program

I want to clear the console output of the NetBeans console. We can clear it manually by using Ctrl+L.
Is it possible to do this programmatically, in Java?
Thank you very much
I think that it's not so simple.
The Netbeans console is not really a full system console.
I'd see a proper option - like getting the console reference using the Netbeans RPC binding, but your application would need to run as a Netbeans plugin or bundle. (so - don't do that, keep it simple)
For a shortcut (workaround) - you may try to use java.awt.Robot class to send a keyRelease event (Ctrl+L) while being focused in the console (effectively sending the Ctrl+L event to the focused component)
This is a poor solution because it doesn't actually clear anything, but it does push it out of the way to hopefully make it more readable.
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
add or subtract "\n" to lengthen or shorten it depending on the size of the console window.

Can the "Java console" window be programmatically enabled within a JNLP application

When running a JNLP application (for example), "Java console" is opened, containing the output of the program and a bunch of debug functionality.
Whether the console is opened or not, is determined in the settings in Java Control Panel.
Is there a way to enable & show this Java console programmatically during the program execution?
Or alternatively, is there any way to to enable this Java console during the program execution if it wasn't enabled at the program startup?
(I'm thinking that probably not but if there's a way that could be nice to know)
br, Touko
Andrew Thompson's answer is the right way to do this. My answer is the fast way, if you don't have time to implement a logging framework now:
If you have written your program without logging and you just want to see all those System.out.println messages, you can redirect them all to a text file.
Inside your main method, try this:
System.setOut(new PrintStream(new File("C:/logs/log.txt"));
You might want to include a complete path instead of a local file reference.

Writing ONLY in the last line of a text box in Java SWT

I'm developing a program like Terminal in Linux or Command Prompt in Windows, but I'm developing it on Linux, and I don't care much about portability.
The program is written in Java, using SWT.
The user writes the commands in a text box, and when he presses 'enter' it reads this line and interpret it.
The problem is that the user writes his commands in a text box, so he is free to write at any place inside it.
I want to restrict him to write only in the last line of the text box. I don't want him to change the position of the cursor and writes any way.
Thanks in advance
I wrote a terminal-like control in .NET. It's not java-based but I think that you can reuse some of the logic in there:
http://wpfterminal.codeplex.com/
Be advised that I didn't mean to release this project so soon, I did it for you, so it might require a little cleaning maybe, as well as a few bug tracking. But anyway, it worked flawlessly for now.
You can see an example in this screenshot (terminal is integrated in a bigger project) :
http://images4.hiboox.com/images/4210/0a2809b63e05c3d0cac678962e0e3d5a.jpg
My solution
.
Basic mechanisms
Actually what I did was to define a lastPromptIndex integer, and everytime a user presses the ENTER key and a new prompt appears, this value is updated.
After that, it's simple, you just need to process any text input before the textbox validates the input. If the textbox caret was located before your lastPromptIndex, you need to raise an error (usually a beep sound) and you must invalidate the text input, so nothing is written in the textbox. I also automatically set the caret position to the end of the textbox, so the user can immediatly input some text.
Extensions
You can enable command completion by looking for an "UP key" input if the caret is before the prompt index, etc. What you need is just to process input events before they are sent to the textbox internal mechanisms. I don't know if SWT controls allow it, but I'm pretty sure they do, like any serious UI system.
Other solution
.
JTERM
I remember that a friend of mine used a terminal in a Java application, and in his sources, I found that if was a control from JTerm, have a look at it:
http://www.acordex.com/vtj/JTerm.html
Your could use 2 text widgets to simulate a terminal session.
One for the entry of the command, and the other for the display of the command and the response.
The entry text widget only needs to accept 70 - 80 characters or so.
The display text widget will scroll and show the most recent command and response.

Categories

Resources