How to resize a custom tree node like you would a JFrame window?

Hello,
I am trying to resize a custom tree node like you would a JFrame window.
As with a JFrame, when your mouse crosses the Border, the cursor should change and you are able to drag the edge to resize the node.
However, I am faced with a problem. Border cannot detect this and I dont want to use a mouse motion listener (with a large number of nodes, I fear it will be inefficient, calculating every node's position constantly).
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.util.EventObject;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
public class ResizeNode extends JPanel {
       AnilTreeCellRenderer2 atcr;
       AnilTreeCellEditor2 atce;
       DefaultTreeModel treeModel;
       JTree tree;
       DefaultMutableTreeNode markedNode = null;
     public ResizeNode() {
            super(new BorderLayout());
               treeModel = new DefaultTreeModel(null);
               tree = new JTree(treeModel);          
              tree.setEditable(true);
               tree.getSelectionModel().setSelectionMode(
                         TreeSelectionModel.SINGLE_TREE_SELECTION);
               tree.setShowsRootHandles(true);
              tree.setCellRenderer(atcr = new AnilTreeCellRenderer2());
              tree.setCellEditor(atce = new AnilTreeCellEditor2(tree, atcr));
               JScrollPane scrollPane = new JScrollPane(tree);
               add(scrollPane,BorderLayout.CENTER);
     public void setRootNode(DefaultMutableTreeNode node) {
          treeModel.setRoot(node);
          treeModel.reload();
       public static void main(String[] args){
            ResizeNode tb = new ResizeNode();
            tb.setPreferredSize(new Dimension(400,200));
              JFrame frame = new JFrame("ResizeNode");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setContentPane(tb);
              frame.setSize(400, 200);
              frame.pack();
              frame.setVisible(true);
              tb.populate();
     private void populate() {
          TextAreaNode2 r = new TextAreaNode2(this);
           setRootNode(r);
           TextAreaNode2 a = new TextAreaNode2(this);
           treeModel.insertNodeInto(a, r, r.getChildCount());          
class AnilTreeCellRenderer2 extends DefaultTreeCellRenderer{
TreeBasic panel;
DefaultMutableTreeNode currentNode;
  public AnilTreeCellRenderer2() {
     super();
public Component getTreeCellRendererComponent
   (JTree tree, Object value, boolean selected, boolean expanded,
   boolean leaf, int row, boolean hasFocus){
     TextAreaNode2 currentNode = (TextAreaNode2)value;
     NodeGUI2 gNode = (NodeGUI2) currentNode.gNode;
    return gNode.box;
class AnilTreeCellEditor2 extends DefaultTreeCellEditor{
  DefaultTreeCellRenderer rend;
  public AnilTreeCellEditor2(JTree tree, DefaultTreeCellRenderer r){
    super(tree, r);
    rend = r;
  public Component getTreeCellEditorComponent(JTree tree, Object value,
   boolean isSelected, boolean expanded, boolean leaf, int row){
    return rend.getTreeCellRendererComponent(tree, value, isSelected, expanded,
     leaf, row, true);
  public boolean isCellEditable(EventObject event){
    return true;
class NodeGUI2 {
     final ResizeNode view;
     Box box = Box.createVerticalBox();
     final JTextArea aa = new JTextArea( 1, 5 );
     final JTextArea aaa = new JTextArea( 1, 8 );
     NodeGUI2( ResizeNode view_ ) {
          this.view = view_;
          box.add( aa );
          aa.setBorder( BorderFactory.createMatteBorder( 0, 0, 1, 0, Color.GREEN ) );
          box.add( aaa );
          box.setBorder( BorderFactory.createMatteBorder( 5, 5, 5, 5, Color.CYAN ) );
     private Dimension getEditorPreferredSize() {
          Insets insets = box.getInsets();
          Dimension boxSize = box.getPreferredSize();
          Dimension aaSize = aa.getPreferredSize();
          Dimension aaaSize = aaa.getPreferredSize();
          int height = aaSize.height + aaaSize.height + insets.top + insets.bottom;
          int width = Math.max( aaSize.width, aaaSize.width );
          if ( width < boxSize.width )
               width += insets.right + insets.left + 3;     // 3 for cursor
          return new Dimension( width, height );               
class TextAreaNode2 extends DefaultMutableTreeNode {  
     NodeGUI2 gNode;
     TextAreaNode2(ResizeNode view_) {     
          gNode = new NodeGUI2(view_);
}

the node on the tree is only painted on using the
renderer to do the painting work. A mouse listener
has to be added to the tree, and when moved over an
area, you have to determine if you are over the
border and which direction to update the cursor and
to know which way to resize when dragged. One of the
BasicRootPaneUI has some code that can help determine
that.Thanks for replying. What is your opinion on this alternative idea that I just had?
I am wondering if it might be easier to have a toggle button in the node that you click when you want to resize the node. Then a mouse-down and dragging the mouse will resize the node. Mouse-up will reset the toggle button, and so will mouse down in an invalid area.
Anil

Similar Messages

  • Custom tree node and DefaultTreeModel

    Hello,
    I would like to know if it's possible to use the following custom tree node with the DefaultTreeModel:
    class MyTreeNode extends DefaultMutableTreeNode{
      private String _argument = null;
      public MyTreeNode(){}
      public MyTreeNode(Object node, String argument){
         super(node);
          _argument = argument;
    }I have tried it and it's not working but I think in theory it should work. Please correct me if I'm wrong.
    The problem is that the tree won't display or insert the custom node when the insertNodeAt(...) method of
    the DefaultTreeModel is called. I also tried using a custom tree listener and calling the .reload() method directly.
    Any help will be very appreciated.
    Thanks in advanced.

    Are you sure it's not being inserted into the model,
    and just not being displayed?
    Are you calling nodesWereInserted() after your
    insertion?Thanks for your reply.
    I've modified my app. so that the following method will populate the tree for testing only:
      public void populateTree()
        MyTreeNode root = new MyTreeNode("Root Node", "test");
        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        JTree tree = new JTree(treeModel);
        tree.setEditable(true);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setShowsRootHandles(true);
        JScrollPane scrollPane = new JScrollPane(tree);
        setLayout(new GridLayout(1,0));
        add(scrollPane);
        int childIdx[] = {0,1};
        String p1Name = new String("Parent 1");
        String p2Name = new String("Parent 2");
        String c1Name = new String("Child 1");
        String c2Name = new String("Child 2");
        MyTreeNode p1 = new MyTreeNode(p1Name, "test");
        MyTreeNode p2 = new MyTreeNode(p2Name, "test");
        MyTreeNode c1 = new MyTreeNode(c1Name, "test");
        MyTreeNode c2 = new MyTreeNode(c2Name, "test");
        treeModel.insertNodeInto(p1, root, 0);
        treeModel.insertNodeInto(p2, root, 1);
        treeModel.nodesWereInserted(root, childIdx);
        treeModel.insertNodeInto(c1, p1, 0);
        treeModel.insertNodeInto(c2, p1, 1);
        treeModel.nodesWereInserted(p1, childIdx);
        treeModel.reload();
    }And the result is that the "Root Node" will be displayed with "Parent1" and "Parent2" but "Parent1" won't have any children. Furthermore, if I try to remove "Parent1" using:
    treeModel.removeNodeFromParent(p1);I will get an exception saying that node p1 has no parent, which suggests that the parent/child relation
    is not being assigned by the model, even though the nodes (p1, p2) are displayed in the tree showing "Root node" as the parent. This is very strange.
    Please help.

  • How to get Value of tree node without Reload Page

    hi,
    i worked with apex 4.2 and i created Tree and tabular form to retrieve the date according the value of tree select node the code of tree something like this
    select case when connect_by_isleaf = 1 then 0
    when level = 1 then 1
    else -1
    end as status,
    level,
    "ENAME" as title,
    null as icon,
    "EMPNO" as value,
    null as tooltip,
    'f?p=36648:34:5234984107903::::P40_SELECTED_NODE:'||empno as link
    from "DEPT"."EMP"
    start with "MGR" is null
    connect by prior "EMPNO" = "MGR"
    order siblings by "ENAME
    and i put Selected Node Page Item: P40_SELECTED_NODE . the tree worked good and retrieve the data into tabular form according to tree node value
    my Question :
    1- i want to retrieve the data without submit the page where each time i select value from tree make page reload to update the tabular form with new value ,there is any way to pass the value of tree node to P40_SELECTED_NODE item and refresh tabular form without page reload .
    2- i want when selected from tree run page process according to value of tree node i tray to create Dynamic action with *(jquery selector : div.tree li>a)* but the Value of node incorrect.
    Regards
    Ahmed;

    look at this link
    Re: How to get Value of tree node without Reload Page ..!

  • How can I quickly view pdf files like I can do with Windows Picture and Fax viewer for jpg files?

    How can I quickly view pdf files like I can do with Windows Picture and Fax viewer for jpg files? I need to look at several thousand PDF files. It takes too long to open each one individually. The only thing I could think of is combining them into large groups and then using the Navigation index. But I like the way windows Picture and Fax Viewer does it because you can keep the files separate. Combining PDFs causes loss of individual file names. That would be a problem since I do need to have the individual file names.

    Windows Picture and Fax Viewer is a DLL and is started via a rundll32.exe call and can't be set as an application to handle images in Firefox 3 and later versions.
    Try to set Windows Picture and Fax Viewer as the default viewer in Windows, then it should be listed automatically in the Mozilla Firefox Browse dialog.
    *http://www.winhelponline.com/articles/115/1/Windows-Picture-and-Fax-Viewer-as-the-default-viewer-in-Mozilla-Firefox.html
    '''If this reply solves your problem, please click "Solved It" next to this reply when <u>signed-in</u> to the forum.'''

  • Is there a program that allows you to black out most of the screen content of a PDF and slide down like you would with an overhead (covered with a piece of paper)for teaching purposes??

    Is there a program that allows you to black out most of the screen content of a PDF (on an iPad) and slide down like you would with an overhead (covered with a piece of paper) for teaching purposes??

    Maybe this online manual would help.
    * http://en.flossmanuals.net/thunderbird/interface/
    Not sure how well printing it out would work, though.

  • Can you display the battery strength in numbers on home screen like you would for the signal?

    I know you can view it at the option/status arena, but can you change the battery strength display at the home screen to numbers like you would on the signal?

    BatteryPercent = DeviceInfo.getBatteryLevel();
    LabelField Label1 = new  LabelField();
    Label1.setText(""+BatteryPercent+"%"); 
    you can use the above code to display battery percent  
    feel free to press the kudos button on the left side to thank the user that helped you.
    please mark posts as solved if you found a solution.

  • Custom tree node

    Hi
    I need to create a custom tree.
    Is it posible in flex tree, to have a node, that is a panel component, with a few other components, like textinput or buttons?
    Clicking on buttons causes adding or removing new nodes in appropriate place in the tree.
    What in fact I need to create, is advanced filter builder (originaly created in GWT), shown here: http://www.smartclient.com/smartgwt/showcase/#featured_filter_builder_grid
    The structure of that filter looks like 'very' custom tree
    Do you have any idea how to do that, or where can I find any hints or solutions?

    Here is my tree renderer:
    package
         import mx.controls.listClasses.IDropInListItemRenderer;
         import mx.controls.treeClasses.*;
         public class treeRendererFilter extends TreeItemRenderer
              private var customerFilterSet:Boolean = false;
              public function treeRendererFilter()
                   super();
              override public function set data(value:Object):void
                    super.data = value;
                      if (value.@name == "Customer Reporting" && !customerFilterSet)
                           var filter:customerReportingFilter = new customerReportingFilter();
                        addChild(filter);
                        filter.data = value;
                           filter.height = 25;
                           filter.width = 280; 
                           customerFilterSet = true;                                                     
                 override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
                  super.updateDisplayList(unscaledWidth, unscaledHeight);
                   if (data.@name == "Customer Reporting")
                        label.width -= getChildAt(1).width;
                        getChildAt(1).x = label.x + label.width;
                        getChildAt(1).visible = true;
                   else     
                        if (customerFilterSet)
                             getChildAt(1).visible = false;
    <?xml version="1.0" encoding="utf-8"?>
    <mx:FormItem xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"
         direction="horizontal" fontSize="10">
         <mx:Script>
              <![CDATA[
              override public function set data(value:Object):void
                   if(value != null)
                        exclude.data = value;
                        comments.data = value;
              override public function get data():Object
                   return data;
         ]]>
         </mx:Script>          
         <mx:HBox width="100%" color="blue">          
              <mx:CheckBox id="exclude" label="Excluding Skus"/>
              <mx:CheckBox id="comments" label="Customer Comments"/>
         </mx:HBox>     
    </mx:FormItem>
    HTH

  • How to Programatically select a Tree Node ?

    Hi,
    JDV = 11.1.1.6
    I don't know about the answer yet! but the question is simple ;)
    say I'm viewing departments Info in a tree (Code , Name)
    How can I select a particular node in my tree using "Code" value as the parameter?
    Thank you ,
    Shahab

    Hi Shahab,
    Please find below a custom method to programmatically select a node during a disclose event by the user:
    public void onRowDisclosure(RowDisclosureEvent rowDisclosureEvent) {
         RichTree tree = (RichTree)rowDisclosureEvent.getSource();
         RowKeySet disclosedRows = rowDisclosureEvent.getAddedSet();
         RowKeySet selectedRows = tree.getSelectedRowKeys();
         selectedRows.clear();
         if (disclosedRows.size() == 1){
         Object disclosedNode = disclosedRows.iterator().next();
         selectedRows.add(disclosedNode);
         makeCurrent(tree, disclosedRows);
         tree.setSelectedRowKeys(selectedRows);
         AdfFacesContext adfFacesContext =
         AdfFacesContext.getCurrentInstance();
         adfFacesContext.addPartialTarget(tree.getParent());
    Hope this helps in your requirement.
    Best Regards,
    Ankit Gupta

  • How can i can add tree node ?

    h want like to add tree node in the run time mode and commit it into database

    use ftree.ADD_TREE_NODE for adding new nodes.
    But what do you want to save in the db ?

  • How to alter the alv tree node text?

    hi
      i want to alter the alv tree node text, many people say it cann't be changed, but
    i look into the sample code 'BCALV_TREE_04', the average funtion(select a column and then select average function in the tool bar) can alter the tree node text dynamically.
      i try to look into the source code, the it's the sap standard code, all the funtions that user input, it goes to the statement "  CALL METHOD cl_gui_cfw=>dispatch.", and i don't understand what this statement do, can someone helps me?

    hi
    good
    The ALV tree report produces uses OBJECT METHOD functionality in-order to produce a
    tree structured ALV output. 
    The creation of an ALVtree report first requires the creation of a simple program to build the ALV
    details such as the fieldcatalog and to call a screen which will be used to display the ALVTree.
    The screen should be created with a 'custom control' where you wish the ALVtree report to appear.
    For the following example it will have the name 'SCREEN_CONTAINER'.
    http://www.sapdevelopment.co.uk/reporting/alv/alvtree%5Calvtree_basic.htm
    reward point if helpful.
    thanks
    mrutyun^

  • How to set the default tree node in JTree?

    I want to set a default tree node in JTree when start the program,
    what is the method or the function to call?
    Thanks~

    If you by "default" mean "selected", then you can use one of the setSelectionXXX methods in the JTree API.
    If you by "default" mean something else, never mind this post :-)

  • How to programmatically click a tree node(make it selected)?

    in my program, i want to make a tree node "clicked" by codes, so that the valueChanged(TreeSelectionEvent tse e) method of the TreeSelectionListener can be invoked.
    thanks a lot!!

        private Robot robot;
        private void triggerMouseClick(TreePath treePath) {
            if (robot == null) {
                try {
                    robot = new Robot();
                    robot.setAutoDelay(60);
                catch (AWTException e) {
                    e.printStackTrace();
            Rectangle rect = this.getPathBounds(treePath);
            Point point = new Point(rect.x+1, rect.y+1);
            SwingUtilities.convertPointToScreen(point, this);
            robot.mouseMove(point.x, point.y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
        }

  • How to highlight/indicate particular tree Node in Tree UI element

    Hi All
    Can anybody let us know how to highlight/indicate specific node in a tree struture.
    currently i am able to display the tree struture with all the nodes & elements but it is always tasking firstnode as highlighted one or indicated one.
    if i want to highlight specific node in a tree struture...what was the procedure or any sample code then it would be great help to us.
    Thanks
    Trisha rani

    Hi Krishna
    Thanks for your reply
    I displayed the tree structure and i want to highlight specific parent node/child element , what was the sample code??
    for example my tree was displayed in the below struture and at the runtime specific child node i wanted to be highlighted i want to make selectable particular nodeType......
    Can you pls send sample codee code??
    my requirement
    A
    A1
       A2
       A3
    B
    B1      
       B2
       B3
       B4
       B5     
    now i want to make selectable or highlighted B4,B5 etc  or A2,A3 at runtime.
    The other guy who replied for this thread it is working for Parent nodes to make highlighted  like it is working for parent nodes which is have child nodes. i am able to hightlight at runtime for Nodes A,B etc .
    Now i want to highlight or make selected one for B4,A3 etc, pls provide sample code??
    it  would be great help to us
    Thanks
    Trisha Rani

  • How to create a new tree node with the initial edit function

    Hi all,
    I would like to mimic the Windows system when the user creates a new node. The newly created node should be in the edit mode, i.e.it should
    be highlighted and the cursor should be appear at the end of
    the newly created node name.
    I have the folloing code after I create the new node and set its selection:
    TreePath selectionPath = getTree().getSelectionPath();
    tree.startEditingAtPath(selectionPath);
    However, this only partially does what I want, the cursor does not appear at the end of the node's name and I am not sure how to select the text name of the new node.
    I hope someone can help.
    Kanita

    I haven't tried myself but my guess is that you need to customize your tree cell editor for putting
    cursor at specific position, etc.

  • How to add icons to tree node ?

    hi,
    i want to set node icon of a JTree with respect to the level of that node.
    Also need to expand the tree up to 1st level while loading.
    how can i get the thing done?

    Read the tutorial: [How to Use Tables|https://java.sun.com/docs/books/tutorial/uiswing/components/table.html]
    kaushalya.cse wrote:
    i want to set node icon of a JTree with respect to the level of that node.You will probably need a custom renderer for this.
    Also need to expand the tree up to 1st level while loading.You can use the expand methods in JTree to expand any node you want.

Maybe you are looking for

  • Move emails between OSX 10.4 and OSX 10.8

    I'm trying to move my old emails and address book from my old computer which is running OSX 10.4 to my new one with OSX 10.8. Migration assistant doesn't work with 10.4 since it's too old, any ideas on how it can be done? Greatful for all help!

  • Solaris installation - ORA-27302

    Hi, I was trying to install the Oracle Collaboration Suite on a Solaris box. During the Information storage, I got the following error in the Oracle Database Configuration Assistant: ORA-27302: failure occured at: skgpwreset1 ORA-27303: additional in

  • Nokia Lumia 521 - WiFi connected, but no internet ...

    I have a brand new Lumia 521 on T-Mobile. I set up the wifi connection and it shows connected. However, there is no internet connection. Cannot download any app or anything. Trouble Shooting: Rebooted the phone couple of times. Turned on/off wifi. de

  • HT1338 IWEB  pages

    After my iweb was erased by an Apple Genius,. I put the information on the PAGES program. The actual site is still on the mobile me web. I am missing the iweb part  Is there a way that I can put the information back on i web so that I can publish and

  • Updating iTunes damaged my library file

    I left iTunes to update, and after restarting my computer, I got a message that popped up saying that the library file had been damaged. The only advice I got came from this article: http://support.apple.com/kb/TS1967 ...which says to revert back to