Solved

Gettting the bounds for a JTree node

Posted on 2002-05-24
5
232 Views
Last Modified: 2010-03-31
Hi,

I am trying to render the JTree nodes with a JPanel

the Panel has a JLabel and a JButton.

obviously the JButton is just a non functional thing

Hence when the user double clicks on the selected node
only if he clicks on the button I want to simulate a button click and do something.

The question is I need to get the bounds of the button
only if the user clicks within the  button bounds
the method "canEditImmediately" will return true

How do I do this

I tried something like this

The funny thing is it works on Jdk1.3 but not on 1.4


protected boolean canEditImmediately(EventObject e)
    {

        boolean rv = false;  // rv = return value

        if (e instanceof MouseEvent)
        {
            MouseEvent me = (MouseEvent) e;

            path = tree.getPathForLocation(me.getX(),
                                                    me.getY());
                       rv = inButtonBoxHitRegion(me);
        }

        return rv;
    }//canEditImmediately()



  public boolean inButtonHitRegion(MouseEvent e)
    {

        boolean rv = false;

        // offset and lastRow DefaultTreeCellEditor
        // protected members
        Rectangle bounds = tree.getRowBounds(lastRow);


        Dimension buttonOffset = rendererI.getButtonOffset();

        bounds.translate(offset + buttonOffset.width,
                         buttonOffset.height);



        rv = bounds.contains(e.getPoint());



        //temporarily, needs to be fixed
        rv = true;
        return rv;
    }//inButtonHitRegion()


This is the renderer method to get the offset

 public Dimension getButtonOffset()
    {


        Graphics g = nodePanelG;

        int xoffset = 0;
        if (g != null)
        {
            try
            {
                FontMetrics fm = g.getFontMetrics();

                xoffset = fm.stringWidth(getText()) +
                        strut.getPreferredSize().width;


            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                g.dispose();
            }
        }


        return new Dimension(xoffset, 0);
    }



What am I doing wrong????????

please help

This is similiar to example in Graphic Java Mastering the JFC Volume ii : Swing , chapter 20 example
0
Comment
Question by:sanjay_thakur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7033767
Quite possibly a regression bug in 1.4 if it works ok in 1.3. Add some debug to see whats returning the incorrect value.
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 7044936
Hi Objects,

Actually I am trying to get the test cases but it might require changes in my code.Might take some time.

But anyway do you think there is a better way to get the bounds of the jbutton with which the nodes are rendered

Basically I need to get the location of the JButton
and if the mouse is within the bounds of the JButton
then cell editing is allowed.

I am pretty sure it is because of the fontmetrics in the getButtonOffsetMethod()

Any suggestions?



0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7045609
button.getBounds() ?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8797965
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to objects@idg

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question