Solved

Gettting the bounds for a JTree node

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 123
factorial example 4 50
ejb entity bean example issue 2 18
What is the use of Forwarding Class in java 1 18
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

820 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