Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

java - How to return value from SwingWorker class and use in other class and enable MenuItem when process is done?

I am using SwingWorker class to make a process run in another thread. What I want is, once this thread has finished processing, it should return a String and also it should enable a JMenuItem. I am using the done() method in the SwingWorker class to enable the JMenuItem but I receive a NullPinterException. The doInBackground() method returns a String which I want to access in the main GUI class - GUIMain.java, present in the same package. How should I do that? I saw many examples which implement done() or onPostExecute() methods, but I think I am going wrong somewhere. Here is the code which I have implemented:

 public class GUIMain extends JFrame implements ActionListener, FocusListener, ItemListener, MouseListener, MouseMotionListener {

        private JMenuBar menuBar;    // Defined a menuBar item
        private JMenu recalibrationMenu;  // Define the recalibration menu item
        private JMenuItem CGMenuItem;
        private JMenuItem TGMenuItem;
        private JMenu viewResultsMenu;  // Define the View Results menu item
        public JMenuItem cgResults;
        public JMenuItem tgResults;    
        private JPanel TGImage;
        private JPanel TGPanel;
        private JPanel CGImage;
        private JPanel CGPanel;
        private JPanel CGRecalibrationParameters;

        private JDialog resultsParameters;
        private JLabel massPeakLabel = new JLabel("Select mass peak (m/z)");
        private JTextField massPeakField = new JTextField(5);
        private JLabel massWindowLabel = new JLabel("Mass window (Da)");
        private JTextField massWindowField = new JTextField(5);
        private float mzPeakValue;
        private float massWindowValue;
        private JButton massPeakSelectionButton = new JButton("OK");
        private JButton CloseDialogButton = new JButton("Cancel");
        private String CGRecalibratedFilesPath;
        private String TGRecalibratedFilesPath;


        /** Constructor to setup the GUI */
        public GUIMain(String title) {

            super(title);
            setLayout(new BorderLayout());



            menuBar = new JMenuBar();
            menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

            // build the Recalibration menu
            recalibrationMenu = new JMenu("Recalibration");
            CGMenuItem = new JMenuItem("Crystal Growth");
            CGMenuItem.setEnabled(false); //initially disabled when app is launched
            CGMenuItem.addActionListener(this);

            TGMenuItem = new JMenuItem("Topological Greedy");
            TGMenuItem.setEnabled(false); //initially disabled when app is launched
            TGMenuItem.addActionListener(this);

            recalibrationMenu.add(CGMenuItem);
            recalibrationMenu.add(TGMenuItem);

            // build the View Results menu
            viewResultsMenu = new JMenu("View Results");
            cgResults = new JMenuItem("CG Recalibration");
            cgResults.setEnabled(false); //initially disabled when app is launched
            cgResults.addActionListener(this);
            tgResults = new JMenuItem("TG Recalibration");
            tgResults.setEnabled(false); //initially disabled when app is launched
            tgResults.addActionListener(this);

            viewResultsMenu.add(cgResults);
            viewResultsMenu.add(tgResults);

            // add menus to menubar
            menuBar.add(fileMenu);
            menuBar.add(recalibrationMenu);
            menuBar.add(viewResultsMenu);


            // put the menubar on the frame
            setJMenuBar(menuBar);      

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exit program if close-window button clicked
            setPreferredSize(new Dimension(1300, 800)); // Set the preferred window size
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
        }

        public GUIMain()
        {

        } 
    }


        public void actionPerformed(ActionEvent e) {

            //Handle CG menu item action
            **if ((e.getSource() == CGMenuItem)) {
                RecalibrationWorker rw = new RecalibrationWorker(file,"CG");
                rw.processCompleted();
                rw.setVisible(true);
                cgResults.setEnabled(true);
            }**


            if ((e.getSource() == cgResults)) {
                viewRecalibrationResults("CG Recalibration - View Results", "CG");
            }

            if ((e.getSource() == tgResults)) {
                viewRecalibrationResults("TG Recalibration - View Results", "TG");
            }

        }


        private void viewRecalibrationResults(String dialogTitle, final String recalibrationType) {
            resultsParameters = new JDialog();
            resultsParameters.setLayout(new GridBagLayout());
            resultsParameters.setTitle(dialogTitle);
            GridBagConstraints gc = new GridBagConstraints();

            gc.gridx = 0;
            gc.gridy = 0;
            gc.anchor = GridBagConstraints.WEST;

            gc.ipady = 20;
            // gc.anchor = GridBagConstraints.WEST;
            resultsParameters.add(massPeakLabel, gc);
            commonMassThresholdLabel.setLabelFor(massPeakField);

            gc.gridx = 1;
            gc.gridy = 0;
            gc.ipady = 0;
            gc.anchor = GridBagConstraints.CENTER;
            resultsParameters.add(massPeakField,gc);
            massPeakField.setText("");
            massPeakField.addActionListener(this);
            massPeakField.addFocusListener(this);

            gc.gridx = 0;
            gc.gridy = 1;
            gc.ipady = 20;
            gc.anchor = GridBagConstraints.WEST;
            resultsParameters.add(massWindowLabel, gc);
            massWindowLabel.setLabelFor(massWindowField);

            gc.gridx = 1;
            gc.gridy = 1;
            gc.ipady = 0;
            gc.anchor = GridBagConstraints.CENTER;
            resultsParameters.add(massWindowField,gc);
            massWindowField.setText("");
            massWindowField.addActionListener(this);
            massWindowField.addFocusListener(this);


            gc.gridx = 0;
            gc.gridy = 2;
            gc.anchor = GridBagConstraints.CENTER;
            resultsParameters.add(massPeakSelectionButton, gc);

            massPeakSelectionButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if ("CG".equals(recalibrationType))
                        recalibrationResults("CG");

                    else if ("TG".equals(recalibrationType))
                        recalibrationResults("TG");
                }
            });

            massPeakSelectionButton.addKeyListener(new KeyListener() {
                @Override
                public void keyTyped(KeyEvent e) {

                }

                @Override
                public void keyPressed(KeyEvent e) {

                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        if ("CG".equals(recalibrationType))
                            recalibrationResults("CG");

                        else if ("TG".equals(recalibrationType))
                            recalibrationResults("TG");
                    }
                }

                @Override
                public void keyReleased(KeyEvent e) {

                }
            });

            gc.gridx = 1;
            gc.gridy = 2;
            gc.anchor = GridBagConstraints.EAST;
            resultsParameters.add(CloseDialogButton,gc);
            CloseDialogButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    resultsParameters.dispose();
                }
            });

            resultsParameters.setVisible(true);
            resultsParameters.setLocationRelativeTo(this);
            resultsParameters.setSize(270, 150);
            resultsParameters.setResizable(false);
        }
    }


