2

I'm writing an application which parses XML files (continuously) and show the data in a GUI (Swing). The ParseThread is in the CoreProject, and the GUI is in the GUIProject.

The start of the ParseThread is connected to a JCheckBoxMenuItem with an ItemListener. The value of setSelected() is set directly after adding to the Menu. At this time the GUI does not contain the Component which the ParseThread needs to show the parsed Data.

My Solution is, that the ParseThread should wait until the GUI is build completely. I thought of something like an EventQueue but I have no Idea how to code one.

nidomiro
  • 820
  • 2
  • 10
  • 24
  • 3
    Why don't you just kick off your threads after the GUI is ready? – GETah Jul 01 '12 at 12:45
  • Agree -- I'm not sure what your exact problem is, though it sounds like you could use a SwingWorker to do background processing with an added PropertyChangeListener to notify the GUI of the thread's progress. – Hovercraft Full Of Eels Jul 01 '12 at 12:56
  • 1
    Yes, I can't see any point in creating the thread until all the data it needs to run, (directly) exists. That said, GUI componenets should not belong in that category anyway - if the GUI is communicated with by messaging in the usual way, this problem should not occur? – Martin James Jul 01 '12 at 13:00

2 Answers2

3

My Solution is, that the ParseThread should wait until the GUI is build completely. I thought of something like an EventQueue but I have no Idea how to code one.

you have got issue with Concurency in Swing, your hard and long running task should be moved to the Background task, for Swing there are two possibilities

  • (easy & simple) use Runnable#Thread, output to Swing GUI must be wrapped into invokeLater(), including thread safe methods as are setText, append e.i.

  • use SwingWorker

EDIT

please to check my visulaizations for Runnable#Thread this is the same thing as you connect server, parse long file e.i.,

with invokeLater() I cannot be sure that the component exists until the call

  1. create GUI,

  2. show GUI,

  3. some (Swing / Util) Timer or user action to invoke code that is/are redirected out of Swing EventDispatchThread, for this reason there are Runnable#Thread or SwingWorker

  4. I'm suggest two easiest of possible ways

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

Ok, I got my problem... The GUI is created like this:

EventQueue.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                try {
                    Mainframe frame = new Mainframe();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

And at construction of the Object Mainframe this code will be executed:

final JCheckBoxMenuItem chckbxmntmParsing = new JCheckBoxMenuItem("Parsing");
    chckbxmntmParsing.setName("mainframe.menu.data.parsing");
    localeChangedListener.add(chckbxmntmParsing);

    chckbxmntmParsing.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if (chckbxmntmParsing.isSelected()) {
                parseManager.startParsing();
            } else {
                parseManager.stopParsing();
            }
        }
    });

    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            boolean enabled = false;
            String prop = PropertyManager.get().getProperty("parser.continuousparsing.enabled");
            if (prop != null) {
                if (prop.trim().equals("true") || prop.trim().equals("1")) {
                    enabled = true;
                }
            }
            chckbxmntmParsing.setSelected(enabled);
        }
    });

So the ParseThread will start after GUI is build.

Sorry for stealing your time

nidomiro
  • 820
  • 2
  • 10
  • 24
  • 2
    *"Sorry for stealing your time"* Speaking only for me (nobody else asks me to speak for them) but apology not accepted because you do not owe me one. Instead, **thank you** for providing the answer to your own question! Great for a Q&A site. :) – Andrew Thompson Jul 01 '12 at 14:03