0

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?

Grim
  • 1,938
  • 10
  • 56
  • 123
  • 1
    *"I add these buttons in different Threads so the order is a kind of random."* What is the compelling reason for adding components to a tool bar in what must be at least two non-EDT threads (..that **doesn't** just make it sound like an idiotic thing to do). – Andrew Thompson May 26 '16 at 14:25
  • @AndrewThompson: I think it's psychological; users hate waiting for the app to warm up less when there's at least _something_ to see, for [example](http://stackoverflow.com/a/25526869/230513). – trashgod May 26 '16 at 16:15
  • @AndrewThompson trashgod is right. Any 3rd party library-programmer shall not must study programming-handbooks before he code to notice to not add buttons in async threads. It is a abnormal limitation. Do you realy like to block the progress until a button is initialized? No. – Grim May 26 '16 at 21:59

2 Answers2

1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

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.