How to give some color to a Node in a JTree

Hi there,
I have built a JTree that is now ALMOST ready to be the way I want... but ONE thing is missing : I want to be able to put some color on the leaves of my tree, well on the String objects that are just right to their icons. I searched almost all day! Why is it so hard to find some function to accomplish this task when this task is a common one.
Well please if anyone knows how to do that in a simple way please write back.
Mr. Big

It's actually quite easy.
What you have to do is write a custom tree cell renderer which extends DefaultTreeCellRenderer. In that class, your getTreeCellRenderer() method should do something like the following:
public Component getTreeCellRendererComponent(
    JTree tree, Object value, boolean selected,
    boolean expanded, boolean leaf, int row, boolean hasFocus )
        Component treeCellRenderer =
              super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        if (selected) {
            setForeground(Color.WHITE);
            setBackground(BACKGROUND_SELECTION_COLOR);
        } else {
            treeCellRenderer.setForeground(Color.RED);
        return treeCellRenderer;
Then you'll need to make sure that you call myTree.setCellRenderer(new CustomTreeCellRenderer());
cheers,
Greg

Similar Messages

  • How to give backgound color

    H All :
    I am using collection in my application for textbox
    HTMLDB_ITEM.TEXT(2,null,30,500,''style="background-color: RED"'')
    i am using this which give me the red in color background effect
    i am using
    HTMLDB_ITEM.DISPLAY_AND_SAVE(2,null,30,500)
    how to give a background or a fore ground color this item
    please suggest
    Thanks
    Sudhir

    It's actually quite easy.
    What you have to do is write a custom tree cell renderer which extends DefaultTreeCellRenderer. In that class, your getTreeCellRenderer() method should do something like the following:
    public Component getTreeCellRendererComponent(
        JTree tree, Object value, boolean selected,
        boolean expanded, boolean leaf, int row, boolean hasFocus )
            Component treeCellRenderer =
                  super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
            if (selected) {
                setForeground(Color.WHITE);
                setBackground(BACKGROUND_SELECTION_COLOR);
            } else {
                treeCellRenderer.setForeground(Color.RED);
            return treeCellRenderer;
    Then you'll need to make sure that you call myTree.setCellRenderer(new CustomTreeCellRenderer());
    cheers,
    Greg

  • How to disable some color when printing separations in Ai CS6-CC (JS)

    Hello Everyone!
    Configuration: Win 7 Pro x64, Illustrator CS6x64-CCx64, default printer PDF.
    When printing separations from Illustrator CS4 following script prints all colors except Cyan (as was intended).
    But when printing from CS6-SS disable the color is not possible - all colors are printed without exception.
    var adoc = activeDocument;
    var o = new PrintOptions();
    o.colorSeparationOptions = new PrintColorSeparationOptions();
    o.colorSeparationOptions.colorSeparationMode = PrintColorSeparationMode.HOSTBASEDSEPARATION;
    o.colorSeparationOptions.inkList = adoc.inkList;
    for (var i = 0; i < o.colorSeparationOptions.inkList.length; i++) {
    if (o.colorSeparationOptions.inkList[i].name == 'Process Cyan') {
      o.colorSeparationOptions.inkList[i].inkInfo.printingStatus = InkPrintStatus.DISABLEINK;
    adoc.print(o);
    If you check, you have inkInfo.printingStatus Cyan changed to "InkPrintStatus.DISABLEINK".
    Why it is printed and how to disable?
    Thanks!

    Hi saaiful,
    Thank you for posting your question. window. will print header and footer.
    Here are some solutions I found after researching your issue:
    *[http://forums.mozillazine.org/viewtopic.php?f=12&t=216810]
    *[http://stackoverflow.com/questions/8228088/remove-header-and-footer-from-window-print]
    *[http://www.dreamincode.net/forums/topic/22598-using-windowprint-without-printing-header-and-footer/]
    *take a screenshot and create a page that is printable [http://www.webdeveloper.com/forum/showthread.php?210947-Using-window-print-without-printing-header-and-footer]
    *Target to another window then print: [http://www.liferay.com/community/forums/-/message_boards/view_message/3401817]
    Hope this helps.

  • How to give labels to all the nodes of a tree

    I have an application in which I have written FTREE for a table ( Table A ) on a "col1" column which is of type NUMBER.
    I have used direct query to show & create the hierarchy tree based on table A.
    The query is like this:
    SELECT 1,level,to_char(col1),null, to_char(col1)
    from A,
    start with col1= somevalue
    connect by prior col1= parent_column
    Another table ( Table B )has a description for the column "col1" in table A.
    I would like to show the description from table B on the nodes instead of the number being shown from table A.
    Any clues how I can do it. Using simple join in the above query is not possible.
    Thanks
    rg
    null

    I have an application in which I have written FTREE for a table ( Table A ) on a "col1" column which is of type NUMBER.
    I have used direct query to show & create the hierarchy tree based on table A.
    The query is like this:
    SELECT 1,level,to_char(col1),null, to_char(col1)
    from A,
    start with col1= somevalue
    connect by prior col1= parent_column
    Another table ( Table B )has a description for the column "col1" in table A.
    I would like to show the description from table B on the nodes instead of the number being shown from table A.
    Any clues how I can do it. Using simple join in the above query is not possible.
    Thanks
    rg
    null

  • How to get some color for urxvt

    What I'm looking for is to make my urxvt look like it does in this screenshot: https://i.imgur.com/UgH2tJ3.jpg
    I've been messing with my ~/.Xdefaults, but that didn't change anything. I'm using rxvt-unicode-pixbuf from the AUR in case that matters.

    Perhaps slithery should have said most programs have color support.  You just need to configure the ones you want to use.  The terminal just shows what the running program tells it to show.  If you want color in vim, you'd set up color in vim.  If you want color in mutt, you'd set up color in mutt.  Etc.
    The terminal has to be capable of showing colors, but yours is.  Just because the terminal is capable, though, you still have to use programs that output color.
    ls is commonly (and by default in arch) aliased to "ls --colors" which will use the dircolors settings to highlight different filetypes in different colors when you run `ls` commands.
    Grep has a similar color setting.
    Pacman has color settings.
    Most text editors have color settings.
    See a pattern?

  • How can I view the newly added node in a JTree

    Hi! I have a problem with my project. I set up a JTree. At first it has a node that contains FARInfo object. When I click this node, the other program in the package will pop up a form to let user input and submit, then it will add a new node into the other node. The new node will contain FilledInfo object. But I cannot view this newly added node. The source code related with the 2 different object is as following:
    tree.addTreeSelectionListener( new TreeSelectionListener()
    public void valueChanged(TreeSelectionEvent e4)
    DefaultMutableTreeNode node = ( DefaultMutableTreeNode )
    (tree.getLastSelectedPathComponent ());
    Object nodeInfo = node.getUserObject();
    if (node.isLeaf())
    if ( nodeInfo instanceof FARInfo )
    FARInfo category = (FARInfo) nodeInfo;
    displayURL ( category.categoryURL );
    displayForm ( category.farFormName );
    if ( DEBUG )
    System.out.print ( category.categoryURL + ":\n" );
    else if ( nodeInfo instanceof FilledInfo )
         FilledInfo category2 = ( FilledInfo ) nodeInfo;      
         displayFilledForm ( category2.num );
    }else
    return;
    My question is: how to deal with the nodes containing 2 different objects: FARInfo and FilledInfo? FilledInfo is created by the other program in the package. Thanks for your help!

    I used insertNodeInto() to inser a new node into the tree, and it can be displayed. But when I used addTreeSelectionListener() to click on the newly added node, it cann not reaspond the click. Following is my original addTreeSelectionListener(). You see, if you click a node of FARInfo, then it will open a form, and after user filled in and submit it, a new node will be added into the Jtree. If you click a node of FilledInfo, then the user should view the content of this new node. But now, it seems that the sencond click cannot work. Thanks for your help.
    tree.addTreeSelectionListener( new TreeSelectionListener()
    public void valueChanged(TreeSelectionEvent e4)
    DefaultMutableTreeNode node = ( DefaultMutableTreeNode )
    (tree.getLastSelectedPathComponent ());
    Object nodeInfo = node.getUserObject();
    if (node.isLeaf())
    if ( nodeInfo instanceof FARInfo )
    FARInfo category = (FARInfo) nodeInfo;
    displayURL ( category.categoryURL );
    displayForm ( category.farFormName );
    if ( DEBUG )
    System.out.print ( category.categoryURL + ":\n" );
    else if ( nodeInfo instanceof FilledInfo )
    FilledInfo category2 = ( FilledInfo ) nodeInfo;
    displayFilledForm ( category2.num );
    }else
    return;

  • How do I get a list of nodes of a JTree?

    I'm getting confusing while reading APIs on JTree and DefaultMutableTreeNode.
    My question is as simple as this: how do I get a list (or array) of nodes on the specified level of a JTree?
    Please help! Thanks!

    Use the children method on DefaultMutableTreeNode. It'll return an Enumeration of all the children for a given node. - MOD

  • Can I  Have JTable As Leaf Node Of A JTree?

    Hello there,
    I want to show jtable on a click of a node of a tree. I have seen Treetable utlility on SUN's site. But it not quite what i want.
    Does any one have any idea how to show JTable as a leaf node of a JTree.
    E.g There is a tree called Components. viz.
    Component
    |Node1
    |Node2
    On click of Node1 i should see Table1 and on click of Node2 i should see Table2.
    If anyone has any idea, can you explain how to do it Or some site name where help on this problem is available?
    Please mail back.
    Regards,
    Amit

    I used insertNodeInto() to inser a new node into the tree, and it can be displayed. But when I used addTreeSelectionListener() to click on the newly added node, it cann not reaspond the click. Following is my original addTreeSelectionListener(). You see, if you click a node of FARInfo, then it will open a form, and after user filled in and submit it, a new node will be added into the Jtree. If you click a node of FilledInfo, then the user should view the content of this new node. But now, it seems that the sencond click cannot work. Thanks for your help.
    tree.addTreeSelectionListener( new TreeSelectionListener()
    public void valueChanged(TreeSelectionEvent e4)
    DefaultMutableTreeNode node = ( DefaultMutableTreeNode )
    (tree.getLastSelectedPathComponent ());
    Object nodeInfo = node.getUserObject();
    if (node.isLeaf())
    if ( nodeInfo instanceof FARInfo )
    FARInfo category = (FARInfo) nodeInfo;
    displayURL ( category.categoryURL );
    displayForm ( category.farFormName );
    if ( DEBUG )
    System.out.print ( category.categoryURL + ":\n" );
    else if ( nodeInfo instanceof FilledInfo )
    FilledInfo category2 = ( FilledInfo ) nodeInfo;
    displayFilledForm ( category2.num );
    }else
    return;

  • How to give colors to the top-of-page in ALV Grid

    How to give colors to the top-of-page in ALV Grid
    in table GT_LIST_TOP_OF_PAGE i am filling 3 rows ,i need 3 different colors to be displyed on top-of-page(one color to one row)
    CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
    I_LOGO = 'ENJOYSAP_LOGO'
    IT_LIST_COMMENTARY = GT_LIST_TOP_OF_PAGE.
    please help mee

    HI Kranthi,
    Check out teh foll. link,
    http://www.sapfans.com/forums/viewtopic.php?t=52107
    Hope this helps.

  • How to give colors to the top-of-page in ALV List Display

    how to give colors to the top-of-page in ALV List Display....

    Check this blog.........
    It provides your required output...........
    /people/vijaybabu.dudla/blog/2006/07/21/topofpage-in-alv-using-clguialvgrid
    See the point 7 for complete code...
    Regards,
    Pavan

  • We have a requirement to print the w2 forms for employees using java application, how can i implement this. Please give some suggestion.

    We have a requirement to print the w2 forms for employees using java application, how can i implement this. Please give some suggestion.

    Anyone any ideas to help please?

  • How about if you want hr or to change some colors?

    Is there away to insert <hr> or change some colors on the row column?
    How do you change some of the look and feel.
    Howard

    Hello,
    If you talking about reposts just concatenate it in in your SQL
    example
    select '&lt;div style="color:#F00">test &lt;hr />data&lt;/div>' as data from dual;
    If thats what your looking for you need to add more detail into your question.
    Carl

  • Hello !  pls give some ti[ps how to use bapi's for data uploading?

    hello !
      pls give some ti[ps how to use bapi's for data uploading?
    regards,
    Arjun

    Hi,
    See the below report extract:
    where it_data is having uploaded data.
    LOOP AT<b> it_data</b> INTO wa_data.
        line_count = sy-tabix.
        "Date Validation
        CONCATENATE wa_data-uplft_date4(4) wa_data-uplft_date2(2) wa_data-uplft_date+0(2)
        INTO wa_data-uplft_date.
        "READ TABLE it_ekko INTO wa_ekko WITH KEY lifnr = wa_data-vendor.
        LOOP AT it_ekko_temp INTO wa_ekko_temp WHERE lifnr = wa_data-vendor.
          IF wa_ekko_temp-kdatb <= wa_data-uplft_date AND wa_ekko_temp-kdate >= wa_data-uplft_date.
            MOVE-CORRESPONDING wa_ekko_temp TO wa_ekko.
            APPEND wa_ekko TO it_ekko.
          ENDIF.
        ENDLOOP.
        "IF sy-subrc = 0 AND wa_ekko-kdatb <= wa_data-uplft_date AND wa_ekko-kdate >= wa_data-uplft_date.
        LOOP AT it_ekko INTO wa_ekko.
          wa_data_header-pstng_date = wa_data-uplft_date.
          wa_data_header-doc_date = sy-datum.
          wa_data_header-bill_of_lading = wa_data-bill_of_lad.
          wa_data_header-ref_doc_no = wa_data-del_no.
          CONCATENATE wa_data-header_text1 '-'
                      wa_data-header_text2 '-'
                      wa_data-header_text3 '-'
                      wa_data-header_text4
                      into wa_data_header-HEADER_TXT.
          IF wa_data-indicator = 'Y'.
            wa_data_item-material = '000000000000200568'.
          ELSE.
            wa_data_item-material = '000000000000200566'.
          ENDIF.
          LOOP AT it_ekpo INTO wa_ekpo WHERE ebeln = wa_ekko-ebeln AND matnr = wa_data_item-material.
            "Collect Item Level Data
            wa_data_item-plant = '1000'.
            wa_data_item-stge_loc = '1001'.
            wa_data_item-move_type = '101'.
            wa_data_item-vendor = wa_data-vendor.
            wa_data-qnty = wa_data-qnty / 1000.
            wa_data_item-entry_qnt = wa_data-qnty.
            wa_data_item-po_pr_qnt = wa_data-qnty.
            wa_data_item-entry_uom = 'KL'.
            wa_data_item-entry_uom_iso = 'KL'.
            wa_data_item-orderpr_un = 'KL'.
            wa_data_item-orderpr_un_iso = 'KL'.
            wa_data_item-no_more_gr = 'X'.
            wa_data_item-po_number = wa_ekpo-ebeln.
            wa_data_item-po_item = wa_ekpo-ebelp.
            wa_data_item-unload_pt = wa_data-unload_pt.
            wa_data_item-mvt_ind = 'B'.
            APPEND wa_data_item TO it_data_item.
            CLEAR wa_data_item.
          ENDLOOP.
          CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
            EXPORTING
              goodsmvt_header = wa_data_header
              goodsmvt_code   = goodsmvt_code
              testrun         = 'X'
            TABLES
              goodsmvt_item   = it_data_item
              return          = return.
          READ TABLE return INTO wa_return WITH KEY type = 'S'.
          IF sy-subrc <> 0.
            DESCRIBE TABLE return LINES sy-tfill.
            IF sy-tfill = 0.
              CALL FUNCTION <b>'BAPI_GOODSMVT_CREATE'</b>   
            EXPORTING
                  goodsmvt_header = wa_data_header
                  goodsmvt_code   = goodsmvt_code
                  testrun         = ' '
                TABLES
                  goodsmvt_item   = it_data_item
                  return          = return.
              CALL FUNCTION <b>'BAPI_TRANSACTION_COMMIT'</b>
               EXPORTING
                 WAIT          = 'X'
              IMPORTING
                RETURN        =
            ENDIF.
          ENDIF.
          LOOP AT return INTO wa_return.
            WRITE: 'Messsage TYPE  ', wa_return-type,
                  /,'ID  ', wa_return-id,
                  /, 'Number  ', wa_return-number,
                  /, 'Message  ', wa_return-message,
                  /, 'Long Text  ', wa_return-message_v1,
                                    wa_return-message_v2,
                                    wa_return-message_v3,
                                    wa_return-message_v4,
                 /, 'Failed at line', line_count.
          ENDLOOP.
          CLEAR: wa_ekko, wa_ekpo, wa_data, it_data_item[], wa_data_header.
        ENDLOOP.
    Reward if useful!

  • How to define common division?   give some examples

    how to definfe common division?   give some examples

    Hi,
    You can define that customer master records, which have been created for each sales organization for a distribution channel (or division), are also valid in other distribution channels (or divisions). As a result, you create and change customer master records that are required in different distribution channels or in different divisions only once. This makes it easier to create and change master records.
    Divisions
    In Customizing, you can specify other divisions in which customer master data (and article master data and prices) from the reference division are also valid.
    You do this in Customizing for Master Data (Sales and Distribution) in the following activities:(SPRO>SD>Master data)
    Define Common Distribution Channels
    Define Common Divisions
    Regatds
    SD

  • How to add style(color) info for the color used in ALV tree nodes

    I have an object for CL_GUI_ALV_TREE and I have added nodes to the tree and has set color for the nodes using 'STYLE' parameter. Now if I have to add the style info to the style(color) then how do I do it using the object I have?
    Edited by: Sujay V Koparde on Feb 20, 2008 4:27 AM

    Yes Eshwar, its similar to what you have written but my problem was that the style has already been assigned in the code and now I want to assign the style info(tooltip text) to the style(colour). I have an object of class cl_gui_alv_tree as of now and I am not able to find out as to how do I proceed to assign style information

Maybe you are looking for

  • How can I delete an iTunes rental file that bombs on download?

    I had a problem with a previously purchased TV episode where the download kept crashing after so many MB. I was able to find and delete the partial file, search for purchases, and complete the download. My wife was jones'n for a chick flick tonight,

  • My daughter and I are receiving each others imessages.  How do we fix this?

    My daugher has a new iphone and i have an older one.  Once she received this phone we started receiving each others texts.  She has her own apple id and password seperate from mine and we thought we fixed the settings on the icloud and messages.  All

  • At my wits end with this horrible company

    I am beyond frustrated with the lack of service with Verizon and the way I have been treated.  I ordered a phone and tablet om-line to start new service with Verizon back in December, the following day I found a better deal elsewhere so I called to c

  • Oracle 9i on LINUX Transparent Gateway for SQL Server

    Does anybody know if there is a Transparent Gateway available for Oracle 9i on LINUX to connect to SQL Server? I have run the installer and cannot seem to find the install options for any Transparent Gateways. I am familiar with their location in the

  • Why does AE CS4 consume all my memory when running a preview?

    I have Windows Vista x64 with 4Gb of RAM but everytime i want to make a RAM PREVIEW the program does not release the memory in use. For example: I start with AE CS4 using 2gb of ram, then i make a preview and it reaches to 3gb of ram. If I continue m