Results 1 to 13 of 13
  1. #1
    rammurugesan is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    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

  2. #2
    frejon26 is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    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,

    Java 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.

    Java 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.

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    448
    Rep Power
    6

    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)

  4. #4
    rammurugesan is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    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).

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See the API docs for JTextArea.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    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?

  7. #7
    rammurugesan is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    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
    -----------------------------------------

  8. #8
    rammurugesan is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    problem is i've concatenated all outputs to a string and displayed in frame as above ...

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    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.

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    As far as using the label "method" you would have to use HTML style text and the "<BR>" tag.

  11. #11
    rammurugesan is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    :)Thanks a lot Masijade ... i'm able to display the output in my desired way .... thanks..

  12. #12
    rammurugesan is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    TextArea append method gave me an idea after reading it in API completely...

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote 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.

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •