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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:30:59+00:00 2026-05-11T20:30:59+00:00

I am working on a GUI where the JComponents are stamped on the screen.

  • 0

I am working on a GUI where the JComponents are “stamped” on the screen. In other words, the actual components aren’t displayed, but images of the components. This is a graph, where the nodes of the graph are custom Swing components – or rather stamped images of Swing components.

Now I want to display tooltips for specific components within my nodes.

To do this, I create a JComponent that is identical to the one displayed and using the mouse x and y values, I ask findComponentAt() for the proper component. This doesn’t work very well. If I reuse JComponents for the nodes, it gets confused if I try to get a tooltip for a node that is a different size than the last one painted. If I create a new JComponent for each node and a new one when calculating the tooltip, the initial size of the new one is 0,0. I can set the size using the getPreferredSize() calculation, but that still doesn’t work. The root JComponent (a JPanel) has the right size, but none of it’s children have any size yet.

A sample of the tooltip calculation code:

// Get a component that matches the stamped component
JComponent nodeComponent = getNodeComponent();

// These next two lines get the size right      
nodeComponent.setSize(nodeComponent.getPreferredSize());
nodeComponent.revalidate();

Component componentTop = nodeComponent.findComponentAt(relativeX, relativeY);

componentTop comes back as the root JComponent no matter what x and y values it is passed.

So is it possible to get Swing to properly calculate the size and locations of the JComponents without actually painting them?

  • 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-05-11T20:30:59+00:00Added an answer on May 11, 2026 at 8:30 pm

    I found the answer myself. The key problem is that Swing doesn’t want to layout a component unless that component has a proper parent. So, I changed my code to this:

        parentComponent.add(nodeComponent);
    
        // Set the node's size and validate it so it's laid out properly
        nodeComponent.setBounds((int)realizer.getX(), (int)realizer.getY(), (int)realizer.getWidth(), (int)realizer.getHeight());
    
        nodeComponent.validate();
    
        // Now we can properly find the child component under our mouse
        Component componentTop = nodeComponent.findComponentAt(relativeX, relativeY);
    
        // Now remove it from the view
        parentComponent.remove(nodeComponent);
    

    And that works like a charm. You should be able to use a similar process to find child components in JLists or JTables (which also use this Renderer pattern).

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

Sidebar

Related Questions

I am working on Java GUI project. I have several components on the swing-based
I am working on a WinForm Application. The Form has many fields/components but is
I'm working on a GUI framework, where I want all the elements to be
I'm working on a GUI for a card game and am using the ACM's
I am working on an application/GUI created with Win32/ATL . So there is UI
I'm working on a simple GUI Python script to do some simple tasks on
I am working on a small basic GUI program that gets the files from
I was working on a simple GUI script yesterday using jdbc in eclipse. When
I'm working on a simple GUI code editor in Python, and I want to
I've recently begun working on a project regarding GUI building using some form of

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.