开发者

Swing app with security manager causing strange GUI-refresh issues

I have a Swing app (residing in an executable, signed jar) that is a client. The app also connects to a se开发者_运维技巧rver. When certain conditions are met, I perform a refresh of the GUI (more specifically, delete all nodes of a JTree and then repopulate it). When I run this client as-is (i.e., without a security manager and without residing in an executable, signed JAR), the tree refreshes and updates without any issue.

However, when I package my client as a signed JAR (with the appropriate policy file), I get refresh issues. When the app starts up, I my JTree is not expanded. When I click it once, it looks like it has expanded, but the child nodes do not show. I have to click it twice after that for the nodes to show. Also, when I perform a refresh (deleting all nodes and then repopulating), the UI doesn't refresh appropriately. I (again) have to click the root-node twice to refresh the GUI.

I tried adding AWT permissions to the policy file, but that didn't help (I wasn't seeing any permission-violations in the first place, but I thought I'd try). I even tried giving the JAR all permissions. That didn't seem to help either. What could be causing this?

The code that performs the refresh is as follows:

private void buildTree() throws IOException, ClassNotFoundException {
    setVisible(false);
    tree.removeTreeWillExpandListener(this);
    tree.removeTreeSelectionListener(this);

    DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot();
    root.removeAllChildren();

    root.setUserObject(base);

    Book[] bookArray = remoteLibraryService.getAllBooks();
    TreeBuilderService.buildTree(root, bookArray);
    treeModel.reload();

    for(int i = 0; i < tree.getRowCount(); i++) {
        tree.expandRow(i);
    }

    tree.addTreeSelectionListener(this);
    tree.addTreeWillExpandListener(this);
    setVisible(true);
}

The code that populates the tree (showing just the relevant snippet):

categoryNode = new DefaultMutableTreeNode(book.getGenre());
root.add(categoryNode);

This probably isn't a deal-breaker as far as the assignment is concerned, but it is really bothering me; I'd like to figure out what's causing it.


As discussed in Initial Threads, Swing GUI components must be created on the event dispatch thread. Empirically, JTree is particularly susceptible to fail when manipulated from another thread. The SecurityManager is probably adventitious; any change in timing, even changing platforms, may expose the flaw.


Well you are doing IO in the GUI refresh cycle, so the GUI thread might well be blocked/stalled as a result. Instead consider using a SwingWorker and asynchronous population of the Tree through its API.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