tags:

views:

33

answers:

1

Hi,

Got a problem with ComponentListener. I'm using it to check if a certain component is resized, and to update some stuff if it is.

The code looks like this though this probably won't be much use to you:

@Override
public void componentResized(ComponentEvent e) {
    // Graph resized, reposition slice nodes
    Component c = e.getComponent();
    if (graphHeight > 0) {
        if (c == graph) {
            int offset = (c.getHeight() - graphHeight) / 2;
            if (offset != 0) {
                try {
                    controller.shiftSlice1NodesY(offset);
                } catch (GraphIntegrityException e1) {
                    logger.error(e1.getMessage(), e1);
                }
                graphHeight = c.getHeight();
            }
        }
    } else {
        graphHeight = c.getHeight();
    }
}

For one section of my code I need to disable this listener. That looks like this:

    graph.removeComponentListener(this);

    controller.parseFile(filename);

    graph.addComponentListener(this);

Everything goes smoothly until I add the component listener again. At this point the componentResized method is called about 20 times. It has possibly buffered the resize events from what I can see. Anyone know what's going on here?

+2  A: 

Looks like you are trying to modify a component off the Event Dispatch Thread (EDT). That's not even a good idea in AWT, let alone Swing.

Other than get all the AWT/Swing stuff on the right thread, part of the fix should be to get the listener check state to see if it should execute its body, rather than attempting to remove and add the listener.

Tom Hawtin - tackline
I'll check out this EDT stuff. Second suggestion is great, thank you!
waitinforatrain