Quand on ferme l'interface, on ferme tout

This commit is contained in:
Theau Giraud 2020-12-17 12:20:51 +01:00
parent fcb1ccf532
commit f496eb89b1

View file

@ -4,14 +4,18 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import controller.Controller; import controller.Controller;
import model.RemoteUser; import model.RemoteUser;
public class Interface implements ActionListener { public class Interface extends JFrame implements ActionListener, WindowListener {
public Controller hisController; /**
*
*/
private static final long serialVersionUID = 1L; //JFrame stuff
public static Controller hisController;
public JLabel Pseudolabel; public JLabel Pseudolabel;
public JTextField PseudotextField; public JTextField PseudotextField;
@ -28,6 +32,14 @@ public class Interface implements ActionListener {
public Interface(Controller controller) { public Interface(Controller controller) {
this.hisController = controller; this.hisController = controller;
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.out.println("Window closing");
}
});
} }
public Component createComponents() { public Component createComponents() {
@ -101,8 +113,6 @@ public class Interface implements ActionListener {
}else { }else {
JComboBox cb = (JComboBox)e.getSource(); //Casts obscurs pour récupérer le numéro du user dans la liste JComboBox cb = (JComboBox)e.getSource(); //Casts obscurs pour récupérer le numéro du user dans la liste
int selectedUsernb = Integer.parseInt(String.valueOf(((String) cb.getSelectedItem()).charAt(1))); int selectedUsernb = Integer.parseInt(String.valueOf(((String) cb.getSelectedItem()).charAt(1)));
//System.out.println("selectedUsernb: " + selectedUsernb); //Test pour voir si ça marche
//TODO Utiliser ce selectedUsernb pour faire le lien avec openSession de Controller
hisController.openSession(hisController.getMyUser().addChats(hisController.getMyUser().getRemoteUsersList().get(selectedUsernb))); hisController.openSession(hisController.getMyUser().addChats(hisController.getMyUser().getRemoteUsersList().get(selectedUsernb)));
} }
@ -156,10 +166,17 @@ public class Interface implements ActionListener {
JFrame.setDefaultLookAndFeelDecorated(true); JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window. //Create and set up the window.
JFrame frame = new JFrame("SwingApplication"); JFrame frame = new JFrame("Clavardage App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.out.println("GUI has been closed");
hisController.interfaceRunning = false;
System.exit(0);
}
});
Interface app = new Interface(controller); Interface app = new Interface(controller);
Component contents = app.createComponents(); Component contents = app.createComponents();
@ -172,7 +189,13 @@ public class Interface implements ActionListener {
return app; return app;
} }
public void windowOpened(WindowEvent e) {} //Had to implement it for WindowListener
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {}
/* /*
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {