Results 1 to 8 of 8
  1. #1
    DrKilljoy is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default Displaying text in a JPanel

    I've just about finished my first GUI, but I'm having a problem displaying text. I've been putting some drawString() calls in my GUI's main repaint() method as a placeholder, but I'd like to have some control over the text's color, font, etc. I figured clever use of a JTextArea would get me what I wanted, so in the initialization I made one called "message", then put the following modifiers on it:

    Java Code:
    message.setFocusable(false);
    message.setText("");
    message.setEditable(false);
    message.setOpaque(false);
    message.setFont(gamefont);
    message.setForeground(Color.black);
    I'm able to edit the contents just fine, and the text shows up, but it's always in the JPanel's top-left corner and is always covered over by the game's graphics. I tried to remedy this by using message's setAlignmentX and setAlignmentY methods, but these don't seem to help. Adding "message.repaint();" to the end of the GUI's main repaint method didn't fix the problem of the game graphics burying the text area, either.

    Could someone explain what I'm doing wrong and what a good way to display text is?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    There are several ways to draw text on your JPanel:

    a) as you already discovered use the Graphics.drawString( ... ) method in the paintComponent( ... ) method of your panel. You can draw your text anyway you like.

    b) use a JLabel and position that label on your JPanel (optionally use a LayoutManager); a JPanel can also handle html text, e.g. "<html>line one<br>line two</html>" displays two lines of text.

    c) use a JTextComponent (as you already tried) and position the entire thing at the correct location in your panel. Some of these components can also handle html text (read the API documentation).

    kind regards,

    Jos

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    d) Don't add the label to the component/panel that displays the game's graphics. Add both to a common container ... possibly with a BorderLayout.

    db

  4. #4
    DrKilljoy is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    Thanks for the quick responses. How do I change how the text is drawn with the drawString() method? The only parameters it takes are string and position.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Depends what you mean by
    how the text is drawn
    Take a look at the API for Graphics and see what properties you can set...

    db

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DrKilljoy View Post
    Thanks for the quick responses. How do I change how the text is drawn with the drawString() method? The only parameters it takes are string and position.
    You can change the font or drawing color or scale, translate or shear the text image. Read the API documentation for the Graphics and Graphics2D classes.

    kind regards,

    Jos

  7. #7
    su5
    su5 is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default to display contents from vector to jpanel

    how to display contents from a vector onto a jpanel?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Quote Originally Posted by su5 View Post
    how to display contents from a vector onto a jpanel?
    Step 1: Hijack an unrelated, old dead thread
    Step 2: Ask the question in as vague terms as possible
    Step 3: Don't show what effort has already been made in respect of the problem.

    db

Similar Threads

  1. displaying JSplitPane in JPanel
    By sandysm in forum AWT / Swing
    Replies: 4
    Last Post: 07-26-2010, 09:13 AM
  2. Jpanel and displaying graphics
    By jdsflash in forum New To Java
    Replies: 6
    Last Post: 11-21-2009, 01:14 AM
  3. Displaying text in a tabel
    By pele in forum SWT / JFace
    Replies: 5
    Last Post: 05-28-2008, 07:05 AM
  4. Displaying data into text area
    By abhiN in forum New To Java
    Replies: 1
    Last Post: 01-22-2008, 10:30 AM
  5. Displaying text box on MIDlet form
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-22-2007, 10:06 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
  •