Grim
Grim

Reputation: 1976

JToolBar order of items

I have a JToolBar and like to reorder the items.

In example, I have a "New", a "Open" and a "Save" Button.

I add these buttons in different Threads so the order is a kind of random.

The order unfortunately is "Save", "Open", "New". This is a problem because users are surprised about this unusual order.

How to change the order of items?

Upvotes: 0

Views: 164

Answers (2)

trashgod
trashgod

Reputation: 205785

Some alternatives:

  • Export instances of Action, illustrated here, so that they are available when the buttons can be added in the desired order.

  • Add the buttons to the tool bar in the desired order, but defer the call to setAction() until the relevant thread completes.

    final Action saveAction = new AbstractAction(…) {…}
    EventQueue.invokeLater(new Runnable() {
    
        @Override
        public void run() {
            saveButton.setAction(saveAction);
            saveButton.setEnabled(true);
        }
    });
    
  • Use a CountDownLatch, illustrated here, to ensure that all the relevant threads have completed before adding the buttons.

Upvotes: 1

A simple approach is to setup your toolbar with all buttons added in the correct order and then make the invisible.

Each thread can then make the relevant button visible. and you won't have to wait for threads to complete - hopefully.

Upvotes: 0

Related Questions