Marcelo Diones
Marcelo Diones

Reputation:

How to determine a swing component?

I have a problem.

I'm working on another guy's code and there is a JFrame with lots of JSeparators(he used them as borders for 'panels') now I'm replacing them for a JBorderedPanel class that follows the same border style of the whole application.

The problem is that some of his separators are not clear to determine where they are in the code, there are lots of jSeparator#, replace with for any number between 0 and 999.

Is there any way to determine which variable correspond to which border other than testing all jSeparators one by one?

In before 'Don't replace them!' I'm obligated to replace them. I wouldn't be doing this if I could.

Thanks in advance.

Upvotes: 3

Views: 1119

Answers (7)

willcodejavaforfood
willcodejavaforfood

Reputation: 44063

I would advise you to not use borders much. Borders are probably the most misused component in GUI history. Originally it was intended to group together a very small set of related components, usually checkboxes or radio buttons. Then someone invted the titled border and it turned into the lazy programmers way of naming sections, which ideally should be done with the use of a label and white space.

A border will just add visual noise instead of the intended separation. Less is more.

Upvotes: 0

Joshua McKinnon
Joshua McKinnon

Reputation: 24709

Take a look at Swing Explorer. It's quite a handy swing debugging tool. There's a plugin for Eclipse that will instrument your code on the fly and launch.

With it you can view the swing object heirarchy, right click on it, and render any part of it in another window that highlights each component and lets you see their boundaries, as well as select them. Once selected, you can right click the component in the tree and print a stacktrace that will lead you to where that component gets created...

Upvotes: 8

Aaron Digulla
Aaron Digulla

Reputation: 328556

Walk the children of the JFrame and add the mouse listener to each JSeparator inside of it:

public void installListeners (java.awt.Container parent) {
    for (Component child: parent.getComponents()) {
        if (child instanceof JSeparator) {
            child.addMouseListener (...
                hover(event);
            }
        }
        if (child instanceof java.awt.Container) {
            installListeners ((java.awt.Container)child);
        }
    }
}

Now implement hover() to compare the event source with all the fields in the current class and print the one that matches:

public void hover (MouseEvent event) {
    for (Field f: getClass().getFields()) {
        if (f.get(this) == event.getSource()) {
            System.out.println(f.getname());
            break;
        }
    }
}

You'll have to handle a bazillion of Exceptions but that's basically it.

Upvotes: 2

Diones
Diones

Reputation: 1481

Op here, I liked both of your ideas, but all the jSeparators are initializated like this:

public JSeparator getJSeparatorArvore01() {
    if (jSeparatorArvore01 == null) {
        jSeparatorArvore01 = new JSeparator();
        jSeparatorArvore01.setLocation(new Point(14, 38));
        jSeparatorArvore01.setSize(new Dimension(72, 10));
    }
    return jSeparatorArvore01;
}

How do I add mouse listeners(or different colores) on over 50 jSeparators, without spending 24h? :(

Upvotes: 0

Dennis C
Dennis C

Reputation: 24747

I guess the previous guy used somekind of GUI editor.

My first try will be the GUI editor in Netbeans or Eclipse. They may able to parse and render it correctly, unless the code is really ugly.

If it can be open, you can trace where they are by select them on UI.

Upvotes: 0

VonC
VonC

Reputation: 1323753

You could add a color to each of those separators in the code (green, red, yellow, and so on) and see where those colored JSeparator end up being displayed in your application...

Upvotes: 2

Bombe
Bombe

Reputation: 83845

You could install a MouseListener on each JSeparator. When the mouse enter its area, turn its background red and print a line identifying the object, preferrably by printing its variable name. This probably requires you to change the constructor calls but your IDE should support you doing it.

Upvotes: 1

Related Questions