Java displays misaligned text vertically

In my attempt to implement word-wrap for my JLabel's text, I referred to this question. The code snippet I used for this purpose is as follows:

label.setText("<html style=\"vertical-align:middle;\">"+label.getText()+"</html>");

However, the text is not displayed correctly by the label. For instance, if the original text was "the big bad wolf", it appears like this:

<html style="vertical-align:middle;">the big bad wolf</html>

Why is the label failing to apply the intended style?
UPDATE: To illustrate my objective further, here is a visual reference: https://i.sstatic.net/8za6S.png

Answer №1

Give this a try:

public String wrapTextWithHtmlTags(String...strings) {
    
    StringBuilder htmlText = new StringBuilder("<html>");
    htmlText.append("<div style = \"text-align: center;\">");

    for(String str : strings) {
        
        htmlText.append("<p>")
                .append(str)
                .append("</p>");
    }

    htmlText.append("</div></html>");
    return htmlText.toString();
}

Test it out with:

String text = wrapTextWithHtmlTags("line one", "longer line two", "even longer line three");
JLabel label = new JLabel(text);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The issue of CSS and PHP header include file not adjusting to device size and exceeding the full width of the screen

This included code is a snippet from a header file for a website. <head> <link rel="shortcut icon" href="favicon.ico"> <meta charset="utf-8" /> <title>Test Site | Foo The Bar </title> <link rel="stylesheet" href="csss_stan ...

Android app: continuously retrieve CDMA signal strength every second

Here's a straightforward question for you, as my search turned up no relevant information. I need to log the CDMA signal strength every second. Any helpful tips? I've come across many examples, but they all rely on the onSignalChange() method w ...

Guide on Updating Data with PHP and MYSQL in Android Applications

For my app development, I am utilizing PHP MySQL to update data. Despite receiving the message "Update Data Successfully," upon checking my database, no changes are reflected in the values. Logcat shows: Log entries here... Here is the PHP Script used ...

A minimalist dropdown menu using only HTML and CSS with an onclick function

I have been working on creating an onclick dropdown menu for mobile devices that should disappear when viewed on wider screens, but I've encountered an issue where the links in the menus are not clickable even though the @media query is set up correct ...

Enhance your product listings in Opencart 2.0 by adding supplementary images alongside the main product image

Can someone assist me with moving additional product images next to the main product image in the default opencart 2.0.1.1 theme? Currently, they are appearing below the product image and I would like them to be displayed alongside it instead. ...

Comparing the name and URL of a link using Selenium webdriver in C#

I am currently looking for ways to automate the testing of links on a Sharepoint site, ensuring they are connected to the correct URL. However, I have hit a roadblock when it comes to performing the comparison. After locating the links and adding them to ...

Height and Width Dilemma in Visuals

Can you help with changing image resolution using jQuery? I have a background image applied to the body using CSS/PHP. My goal is to make the image fill the entire window by applying the screen height and width, without using repeat style. The code I am c ...

Make sure all of the inner tags are properly contained within their respective containers

In my explanatory bubble, I aim to include text within the explain-header div as a container for my explanations: /*Explain Bubble*/ .explain-container { position: relative; overflow: hidden; max-width: 70vw; max-height: 50vh; background-c ...

Change the class of the div when the first div is selected

My goal is to switch the class of the #klapp element (from .klapp to .klappe) whenever a click event happens inside the #label-it container, including when the #klapp element itself is clicked. The challenge is that I am not able to use unique IDs for each ...

Initiate a page break at the beginning of the table in case there are not enough lines on the first page

Does anyone know how to ensure that a table starts on a new page when printing, rather than continuing at the bottom of the previous page? I have tables with repeating headers and many rows, and sometimes a new table will start at the very end of a page. ...

"Crafting a sleek card design using Material UI components in React JS

Exploring the world of material UI and looking to create a card with an image and footer. However, I'm facing challenges as there is no default option for adding a footer in the card. https://i.stack.imgur.com/xlxyb.png I want the image to be center ...

How to access bespoke attributes in Scene Builder using JavaFX?

I am on the hunt for a way to designate customizable properties for my custom controls. Jens Deters has already created some fantastic custom controls utilizing fontawesomefx for JavaFX. Once you import the jar files into Scene Builder, you can easily inc ...

Following the modification of the Context root, the JSP page is no longer functioning as expected

Recently, I developed a JSP page within the webapp that utilizes jquery, CSS, and Angular JS. The files jquery-1.12.0.js, angular.min.js, and TableCSSCode.css are all located in the same directory as the JSP page. Initially, my application context was set ...

Arrays in Javascript (correlating)

I'm having trouble figuring out what needs to be included in this code (I apologize that it's in German, it's an array corresponding to images for JavaScript.) function namenZuBildern(){ var bilder = new Array( "000227", "000228", " ...

Creating an object positioned to the right side of a div (div:right) is a matter of using CSS positioning properties

While we are familiar with pseudo-classes like :before and :after, have you ever wondered why there is no nav ul li a:left or :right? Do you think it's achievable? I'm open to using HTML5, CSS3, and JavaScript to make it happen. ...

Guide to displaying a template using Vue.js

Incorporating jQuery and vuejs version 2.3.4: https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js The code snippet below demonstrates my current setup: // Instantiating Vue. vueVar = new Vue({ el: '#content', ...

Restrict the width of CSS columns to the value of an assigned variable

I'm struggling with centering a one-row table within a div. Specifically, I'm having trouble adjusting the size of two columns to match the length of the content inside them (team name and round). The HTML code is: <div id="greeting"> ...

Guide to adjusting the zoom level when cropping an image

Looking for help on fixing the zoom size for image cropping. I've tried various solutions but haven't found the right one yet. The cropped image is too zoomed in and appears blurry. Any assistance would be greatly appreciated. Here's my curr ...

Exploring the implementation of mutual SSL on a website using Java language

Trying to write a Cucumber Test case in Java to confirm 2-way SSL support on a website has been quite challenging. Despite reading numerous articles and answers, I'm still unsure of the exact approach. I've managed to create a self-signed certifi ...

The div above the footer seems to be interfering with the styling of the footer. Is there a solution to

I am currently working on implementing a Help functionality for our users. In order to do this, I have placed a div right above my footer. However, I am facing an issue where the styling of my div is affecting the footer in ways that I cannot figure out. ...