Solved

Gettting the bounds for a JTree node

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse java  build path 6 57
xampp tool 12 50
type mismatch (Object[] to double[] 4 24
hibernate example for saving data 19 42
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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