Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top