Pydi
Pydi

Reputation: 77

Alert Message for unsaved changes in swings

I have a one frame which consists different components(jtextField,jcomboBox,jspinner etc)

when ever user closing the frame i want an alert message that "you have an unsaved data" if user changes some thing and not saved.

Is there any alternative way to do this other than keeping one dirty boolean variable and updating it.

Like in jquery (i searched from web)

$(document).ready(function() {
    $('input:not(:button,:submit),textarea,select').change(function () {
        warnMessage = "You have unsaved changes on this page!";
    });
    $('input:submit').click(function(e) {
        warnMessage = null;
    });
});

Thanks in advance

Upvotes: 1

Views: 371

Answers (1)

Catalina Island
Catalina Island

Reputation: 7136

Here's an outline of how to do this in Swing:

  • In a listener attached to your component's model, set a boolean when the model is changed; e.g. in a DocumentListener, set documentChanged = true.

  • Add a WindowListener; in your windowClosing() handler, check documentChanged.

  • If there's a change, use a JOptionPane to ask the user what to do.

Upvotes: 1

Related Questions