I'm trying to make a method which uses messageBox, taking into account situations in which the program exits or pressing the "X" button, to ask whether to record changes and tie it to save the code to work. If there is no change, no message is displayed.
I understand that I need to use boolean, but I failed. And the rest is the part that will attach to the result.
package nbu.university.scheduler.messageBox;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class ClosingFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public ClosingFrame() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
int showConfirmDialog = JOptionPane.
showConfirmDialog(ClosingFrame.this, "Do you want to save changes?");
if (showConfirmDialog == JOptionPane.YES_OPTION) {
final JOptionPane optionPane = new JOptionPane("Has been save", JOptionPane.INFORMATION_MESSAGE,
JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
final JDialog dialog = new JDialog();
dialog.setTitle("Message");
dialog.setModal(true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
//create timer to dispose of dialog after 1 seconds
Timer timer = new Timer(1000, new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent ae) {
dialog.dispose();
}
});
timer.setRepeats(false);//the timer should only go off once
//start timer to close JDialog as dialog modal we must start the timer before its visible
timer.start();
pack();
dialog.setVisible(true);
System.out.println("saved");
System.exit(0);
} else if (showConfirmDialog == JOptionPane.NO_OPTION) {
System.out.println("not saved");
System.exit(0);
} else {
System.out.println("aborted");
// do nothing
}
}
});
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater((Runnable) new ClosingFrame());
}
}
Аnd this is the code from the table for which I'm trying to do this
private void exit()
{
this.dispose();
}
@Override
protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
exit();
}
else
{
super.processWindowEvent(e);
}
}
...............................................................................
public void getClassSchedulerModel(int classType, int classNumber)
{
if (classType == 0)
{
int count = bachelorClassesSchedulesHolder.getComponentCount();
if (classNumber < count)
{
if (bachelorClassesSchedulesHolder.getComponent(classNumber) instanceof JTabbedPane)
{
JTabbedPane component = (JTabbedPane)bachelorClassesSchedulesHolder.getComponent(classNumber);
rewriteTableRow(component);
}
}
}
if (classType == 1)
{
int count = masterClassesSchedulesHolder.getComponentCount();
if (classNumber < count)
{
if (masterClassesSchedulesHolder.getComponent(classNumber) instanceof JTabbedPane)
{
JTabbedPane component = (JTabbedPane)masterClassesSchedulesHolder.getComponent(classNumber);
rewriteTableRow(component);
}
}
}
}