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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:57:35+00:00 2026-06-13T02:57:35+00:00

I have created a label with overriding its paintComponent (to paint it in a

  • 0

I have created a label with overriding its paintComponent (to paint it in a different format with an arc) and located them on a panel. Everything worked fine, however the labels which are located in same location (by .setlocation) are creating problems.

Lets say I have 3 labels at the same location with different shapes. A-B-C
A is first created, then B is created and lastly c is created.
When I click on A, my function paints A and displays “youve clicked A”

But when I click on B or C, it acts like i have clicked on A.

(I’m adding labels in a panel stacked in scroll pane )

Here is the code where i make labels

for(int i=0;i<rowcount;i++){
        arcstart = Rawazimuth(azimuth[i]);
                    //custom JLabel to paint it like an arc
        MyLabel FLabel = new MyLabel(100, 50, (int)arcstart,cellid[i],lon[i],lat[i]);
        FLabel.setOpaque(true);
        Dimension LabelSize = new Dimension( 100,50);
        FLabel.setSize(LabelSize);
                    //locate the JLabel to labels location. it might be same or not
        FLabel.setLocation(lon[i], lat[i]);

        MyPanel.add(FLabel);    
}

this is my custom jlabel class

public MyLabel(int W, int H, int start,int outcellid,int lonn, int latt) {
    addMouseListener(this);
    arcsizeW = W;
    arcsizeH = H;
    arcstart = start;
    cellid = outcellid;
    clicked = false;
    lon = lonn;
    lat = latt;
}

@Override
public void paintComponent(Graphics g){
    // true if button is clicked, so paint acordingly
    if(clicked ==true){
       g.setColor(dummyg.getColor());  
   }else{
       g.setColor(Color.blue);
   }
  // draw the arc
   g.fillArc(0, 0,arcsizeW , arcsizeH, arcstart, 60);
}

 //if cell is clicked, change its color to red and print its cellid
@Override
public void mouseClicked(MouseEvent e) {

    System.out.println(cellid);

     dummyg = this.getGraphics();
     dummyg.setColor(Color.red);
    this.paintComponent(dummyg);
    clicked = true;
}// other listener stuff}

so how do i prevent this?
i think i can use jlayerpane, but i will need at least 4 layers ( dunno if its ok ).

  • 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-13T02:57:37+00:00Added an answer on June 13, 2026 at 2:57 am

    I have solved my problem.

    The real problem that i found was, Java is interpreting each component with their x,y location and width & height. So even if your label does not overlap ( graphically ) they can be interpreted as same component.

    To solve my problem,
    I found the angle between mouse click point and component center; then calculated the closest component and reached it.

    Layers and such doesn’t work in this situation, the only other way around is possibly overriding the contains method for the actualy shape.

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

Sidebar

Related Questions

I have created a RibbonButton and given its Tooltip this way <ribbon:RibbonButton Label=My Label
I have created a label , and now i want to change its postion
I have created an iframe which contains the label, powered by MyWebsite.site The iframe
I have created a user control (link) which has a label and a button.
In my app i have created one custom class for Label.Now i want to
Hi i want to create a multiline Custom Label in Blackberry I have created
I have an image label created like this. Painter painter = new Painter() {
i have created a label and set tap gesture on it but it show
I have created a label and a dropdownlist. The label has the dropdownlist as
I have created a dummy mail to create Empty Label in Google APPs mail.

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.