Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:37:08+00:00 2026-05-30T12:37:08+00:00

Trigger is a recently re-detected SwingX issue : support deep – that is under

  • 0

Trigger is a recently re-detected SwingX issue: support deep – that is under collapsed nodes as opposed to visible nodes only, which is the current behaviour – node searching.

“Nichts leichter als das” with all my current exposure to SwingWorker: walk the TreeModel in the background thread and update the ui in process, like shown in a crude snippet below. Fest’s EDT checker is happy enough, but then it only checks on repaint (which is nicely happening on the EDT here)

Only … strictly speaking, that background thread must be the EDT as it is accessing (by reading) the model. So, the questions are:

  • how to implement the search thread-correctly?
  • or can we live with that risk (heavily documented, of course)

One possibility for a special case solution would be to have a second (cloned or otherwise “same”-made) model for searching and then find the corresponding matches in the “real” model. That doesn’t play overly nicely with a general searching support, as that can’t know anything about any particular model, that is can’t create a clone even if it wanted. Plus it would have to apply all the view sorting/filtering (in future) …

// a crude worker (match hard-coded and directly coupled to the ui)
public static class SearchWorker extends SwingWorker<Void, File> {

    private Enumeration enumer;
    private JXList list;
    private JXTree tree;

    public SearchWorker(Enumeration enumer, JXList list, JXTree tree) {
        this.enumer = enumer;
        this.list = list;
        this.tree = tree;
    }

    @Override
    protected Void doInBackground() throws Exception {
        int count = 0;
        while (enumer.hasMoreElements()) {
            count++;
            File file = (File) enumer.nextElement();
            if (match(file)) {
                publish(file);
            }
            if (count > 100){
                count = 0;
                Thread.sleep(50);
            }    
        }
        return null;
    }


    @Override
    protected void process(List<File> chunks) {
        for (File file : chunks) {
            ((DefaultListModel) list.getModel()).addElement(file);
            TreePath path = createPathToRoot(file);
            tree.addSelectionPath(path);
            tree.scrollPathToVisible(path);
        }
    }

    private TreePath createPathToRoot(File file) {
        boolean result = false;
        List<File> path = new LinkedList<File>();
        while(!result && file != null) {
            result = file.equals(tree.getModel().getRoot());
            path.add(0, file);
            file = file.getParentFile();
        }
        return new TreePath(path.toArray());
    }

    private boolean match(File file) {
        return file.getName().startsWith("c");
    }

}

// its usage in terms of SwingX test support
public void interactiveDeepSearch() {
    final FileSystemModel files = new FileSystemModel(new File("."));
    final JXTree tree = new JXTree(files);
    tree.setCellRenderer(new DefaultTreeRenderer(IconValues.FILE_ICON, StringValues.FILE_NAME));
    final JXList list = new JXList(new DefaultListModel());
    list.setCellRenderer(new DefaultListRenderer(StringValues.FILE_NAME));
    list.setVisibleRowCount(20);
    JXFrame frame = wrapWithScrollingInFrame(tree, "search files");
    frame.add(new JScrollPane(list), BorderLayout.SOUTH);
    Action traverse = new AbstractAction("worker") {

        @Override
        public void actionPerformed(ActionEvent e) {
            setEnabled(false);
            Enumeration fileEnum = new PreorderModelEnumeration(files);
            SwingWorker worker = new SearchWorker(fileEnum, list, tree);
            PropertyChangeListener l = new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
                        //T.imeOut("search end ");
                        setEnabled(true);
                        ((SwingWorker) evt.getSource()).removePropertyChangeListener(this);
                    }
                }
            };
            worker.addPropertyChangeListener(l);
            // T.imeOn("starting search ... ");
            worker.execute();
        }

    };
    addAction(frame, traverse);
    show(frame)
 } 

FYI: cross-posted to OTN’s Swing forum and SwingLabs forum – will try to post a summary of all input at the end (if there is any 🙂

Addendum

At the end of the day, it turned out that I asked the wrong question (or right question in a wrong context ;-): the “problem” arose by an assumed solution, the real task to solve is to support a hierarchical search algorithm (right now the AbstractSearchable is heavily skewed on linear search).

Once that will solved, the next question might be how much a framework can do to support concrete hierarchical searchables. Given the variety of custom implementations of TreeModels, that’s most probably possible only for the most simple.

Some thoughts that came up in the discussions here and the other forums. In a concrete context, first measure if the traversal is slow: most in-memory models are lightning fast to traverse, nothing needs to be done except using the basic support.

Only if the traversal is the bottleneck (as f.i. in the FileSystemModel implementations of SwingX) additional work is needed:

  • in a truly immutable and unmodifiable TreeModel we might get away with read-only access in a SwingWorker’s background thread
  • the unmodifiable precondition is violated in lazy loading/deleting scenarios
  • there might be a natural custom data structure which backs the model, which is effectively kind-of “detached” from the actual model which allows synchronization to that backing model (in both traversal and view model)
  • pass the actual search back to the database
  • use an wrapper on top of a given TreeModel which guarantees to access the underlying model on the EDT
  • “fake” background searching: actually do so in small-enough blocks on the EDT (f.i. in a Timer) so that the user doesn’t notice any delay

Whatever the technical option to a slow search, there’s the same usability problem to solve: how to present the delay to the end user? And that’s an entirely different story, probably even more context/requirement dependent 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:37:10+00:00Added an answer on May 30, 2026 at 12:37 pm

    Both SwingWorker and TreeModel should synchronize access to a common, underlying DataModel. Making the shared quanta of data (effectively) immutable can minimize the overhead. As this is highly application-dependent, I’m not sure how much support the view can offer for searching non-visible nodes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an auditing trigger that automatically places the time something was updated and
I want to trigger an event that causes the ScrollPane to start scrolling up
Recently, I encountered a BEFORE INSERT OR UPDATE trigger on a table. In this
I recently read about http://php.net/pcntl and was woundering how good that functions works and
As recently as several years ago, the developers actually made the builds that went
I've recently having problems with custm CALLBACK's function pointers, and that boiled down into
Recently in a project I saw another developer call custom nodes from a JTree
I read some code recently that does something like this: bob = {'name': 'Bob
I have a table in SQL Server, tblMain. There's a trigger that when a
The trigger below is delaying my insert response. How can I prevent this? create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.