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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:15:34+00:00 2026-06-17T17:15:34+00:00

I have a JLabel inside a JPanel. I’m using Netbeans IDE for the GUI

  • 0

I have a JLabel inside a JPanel. I’m using Netbeans IDE for the GUI of my java program (free design, and no Layout Manager).

At runtime I need to highlight a part of my JLabel. Highlighting a part of JLabel can be done as said here : How to highlight part of a JLabel?

I don’t know how to do that in run time, I mean I already have a JLabel, now how can I override it’s paintComponent() method for that purpose ?

  • 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-17T17:15:35+00:00Added an answer on June 17, 2026 at 5:15 pm

    Here is an illustrative example of the answer given by Tikhon Jelvis in the post you mentioned, all you need is to add some fields (in this example start and end) to indicate the regions to be highlighted, and use a method (highlightRegion in this example) to set these fields:

    import java.awt.Color;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class Main
    {
        public static void main(String[] argv)
        {
            JFrame frame = new JFrame("Highlighting");
            frame.setSize(300, 100);
    
            Label label = new Label();
            label.setText("Money does not buy happiness");
            label.setVerticalAlignment(JLabel.TOP);
            label.highlightRegion(3, 15);
    
            frame.add(label);
            frame.setVisible(true);
        }
    
        static class Label extends JLabel
        {
            private static final long serialVersionUID = 1L;
            private int start;
            private int end;
    
            @Override
            public void paint(Graphics g)
            {
                FontMetrics fontMetrics = g.getFontMetrics();
    
                String startString = getText().substring(0, start);
                String text = getText().substring(start, end);
    
                int startX = fontMetrics.stringWidth(startString);
                int startY = 0;
    
                int length = fontMetrics.stringWidth(text);
                int height = fontMetrics.getHeight();
    
                g.setColor(new Color(0x33, 0x66, 0xFF, 0x66));
                g.fillRect(startX, startY, length, height);
    
                super.paint(g);
            }
    
            public void highlightRegion(int start, int end)
            {
                this.start = start;
                this.end = end;
            }
        }
    
    }
    

    Notice the use of label.setVerticalAlignment(JLabel.TOP); to simplify things. If you don’t use it then you need to do extra computations to determine the exact vertical location of the region to be highlighted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using NetBeans, and I have a class that extends to JDialog, inside
I have a subclass of JLabel that forms a component of my GUI. I
I have a JScrollpane that has a JPanel on the inside (and the panel
I have a class which extends JPanel, inside this panel i have added a
I have a very simple JLabel with a quite long text. The parent layout
I have a JDialog and inside it I have a JPanel that uses FlowLayout
I have a simple program for server/client communication using sockets. The server class contains
i have a jdialog with a jpanel inside it.I have jlabels(pictures) on the jpanel.How
I want to have words inside a Jlabel text that are clickable, and I
I have problem with creating border for an icon in JLabel. I have JPanel

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.