Results 1 to 5 of 5
  1. #1
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default How to write multiline String in a JLabel

    How can i write a string that is shown in many lines in a JLabel. Or is there any orher way to write them?

    Also, similarly how to write tooltip text that spans across many lines. Not just one line

  2. #2
    Ada
    Ada is offline Member
    Join Date
    May 2007
    Posts
    8
    Rep Power
    0

    Default

    You could just try setting the string text to what you want, and use the \n command to break the lines up.

    ie.

    Java Code:
    String str = "This is \nmy example \nof a multi-line \nJLabel";
    JLabel strLabel = new JLabel (str);

  3. #3
    levent Guest

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
     
    public class MultilineLabel {
    
        public static void main(String[] args) {
            String str = "This is \nmy example \nof a multi-line \nJLabel";
            JLabel strLabel = new JLabel (str);
            display(strLabel, "Hmmm...");
        }
     
        public static void display(JComponent comp, String title) {
            JFrame f = new JFrame(title);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(comp);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }

  4. #4
    mikeeve is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    I know this is an old thread, but, since the original poster did not followup, I copied and pasted the code given by levent into NetBeans and ran it. The newlines did NOT appear in the label. Perhaps this worked circa June, 2007, but it doesn't work with Java SE 6.10.

    Edit:

    goto velocityreviews forums multiline-jlabel (wasn't allowed to include link)

    shows how to do this using HTML. one is to use only html formatting including <br> in place of \n, another is to use standard C-format codes and use the <pre></pre> tags.
    Last edited by mikeeve; 02-13-2009 at 01:33 AM.

  5. #5
    akshaysahu is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Cool Multiline Text in a JLabel

    The direct procedure of writing a multi-line text in a jlabel is:

    JLabel label = new JLabel("<html>First Line<br>Second Line</html>");

Similar Threads

  1. Replies: 4
    Last Post: 03-25-2008, 01:16 PM
  2. Replies: 2
    Last Post: 03-25-2008, 06:49 AM
  3. Problems with JLabel 2
    By geork in forum New To Java
    Replies: 2
    Last Post: 02-03-2008, 09:40 PM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 02:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 03:19 PM

Posting Permissions

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