Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 591
  • Last Modified:

Show output in app window

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
gbcbr
Asked:
gbcbr
  • 9
  • 6
  • 4
2 Solutions
 
for_yanCommented:
Don't fully understand, maybe you want to use JOptionPane.showMessageDialog method and show messages there instead of printing them to System.out
0
 
for_yanCommented:
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
 
gbcbrAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
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
 
gbcbrAuthor Commented:
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
 
for_yanCommented:
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
 
gbcbrAuthor Commented:
But I don't understand how messages will come to this TextArea, where is method to get this messages?
0
 
for_yanCommented:
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
 
gbcbrAuthor Commented:
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
 
for_yanCommented:

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
 
gbcbrAuthor Commented:
>>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
 
objectsCommented:
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
 
gbcbrAuthor Commented:
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
 
objectsCommented:
same way you add scroll bars to any component

        this.getContentPane().add(new JScrollPane(jTextArea1), null);
0
 
gbcbrAuthor Commented:
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
 
objectsCommented:
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
 
gbcbrAuthor Commented:
Perfect and clear solution
Thanks a lot!
0
 
gbcbrAuthor Commented:
No comments.
Just excellent!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now