Java - Changing Font Size in HTML JTextPane with CSS Styling

When utilizing the following code snippet:

Action sizeAction = new StyledEditorKit.FontSizeAction(String.valueOf(size), size);

The button associated with the action adds the HTML tags:

<FONT SIZE="5"> My text here </FONT> 

Utilizing these tags leads to an incorrect representation of font size when other functions are performed. Instead, I aim to use a style tag to adjust the font size, such as:

<FONT STYLE="font-size: 16pt;"> My text here </FONT>

Is there a way to modify the TAGS inserted by the FONTSIZE action?

Answer №1

Discovered an improved solution for resolving the issue! Simply introduce a new Action and modify the method of adding attributes.

class TextSizeAction extends HTMLEditorKit.TextSizeAction {
    private int fontSize;

    public TextSizeAction(int fontSize) {
        super("text-size-" + fontSize, fontSize);
        this.fontSize = fontSize;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        JEditorPane textEditor = getEditor(event);
        if (fontSize > 0) {
           MutableAttributeSet attribute = new SimpleAttributeSet();
           attribute.addAttribute(StyleConstants.FontSize, fontSize + "pt");
           setCharacterAttributes(textEditor, attribute, false);
        } else UIManager.getLookAndFeel().provideErrorFeedback(textEditor);   
    }
}

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

Is there a Java package available for performing matrix calculations with complex numbers?

Looking to refactor my signal processing code from C++ to Java, I come across matrices with complex numbers (numbers containing imaginary components). My main goal is to calculate the inverse of an NxN complex matrix and also determine the principal eigenv ...

how can a select dropdown be dynamically displayed based on the previous selection?

If the first dropdown is set to "Professor" I want to display a second dropdown, but if it is set to "Student" then I do not want to display the second dropdown. function checkPrivilege() { var privilege = document.getElementById("permisija5").value; ...

Navigating through history using the pushState method and incorporating back and forward buttons

I am trying to implement back and forward buttons functionality with this ajax method The code is working well, and the URL in the browser is changing as expected However, when I click on the back and forward buttons, nothing happens (function(){ ...

The hamburger menu unexpectedly appears outside the visible screen area and then reappears at random intervals

My website has a hamburger menu for mobile devices, but there's a problem. When the page loads on a small screen, the hamburger menu is off to the right, causing side scrolling issues and white space. I thought it might be a CSS problem, but after exp ...

step-by-step guide to disabling grayscale mode automatically

In recent times, a trend has emerged in Thailand where many websites are appearing in grayscale. This is achieved through the use of -webkit-filter: grayscale(100%), filter: grayscale(100%) and similar filters. Although it is possible to revert these webs ...

Unique background image is assigned to each div sharing the same class

Seeking a way to assign unique random backgrounds to each div with the .item class. Initially attempted using PHP and CSS, for example: <?php $bg = array('bg1.jpg', 'bg2.jpg', 'bg3.jpg', 'bg4.jpg', 'bg5.jpg ...

div within a fixed width container with 100% width inside

Is there a way to ensure that div B has a width of 100% when nested inside Div A, which already has a fixed width? .divA{width:600px; margin:0 auto;} .divB{width:100%; height:20px; background-color:black} .filler{height:800px; background-color:grey} ...

Try placing the div class above the navbar in Bootstrap

I've created these fork me ribbons in CSS with the intention of placing them on top of the navbar, similar to the Github images. Here is how they currently appear: http://jsbin.com/womib/1 .ribbon { background-color: #F38419; overflow: h ...

Trouble with SCSS Nesting

Can anyone help me with styling an H3 and a p tag within a div? I'm facing an issue where the p tag is not being styled in this code block: .marketing-single-page-titles{ h3 { margin: 0; padding: 0; p { margin: 0; padding: 0; position: rel ...

What are some methods for utilizing the "name" attribute within React components?

About My Coding Environment Utilizing TypeScript and ReactJS The Issue with Using name as an Attribute Encountering the following error: Type '{ name: string; "data-id": string; "data-type": string; }' is not assignable to ...

Hide the #loading element when the <input> field is devoid of content

Sample - jsfiddle.net/75CqW/ Upon clicking the Submit button, the loading div is displayed even when the input field is empty. I attempted to add "required" to the input field to prevent the loading div from showing when it's empty, but it's st ...

I'm trying to retrieve information from openweathermap in order to show it on my app, but I keep running into an error that says "Uncaught RangeError: Maximum

I recently created a div with the id 'temporary' in order to display data retrieved from the openweathermap.org API. However, I am encountering an error in the console and I'm not sure why. This is my first time working with APIs and I would ...

Identifying the Ajax request URL

I am working on a project where I have an HTML document that retrieves content from a PHP file using an AJAX call. The key portion of my code can be seen below: default.html : /*more code above*/ var PHP_URL = "content.php"; var Content = document.getEle ...

Elevate Your Flipclock.js

I am currently working on a website that utilizes flipclock.js, but I am facing some challenges in enlarging it to occupy more space on the page. Is there anyone who knows how to upscale or increase its size? ...

Navigate to the element and retrieve a null offset

Currently, I am attempting to retrieve an element's ID from a select box and then scroll to that specific element using the following code snippet: // move to services $(function () { $("select.jump").change(function() { var selected = $( ...

Analyzing data visualization within CSS styling

I have the desire to create something similar to this, but I am unsure of where to start. Although I have a concept in mind, I am struggling to make it functional and visually appealing. <div id="data"> <div id="men" class="shape"></di ...

Display or conceal the nearest element that was clicked

http://jsfiddle.net/KevinOrin/xycCx/1/ jQuery('.happening-main-text .readmore').click(function() { jQuery('.divmore').toggle('slow'); return false; }); ...

Ways to limit the extent of inline CSS styling?

I am working on a project where I need to apply unique CSS to each item in a list dynamically. Each item will have its own set of CSS rules specifically tailored to its elements. <div id="thing1" class="vegas"> <style> p { font-size: ...

Is there a way I can create a slight gap between the icon and the text in this area?

After learning Reactjs and JavaScript, I have encountered a problem that I need advice on. In the image provided, the "Sign in" text does not have proper spacing between the icon and the text, unlike the other menu items. https://i.stack.imgur.com/b663b. ...

Creating a dynamic design with a responsive background image and three columns of engaging content placed on top using Bootstrap

I'm struggling to translate a design mockup into Bootstrap 3.3.6 Here is an image showcasing the concept I am aiming for: https://i.sstatic.net/1jZmh.png The blue background represents an image, with grey text boxes overlaid on top (one of which in ...