Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:13:52+00:00 2026-06-17T18:13:52+00:00

I trying to some formatting for my label in java swing. If user enter

  • 0

I trying to some formatting for my label in java swing. If user enter :

    This
    is
    testing.

And I store this into database and retrieve out. How can I make this prevent from auto joining into one sentence. For example, I want this to be shown on my label which is scrollable :

    This
    is
    testing.

instead of Thisistesting. I do not know what is this called. Is this the line wrap method? I added this : jLabel_reply.setLineWrap(true); into my code but no luck. The sentence is still auto joined together. Any guides? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:13:53+00:00Added an answer on June 17, 2026 at 6:13 pm

    JLabel does not display itself on multiple lines by default.

    You have two options here:

    1) Use a non-editable, non-opaque JTextArea

    2) Use HTML inside your JLabel and replace all your line separators with <br>

    Basic example illustrating my saying:

    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class TestMultipleLineLabel {
    
        private static final String TEXT = "This is\na text\non multiple lines";
    
        protected void initUI() {
            JFrame frame = new JFrame(TestMultipleLineLabel.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
    
            JLabel label = new JLabel();
            label.setText(getHTMLText(TEXT));
            label.setFont(label.getFont().deriveFont(Font.PLAIN));
            panel.add(label, gbc);
            JTextArea textArea = new JTextArea();
            textArea.setEditable(false);
            textArea.setLineWrap(false);
            textArea.setOpaque(false);
            textArea.setWrapStyleWord(false);
            textArea.setText(TEXT);
            panel.add(textArea, gbc);
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
        }
    
        private String getHTMLText(String text) {
            return "<html>" + text.replace("\n", "<br>") + "</html>";
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestMultipleLineLabel().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to echo out some JavaScript, but I can't get the formatting
Trying to compare some dates in java but can't get the formatting right, where
I'm pulling in a google news feed, then trying to take out some mal-formatting
I am trying to do some text formatting with sql (MS SQL server 2008).
I'm trying to do some number formatting and I'm looking for an elegant way
I am trying to have a macro that will do some formatting. I need
I'm trying to remove wiki formatting from some text so it can be parsed.
I am trying to do some additional checks and formatting on certain fields of
Recently I was trying some practice programs in python and I came across this
i was trying to do some simple text formatting using JEditorPane but then as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.