1

I have implemented a Navigation tree extending JTree. For every click on a node, a form providedby the node will be displayed after the previous form has been removed.

If no form is provided by the node, the old form is simply rmoved.

My problem is that the part of displaying the first form works, but when I click on a node without a form (null), the old form is still being displayed until I e.g. resize the window.

Here is my code:

public class NavigationTree extends EncoTree {

private Container target = null;

public NavigationTree(Color from, Color to, Container trg) {
    super(from, to);
    this.target = trg;

    this.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me){
            TreePath tps = getSelectionPath();
            if(tps != null){
                cstmMutableTreeNode node = (cstmMutableTreeNode) getLastSelectedPathComponent();
                target.removeAll();     
                if(node.form != null){
                    node.form.construct();
                    node.form.setPreferredSize(new Dimension(200, target.getSize().height));

                    target.add(node.form, BorderLayout.PAGE_START);

                }
                target.validate();
            }
        }
    });
}

}

As far as I understand it a repaint() request should be enqueued in the EDT as soon as I revalidate the container.

So, why does displaying a the first form work without having to resize the window, but simply removing components does not?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
LuigiEdlCarno
  • 2,410
  • 2
  • 21
  • 37
  • 1) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, post an [SSCCE](http://sscce.org/). 3) Why add a mouse listener to a tree, when it already has better (more specific) types of listeners? – Andrew Thompson Sep 04 '13 at 14:10

1 Answers1

6

As far as I understand it a repaint() request should be enqueued in the EDT as soon as I revalidate the container.

When using Swing you should use revalidate() (instead of validate) and repaint() on the target container. The repaint may not happen on its own because it sound like you are replacing the existing component with a component of the same size so Swing doesn't think anything has changed and doesn't do the repaint.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • The use of `revalidate()` and `repaint()` did the trick. Thanks. In most cases the GUI is refreshed now. There instances though, when the refresh does not occur. So far that seems to happen, when I quickly change through the forms. – LuigiEdlCarno Sep 05 '13 at 07:27