2

I'm posting the full code below, but basically I have a class which has a dialog containing a scrollpane that holds a jlist. It is set to "DISPOSE_ON_CLOSE", and I've tried setting every single variable that is ever created to null after the value is obtained, yet javaw.exe will continue to run indefinitely unless I forcibly close it.

In case more information is needed, here's a quick explanation. This class is meant to create a dialog and show it, wait for the user input, and return the chosen text. It already does this. But for some reason, it continues to run in the background after it's finished.

This is meant for an application, so having Java run in the background non-stop is not an appealing prospect. I really don't know what else to try at this point. Below is my code.

package (REMOVED);

import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class PatientScrollPane implements ListSelectionListener, MouseListener {
    private String currentPatient;
    private JList patientList;
    private JDialog dialog;
    private JFrame frame;
    private JScrollPane scrollPane;
    private static int MAX_VISIBLE_ROW_COUNT = 15;

    public void setDialog(JDialog dialog) {
        this.dialog = dialog;
    }

    public JList getPatientList() {
        return patientList;
    }

    public void setPatientList(JList patientList) {
        this.patientList = patientList;
    }

    public String getCurrentPatient() {
        return currentPatient;
    }

    public void setCurrentPatient(String currentPatient) {
        this.currentPatient = currentPatient;
    }

    public JDialog getDialog() {
        return dialog;
    }

    public JFrame getFrame() {
        return frame;
    }

    public void setFrame(JFrame frame) {
        this.frame = frame;
    }

    public JScrollPane getScrollPane() {
        return scrollPane;
    }

    public void setScrollPane(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
    }

    public PatientScrollPane() {
        this(null);
    }

    public PatientScrollPane(JComponent locationRelativeToComponent) {
        this(locationRelativeToComponent, new JList(fakePatientList()));
    }

    public PatientScrollPane(JComponent locationRelativeToComponent, JList patientList) {
        this.patientList = patientList;
        patientList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        frame = new JFrame("Patient List");

        dialog = new JDialog(frame);
        dialog.setLocationRelativeTo(locationRelativeToComponent);

        patientList.addListSelectionListener(this);
        patientList.addMouseListener(this);
        setVisibleRowCount();

        scrollPane = new JScrollPane(patientList);
        dialog.getContentPane().add(scrollPane, BorderLayout.CENTER);

        dialog.pack();
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dialog.setModalityType(ModalityType.APPLICATION_MODAL);
        dialog.setVisible(true);
    }

    private void setVisibleRowCount() {
        int size = patientList.getModel().getSize();

        if(size <= MAX_VISIBLE_ROW_COUNT) {
            patientList.setVisibleRowCount(size);
        } else {
            patientList.setVisibleRowCount(MAX_VISIBLE_ROW_COUNT);
        }
    }

    public static String[] fakePatientList() {
        String[] patients = new String[20];
        for(int i = 0; i < patients.length; i++) {
            patients[i] = "Patient " + i;
        }

        return patients;
    }

    public static String getPatient() {
        PatientScrollPane patientScrollPane = new PatientScrollPane();
        String patient = patientScrollPane.getCurrentPatient();
        patientScrollPane.setPatientList(null);
        patientScrollPane.setDialog(null);
        patientScrollPane.setCurrentPatient(null);
        patientScrollPane.setFrame(null);
        patientScrollPane.setScrollPane(null);
        patientScrollPane = null;

        return patient;
    }

    public static String getPatient(JComponent locationRelativeToComponent) {
        PatientScrollPane patientScrollPane = new PatientScrollPane(locationRelativeToComponent);

        return patientScrollPane.getCurrentPatient();
    }

    public static String getPatient(JComponent locationRelativeToComponent, JList patientList) {
        PatientScrollPane patientScrollPane = new PatientScrollPane(locationRelativeToComponent, patientList);

        String patient = patientScrollPane.getCurrentPatient();
        return patient;
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        currentPatient = (String)patientList.getSelectedValue();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int index = patientList.locationToIndex(e.getPoint());
        patientList.setSelectedIndex(index);
        dialog.dispose();
    }

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    public static void main(String[] args) {
        String patient = PatientScrollPane.getPatient();

        System.out.println("Chosen patient: " + patient);
    }
}

(I tried looking high and low for a solution to a similar problem, but it's sort of too generic to find.)

2 Answers 2

4

As long as your JFrame exists, the event dispatch thread will keep running and will keep the JVM alive since the JFrame, a top-level window initiates a non-daemon thread.

For more on this, please look here.

And for example,...

import javax.swing.JDialog;
import javax.swing.JFrame;

public class DialogAndDaemons {
   public static void main(String[] args) {
      JFrame frame = new JFrame();

      JDialog dialog = new JDialog(frame, "Dialog");
      dialog.pack();
      dialog.setLocationRelativeTo(null);
      dialog.setVisible(true);
   }
}
1
  • This fixed the problem, all I needed was to add this line of code: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Thank you!
    – Asaf
    Oct 17, 2011 at 2:31
3

After dialog.dispose();, use System.exit(0);

3
  • That would work, for the example in main... but this class is called from a much larger application. If I use system.exit(0) won't it exit the whole application?
    – Asaf
    Oct 17, 2011 at 2:22
  • 1
    If you want the application to continue running then why are you worried about javaw.exe?
    – Paul
    Oct 17, 2011 at 2:26
  • Because even after the rest of the application has finished running javaw.exe is still going... but now I feel like an idiot because I could call system.exit(0) when I exit the full application. So thank you for that solution... I'm going to try the answer posted below first though.
    – Asaf
    Oct 17, 2011 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.