views:

389

answers:

7

Hi guys, 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.

+1  A: 

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.

Bombe
+2  A: 

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...

VonC
A: 

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.

Dennis Cheung
I think he used the eclipse GUI editor, but last time I tried to use it, I had to reinstall eclipse.
Diones
A: 

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? :(

Diones
+7  A: 

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...

Joshua McKinnon
I can't recommend Swing Explorer enough - the authors presented at JavaOne last year, and it was the most useful presentation of the entire conference (at least for us desktop developers)
Kevin Day
+2  A: 

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.

Aaron Digulla
A: 

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.

willcodejavaforfood