Link to home
Start Free TrialLog in
Avatar of sanjay_thakur
sanjay_thakur

asked on

Gettting the bounds for a JTree node

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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.
Avatar of sanjay_thakur
sanjay_thakur

ASKER

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?



ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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