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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:57:37+00:00 2026-06-11T06:57:37+00:00

In Swing, its possible to use HTML to style the words on a label.

  • 0

In Swing, its possible to use HTML to style the words on a label. E.g if I wanted a certain word on a label to be bolded or be of a different color, I could do that with HTML.

Is there an equivalent feature for SWT? Say if I had the words “A quick brown fox jumped over a lazy dog” as the text of a label, and I wanted to change the color of “fox” to be brown, how would I do that?

  • 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-11T06:57:38+00:00Added an answer on June 11, 2026 at 6:57 am

    If you really need a Label, you can use the code below. Otherwise I would suggest a StyledText (as mentioned in the comments):

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
    
        Label label = new Label(shell, SWT.NONE);
        label.setText("Blue and not blue");
    
        Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    
        final TextLayout layout = new TextLayout(display);
        layout.setText("Blue and not blue");
        final TextStyle style = new TextStyle(display.getSystemFont(), blue, null);
    
        label.addListener(SWT.Paint, new Listener() {
            @Override
            public void handleEvent(Event event) {
                layout.setStyle(style, 0, 3);
                layout.draw(event.gc, event.x, event.y);
            }
        });
    
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    
    }
    

    With StyledText it would look something like this:

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
    
        StyledText text = new StyledText(shell, SWT.NONE);
        text.setEditable(false);
        text.setEnabled(false);
        text.setText("Blue and not blue");
    
        Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    
        StyleRange range = new StyleRange(0, 4, blue, null);
    
        text.setStyleRange(range);
    
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use android UI components in a Swing app? For example,
I know it's possible to use HTML but what if you are loading the
I need some help with Java Swing components and its capabilities. I need to
What Swing class can I use to implement something like this? Add To List
I have a Java Swing application client that I want to use to consume
Possible Duplicate: Swing: How do I set a component height to the container's height?
I'm trying to use Scala/Swing to create a Table, one of whose columns is
I want to know that is it possible to send major Swing classes event/actionlisteners,
Possible Duplicate: Java Full Screen Program (Swing) -Tab/ALT F4 I've got a full screen
Is it possible to specify a color in a properties file that has an

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.