Solved

Gettting the bounds for a JTree node

Posted on 2002-05-24
5
223 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
  • 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now