Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4265560
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:38:27+00:00 2026-05-21T06:38:27+00:00

Recently in a project I saw another developer call custom nodes from a JTree

  • 0

Recently in a project I saw another developer call custom nodes from a JTree using the class name as a reference to then trigger the display of a Panel in the central area of a JFrame (constraint: one and only one Panel per node).

I can understand the use of class names in the tree as there’s not much displaying going on. However, I’d tend to use an instance in the tree itself that could also animate a JPanel. What is the most widespread practise and in what cases would one approach be more appropriate than another?

Edit: Code added at request. It seems I misremembered things as there was a bit of decoupling.

The enum represents a sort of hardcoded registry for JPanels which are displayed when a manipulation is carried out by a user. This is what I mean by using actual class names.

public enum RightEnum {
    BLAH1(BLAH1.class, "BLAH1", "This is a custom JPanel"),
    BLAH2(BLAH2.class, "BLAH2", "This is another custom JPanel")
    // etc

    private Class<? extends AbstractWidget> widgetClass;
    private String key; 
    private String label;


    RightEnum(Class<? extends AbstractWidget> widgetClass, String key, String label) {
        this.widgetClass = widgetClass;
        this.key = key;
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    public String getKey() {
        return key;
    }

    public Class<? extends AbstractWidget> getWidgetClass() {
        return widgetClass;
    }

    public static RightEnum getEnum(String key) {
        if(key == null) {
            return null;
        }
        for(RightEnum right : RightEnum.values()) {
            if(right.key.equals(key)) {
                return right;
            }
        }
        return null;
    }
}

Method from containing Swing class which recreates a custom JPanel from the enum.

private static AbstractWidget createWidget(Right right) throws Exception {
    RightEnum rightEnum = RightEnum.getEnum(right.getKey());
    Class<? extends AbstractWidget> widgetClass = rightEnum.getWidgetClass();
    AbstractWidget widget = (AbstractWidget) widgetClass.newInstance();
    widget.setRight(right);
    return widget;
}

Method inside a MouseAdapter/KeyListener which fetches some sort of model object:

private void doOpenView() throws Exception {
        NavigationTreeNode navNode = (NavigationTreeNode) CustomFrame.navigationMenu.getLastSelectedPathComponent();
        if (navNode.isLeaf()) {
            if(navNode.getObject() instanceof Right) {
                Right right = (Right) navNode.getObject();
                try {
                    CustomFrame.widgetContainer.createAndAddWidget(right);                  
                } catch (Exception ex) {
                    throw ex;
                }
            }
        }
    }

The actual JTree has nothing much interesting and simply recreates nodes from model classes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:38:28+00:00Added an answer on May 21, 2026 at 6:38 am

    It’s still a little hard to follow, but I believe your approach is probably better. If the class instance stored can also do the displaying, that’s a great method to use.

    Whenever you add a layer of “Meta”–using reflection–you really should have a great reason to do so because it encrypts your code a little. The best use in general for reflection is probably loading objects from non-java text or xml files, and even then it helps to isolate the reflection parts inside a single factory method and return instantiated java objects that don’t use any further reflection.

    Be careful, however, of the words elegant and crappy. Often what people call elegant I’d call “More Cryptic”, and crappy is usually also “More Cryptic”.

    Your goal should always be “More Readable”, even if your code is a little longer or a little less fun to write. Actually I’d say “Fully Factored” is most important, followed closely by “More Readable” (which implies some level of brevity, too much code can be as hard to read as overly “elegant” code).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on an embedded C/C++ project recently using the shell in Tornado
Recently I saw the following code that creates a class in javascript: var Model.Foo
I started a Rails project recently and decided to use RESTful controllers. I created
I got into a mini-argument with my boss recently regarding project failure. After three
I recently participated a project that replaced a mainframe system with a web (asp.net
So recently on a project I'm working on, we've been struggling to keep a
I've become very addicted to Project Euler recently and am trying to do this
I had never tried Continuum as a build server until recently when a project
recently I downloaded this open source project and I am trying to compile it.
Have recently been given a project to complete which uses XML quite extensively.Am looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.