public class RecalibrationWorker extends JDialog {

    private boolean isStarted = false;
    private JLabel counterLabel = new JLabel("Recalibration not yet started");
    private Worker worker = new Worker();
    private JPanel recalibrationParameters;
    private ButtonGroup radioButtonGroup = new ButtonGroup();
    private JRadioButton rawSpectra = new JRadioButton("Use raw spectra");
    private JRadioButton preprocessedSpectra = new JRadioButton("Use preprocessed spectra");
    private static float commonMassThresholdValue;
    private static float recalibrationThresholdValue;
    private static double mergeSpectraThresholdValue;
    private JLabel commonMassThresholdLabel = new JLabel("<html>Common Mass Window<br>(value between 0.01-0.9 Da)</html>");
    private JTextField commonMassThresholdField = new JTextField(String.valueOf(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            commonMassThresholdValue = Float.parseFloat(commonMassThresholdField.getText());
            recalibrationThresholdField.requestFocusInWindow();

        }
    }));
    private JLabel recalibrationThresholdLabel = new JLabel("<html>Mass threshold to recalibrate two spectra<br>(value between 0.01-0.9 Da)</html>");
    private JTextField recalibrationThresholdField = new JTextField(String.valueOf(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            recalibrationThresholdValue = Float.parseFloat(recalibrationThresholdField.getText());
            mergeSpectraThresholdField.requestFocusInWindow();

        }
    }));
    private JLabel mergeSpectraThresholdLabel = new JLabel("<html>Mass threshold to merge spectra<br>(value between 0.01-0.9 Da)</html>");
    private JTextField mergeSpectraThresholdField= new JTextField(String.valueOf(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            mergeSpectraThresholdValue = Float.parseFloat(mergeSpectraThresholdField.getText());
            startButton.requestFocusInWindow();

        }
    }));
    private JTextArea recalibrationStatus = new JTextArea(7,32);
    private JScrollPane textAreaScrolling = new JScrollPane(recalibrationStatus);


    private JButton startButton = new JButton(new AbstractAction("Recalibrate") {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(!isStarted) {
                Worker w = new Worker();
                w.addPropertyChangeListener(new RecalibrationWorkerPropertyHandler(RecalibrationWorker.this));
                w.execute();
                isStarted = false;
            }
        }

    });
    private JButton stopButton = new JButton(new AbstractAction("Stop") {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            worker.cancel(true);
        }

    });

    public File file;
    public String type;
    public String resultFilePath;


    public RecalibrationWorker(File dataFile, String recalibrationType) {

        file = dataFile;
        type = recalibrationType;

        setLayout(new GridBagLayout());
        if("CG".equals(recalibrationType))
            setTitle("Crystal Growth Recalibration");
        else if("TG".equals(recalibrationType))
            setTitle("Topological Greedy Recalibration");

        recalibrationParameters = new JPanel(new GridBagLayout());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since you're using a JDialog to manage the SwingWorker, you can actual use the modal state of the dialog to you advantage.

Essentially, when a dialog is modal, it will block the code execution where the dialog is made visible. It will block until the dialog is hidden, at which time, the code will continue to execute.

So, you can disable the button before the dialog is made visible and re-enable it when it's closed.

Worker

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JButton btn = new JButton("Go");
            add(btn);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    btn.setEnabled(false);
                    SomeWorkerDialog worker = new SomeWorkerDialog(TestPane.this);
                    // This is just so we can see the button ;)
                    Point point = btn.getLocationOnScreen();
                    worker.setLocation(worker.getX(), point.y + btn.getHeight());
                    worker.setVisible(true);
                    btn.setEnabled(true);
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public class SomeWorkerDialog extends JDialog {

        private JProgressBar progressBar;
        private SomeWorkerSomeWhere worker;


        public SomeWorkerDialog(JComponent parent) {
            super(SwingUtilities.getWindowAncestor(parent), "Working Hard", DEFAULT_MODALITY_TYPE);

            progressBar = new JProgressBar();
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            gbc.insets = new Insets(10, 10, 10, 10);

            add(progressBar, gbc);

            worker = new SomeWorkerSomeWhere();
            worker.addPropertyChangeListener(new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    String name = evt.getPropertyName();
                    switch (name) {
                        case "state":
                            switch (worker.getState()) {
                                case DONE:
                                    setVisible(false);
                                    break;
                            }
                            break;
                        case "progress":
                            progressBar.setValue(worker.getProgress());
                            break;
                    }
                }
            });

            addWindowListener(new WindowAdapter() {

                @Override
                public void windowOpened(WindowEvent e) {
                    worker.execute();
                }

                @Override
                public void windowClosed(WindowEvent e) {
                    worker.cancel(true);
                }

            });

            pack();
            setLocationRelativeTo(parent);

        }

    }

    public class SomeWorkerSomeWhere extends SwingWorker {

        @Override
        protected Object doInBackground() throws Exception {
            for (int index = 0; index < 100 && !isCancelled(); index++) {
                setProgress(index);
                Thread.sleep(10);
            }
            return "AllDone";
        }

    }

}

Update

To get the value returned by the worked, you can add a method to the dialog which returns the calculated value...

public String getValue() throws Exception {
    return worker.get()
}

So, once the dialog is closed and the execution of your code continues, you can enable the components you want and call the getValue method

You can also store the result when the state changes and the PropertyChangeListener is notified


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...