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 6745851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:15:12+00:00 2026-05-26T12:15:12+00:00

Im working on a Marquee JPanel ,and im using a JLabel to scroll Text

  • 0

Im working on a Marquee JPanel ,and im using a JLabel to scroll Text within the JPanel by Decreasing the Y of JLabel inside the JPanel .

The issue is i need a Specific Width of JLabel where Height is not important because it will scroll the Text until the Y of JLabel is > than -JLabel.Height which means JLabel last line of text is at Top of JPanel .

So how can i calculate the Height which can hold a sort of Text based on a specific value of Width .

eg.
If Width of JLabel is 50 than the Height should be that value that can fit the text within if text is “Bla bla bla some text over …” and font is “Serif”, Font.ITALIC, 20

  • 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-05-26T12:15:12+00:00Added an answer on May 26, 2026 at 12:15 pm

    Despite that this is answered, I thought I’d offer an alternative approach. This alternate boils down to ‘put the text it in an HTML formatted label’. The advantage of this approach is that line breaks are normally handled automatically, without having to worry about where to break each line. E.G.

    import javax.swing.*;
    
    class HeightOfFixedWidthText {
    
        public static void show(String s) {
            JLabel l = new JLabel(s);
            l.setSize(l.getPreferredSize());
            JOptionPane.showMessageDialog(null, l);
            System.out.println(l.getSize());
        }
    
        public static void main(String[] srgs) {
            String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus.";
            String html1 = "<html><body " +
                "style='font-family: Serif; font-style: italic; font-size: 20px; padding: 0px; margin: 0px;" +
                " width: ";
            String html2 = "px'>";
            String html3 = "</body></html>";
    
            show(html1+"200"+html2+s+html3);
            show(html1+"300"+html2+s+html3);
        }
    }
    

    Output

    java.awt.Dimension[width=260,height=884]
    java.awt.Dimension[width=390,height=544]
    

    Caveat

    1. There is probably some (more) padding in the body of that HTML that needs to be removed in order to get the smallest size needed to display the the text. Left as an exercise for the user.
    2. Swing’s HTML formatting is notorious for getting the ‘baseline’ of text wrong. This is likely to count if the text content includes things like sub-scripts which drop below the usual baseline of the text. It seems as though that won’t be a problem in this use-case.
    3. I only just noticed that though the width is specified as 200 then 300, the result comes back as 260 & 390. I could understand if that difference was constant between the two (extra padding to remove), but am quite mystified as to why it is different for each label.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Marquee is not working in textview.I placed the textview inside the TableLoyout between two
How to add marquee behaviour to text of JLabel ? I have tried this
I use below marquee in my webpage, <MARQUEE WIDTH=100% DIRECTION=LEFT BEHAVIOR=SCROLL Loop=auto BGColor=White onmouseover=this.stop()
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.
I am using C# and Windows Forms. I have a normal progress bar working
I was trying to show a text gradually on the screen (like marquee). e.g.
I'm using the jQuery Cycle plugin for a slider - I need to complete
Working in C#, I need to find all local peaks in a List of
My <marquee> tag is working perfectly in ie6 and ie7, but in ie8 it

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.