Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-13-2009, 08:33 AM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
rammurugesan is on a distinguished road
Arrow Display output in Jframe or JPanel
HI Everyone ,
I'm new to JAVA Swings ... i have few problem in displaying the output contents of my program in JFrame or JPanel .
Here is program flow ..

// few codes here
System.out.println("Output1 to be displayed in JFrame");
//few "if" and "while" statements
System.out.println("Output2 to be displayed in JFrame");

now my problem is , how do i display these two contents in Jframe or JPanel ...

i have no idea about this . someone plz help me in this
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-13-2009, 09:12 AM
Member
 
Join Date: Jan 2008
Posts: 10
Rep Power: 0
frejon26 is on a distinguished road
Default
To output to a graphical user interface requires a different sort of approach. There is no System.out.println in Swing because the "out" object of the System class is linked directly to the standard console screen. Anyway there are a lot of things you can do.

1. You can create an instance of JOPtionPane by calling one of its many static methods, like JOptionPane.showMessageDialog(null, "Message");
where message is what you would like your JFrame to display.

2. If you would like to print directly to the JPanel or JFrame you need to extend the JPanel or JFrame in your class declaration like,

Code:
import javax.swing.*;
public class MyFrame extends JFrame {

}
then you need to override the paint method of either the JPanel or the JFrame.

Here is a code example that will paint the X and Y coordinates of your mouse clicks on the back of a JFrame.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Intro extends JFrame{
	int x = 0, y = 0;
	Intro() {
		super("Writing on the JFrame");
		this.setBounds(100,100,300,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.addMouseListener(new MouseAdapter() {
		public void mousePressed(MouseEvent me) {
				x = me.getX();
				y = me.getY();
				repaint();
			}
		});
		this.setVisible(true);
	}
	//here is the overridden paint method.
	public void paint(Graphics g) {
		super.paint(g);
		g.drawString("Click the mouse inside the window.",40,45);
		g.drawString("X : " + x,40,60);
		g.drawString("Y : " + y,40,75);
	}
	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Intro();
			}
		});
	}
}
In reality though you would want to paint inside of a JPanel and hold the JPanel inside of a JFrame, rather than painting straight to the back of a JFrame Object.

Look into JOptionDialogs or extending JFrames or JPanels.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-13-2009, 01:50 PM
makpandian's Avatar
Senior Member
 
Join Date: Dec 2008
Location: Chennai
Posts: 253
Rep Power: 2
makpandian is on a distinguished road
Default
I think as a beginner the above examle may be tough for you.

Instead of that ,i give a short way for that.

Create a label into Frame and use setText() of Label to dispaly your Text.

But dont do this often.It is not a good practise.
__________________
Mak
(Living @ Virtual World)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-14-2009, 08:56 AM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
rammurugesan is on a distinguished road
Default
Hi Mak ,
Thanks for your reply ... my sample output as displayed in console is below ..
-----------------------------------
11:59:02 : JMS Card profile Timeout - 6
IRI DB Connection count has reached 23 @ 11:59:02
11:59:03 : Stale Connections - 2
----------------------------------------
These outputs are generated by System.out.println() function at different places in my code.
But when i make it display in a frame ... i can view oly the first line ... where as my other outputs are not visible as in console which is continous ...

My problem is i need to append all the output content in a single label and display it together (as in console).
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-14-2009, 09:11 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,393
Rep Power: 3
masijade is on a distinguished road
Default
See the API docs for JTextArea.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-14-2009, 09:11 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Are your following the way Mak explain? If so, if you call setText() set the text which you call at last. But the messup here is you get the first line not the last one.

Can you show your code segment how set the text on the label?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-14-2009, 09:22 AM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
rammurugesan is on a distinguished road
Default
Sure i'll refer the API Docs for JText area ...
now i'm able to display the entire contents in JFrame .. but every output is appended to each other on a single line without gaps as below ...

------------------------------------------
11:59:02 : JMS Card profile Timeout - 6IRI DB Connection count has reached 23 @ 11:59:0211:59:03 : Stale Connections - 2
-----------------------------------------
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-14-2009, 09:37 AM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
rammurugesan is on a distinguished road
Default
problem is i've concatenated all outputs to a string and displayed in frame as above ...
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-14-2009, 09:39 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,393
Rep Power: 3
masijade is on a distinguished road
Default
Then you didn't read the API docs well enough. Use the append method of JTextArea and add a newline character (\n) to each String.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-14-2009, 09:40 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,393
Rep Power: 3
masijade is on a distinguished road
Default
As far as using the label "method" you would have to use HTML style text and the "<BR>" tag.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-14-2009, 10:01 AM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
rammurugesan is on a distinguished road
Default
Thanks a lot Masijade ... i'm able to display the output in my desired way .... thanks..
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-14-2009, 10:03 AM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
rammurugesan is on a distinguished road
Default
TextArea append method gave me an idea after reading it in API completely...
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-14-2009, 02:45 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by rammurugesan View Post
TextArea append method gave me an idea after reading it in API completely...
Reading the API is must in any case. It's really helpful to you.

If you have found the solution for your question then mark the thread solved.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add JFrame inside JPanel niteshwar.bhardwaj Java 2D 8 12-13-2009 09:41 PM
problems with JPanel and JFrame v1nsai New To Java 13 04-08-2009 08:49 PM
can display image in JFrame? xCLARAx AWT / Swing 14 04-03-2009 08:02 PM
How to display image from byte array in JPANEL waqasdaar AWT / Swing 0 03-22-2009 01:11 AM
scroll a Jpanel in a JFrame nidhirastogi SWT / JFace 1 09-07-2008 04:42 AM


All times are GMT +2. The time now is 03:32 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org