Solved

Show output in app window

Posted on 2011-02-21
19
581 Views
Last Modified: 2012-05-11
I have deployed my app, but I lost current control for some parameters of app because I still not developed some information fields. Please advice how I can temporarily translate to my JFrame output from " System.out.println " window like I have in IDE until I'll finish all necessary information fields?
0
Comment
Question by:gbcbr
  • 9
  • 6
  • 4
19 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34944235
Don't fully understand, maybe you want to use JOptionPane.showMessageDialog method and show messages there instead of printing them to System.out
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34944281
You don't need to be in IDE to see system.out messages - you shiuld see them in the window from which you started your app by the Java command. What OS are you using ?
0
 

Author Comment

by:gbcbr
ID: 34944306
At the moment I have all messages from  System.out.println (" hbdHBH " + hdh) in my Output window in NetBeans.
I need to create the same window into deployed app, so I can seen(by easy way) all control markers.
I use Mac and I deploy app with Jar Bundler.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34944390
I'm no specialiost in Mac but look int jar Bundler but look atv it morr, there should be a way to show Java console - all such thing usually have an option to see it
0
 

Author Comment

by:gbcbr
ID: 34944436
No, you don't understand my question.
I want to create separate JFrame in my app and translate there all System.out.println messages.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34944597
No problem - create separate JFrame, place TextArea in that frame and istead
of System.out.println() just do

JFrame f = new JFrame();
....create and addTextArea

and at the time of printing say
String s = txtArea.getText();
S+= new Text
txtArea.setText(s);

or there is probably even method to append to TextArea, I ususally do it this way.

So there is no prtoblem with that.
0
 

Author Comment

by:gbcbr
ID: 34944682
But I don't understand how messages will come to this TextArea, where is method to get this messages?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34944777
If you want to see the System.out.println messages in the deployed application, I think
there are two ways - one way is to modify your application - to create
the frame, the textArae, place textArea into the frame and modify your code so that
every time your code is using System.out.println(...) alongside with it
you'll add the smae string which you print to sysyem.out yo'll add this very string
to the textArea - so you'll see your text growing in your window as your aplication goes on.

If the purpose is to see these messages, say for debugging purposes, then
I'd think much more prefereable way is to find the way to show Java console.
All tools which package and start java applications for you on different
systems usually have an option to display the console - and in this case you'll not need
to modify your code for that.
On Windows or Unix where you can start your Java app from command line "java...classnmae"
this window will usually be your console output. On Mac it may be different,
but I'm sure you can also start your java app from command line.
If you explain waht is the purpose of seeing this messsage, then I'd probably
understand sometghing better, as a rule console output is much more convenient
than to modify your code.





 
0
 

Author Comment

by:gbcbr
ID: 34945219
I need first option, just advise how to make
>>every time your code is using System.out.println(...) alongside with it
you'll add the smae string which you print to sysyem.out yo'll add this very string
to the textArea - so you'll see your text growing in your window as your aplication goes on.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 47

Expert Comment

by:for_yan
ID: 34945328

Well, somehwere in the beginning of your code, you write something like that:





Frame f = new Frame("output text");
TextArea txtArea = new TextArea(60,80);
f.add(txtArea);
f.setSize(600,600);
f.show();

If your application constructs its main initial window
in the constructor and then waits for the mouse input,
then you can create this Frame and TextArea in the constructor
before you instantiate your main window

You may also want to have this Frame and TextArea as your
instance variables, so you can refere to them from all your methods actions.
In this case, instead of the code above - you declare

Frame and TextArea  before your constructor as instance

Frame f;

TextArea txtArea;

then inside constructor:

f = new Frame...
txtArea = new ...  and so on according to the code above

In this way you can then print to txtArea (appensd new lines to it) from any place in your calss








You can of course use JFrame, but if it is just for you and you do not care
much about good looks, then awt (Frame) is alittle bit simpler


Then along your calculations or flow of your program anywhere
where you now have System.out.println(someString);
you also add such code:
.....
System.out.println(someString); <--- that was your old code;
You add what is below:
String s = txtArea.getText();
s += someString + System.getProperty("line.separator");
txtArea.setText(s);







0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34946873
0
 

Author Comment

by:gbcbr
ID: 34949243
>>see if this helps
Perfect solution, exactly what I need <<but often we’re in a rush and we end up writing to stdout because its simpler at the time.>>
Just please advise how to insert this redirection in my JFrame:
package control;

import java.awt.Dimension;
import java.awt.Rectangle;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

import javax.swing.JFrame;

public class RedirectOutput extends JFrame {
    private JTextArea jTextArea1 = new JTextArea();

    public RedirectOutput() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout( null );
        this.setSize(new Dimension(517, 295));
        this.setTitle( "RedirectOutput" );
        jTextArea1.setBounds(new Rectangle(15, 10, 470, 230));
        this.getContentPane().add(jTextArea1, null);


    }
    
}

Open in new window

Thank you
0
 
LVL 92

Expert Comment

by:objects
ID: 34949333
Save the redirect code as a new class
And rename  your class, and add call to sendTo as shown below

public class ConsoleFrame extends JFrame {
    private JTextArea jTextArea1 = new JTextArea();

    public ConsoleFrame() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        RedirectOutput.sendTo(jTextArea1);
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout( null );
        this.setSize(new Dimension(517, 295));
        this.setTitle( "RedirectOutput" );
        jTextArea1.setBounds(new Rectangle(15, 10, 470, 230));
        this.getContentPane().add(jTextArea1, null);


    }
    
}

Open in new window

0
 

Author Comment

by:gbcbr
ID: 34950658
Excellent!
This is exactly what I was asked for.
Please publish second class on your tutorial site also, so visitors, like me, will have complete solution.
Just last question, how to make TextArea1 scrollable and show all the time last line.
0
 
LVL 92

Expert Comment

by:objects
ID: 34956202
same way you add scroll bars to any component

        this.getContentPane().add(new JScrollPane(jTextArea1), null);
0
 

Author Comment

by:gbcbr
ID: 34956385
when I change >>this.getContentPane().add (jTextArea1, null); << to
>>this.getContentPane().add(new JScrollPane(jTextArea1), null);<<
it show only empty panel.
When I change back shows terminal lines, but they go down, to the bottom of JFrame and I don't see them.
Please advice
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 34957786
you need to set the bounds, or better use a LayoutManager


        this.getContentPane().setLayout( new BorderLayout() );
        this.setSize(new Dimension(517, 295));
        this.setTitle( "RedirectOutput" );
        this.getContentPane().add(new JScrollPane(jTextArea1), null);
0
 

Author Comment

by:gbcbr
ID: 34958715
Perfect and clear solution
Thanks a lot!
0
 

Author Closing Comment

by:gbcbr
ID: 34958725
No comments.
Just excellent!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now