From f496eb89b12fb87fe2a4eb0aa3754338fd0998ce Mon Sep 17 00:00:00 2001 From: tgiraud Date: Thu, 17 Dec 2020 12:20:51 +0100 Subject: [PATCH] Quand on ferme l'interface, on ferme tout --- .../Clavardage/src/view/Interface.java | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/Application/Clavardage/src/view/Interface.java b/Application/Clavardage/src/view/Interface.java index 7d6669d..c9f45c4 100644 --- a/Application/Clavardage/src/view/Interface.java +++ b/Application/Clavardage/src/view/Interface.java @@ -4,14 +4,18 @@ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.IOException; -import java.util.ArrayList; import controller.Controller; 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 JTextField PseudotextField; @@ -28,6 +32,14 @@ public class Interface implements ActionListener { public Interface(Controller 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() { @@ -56,7 +68,7 @@ public class Interface implements ActionListener { Messagelabel.setLabelFor(MessagetextField); MessagetextField.setVisible(false); Messagelabel.setVisible(false); - + /* * An easy way to put space between a top-level container * and its contents is to put the contents in a JPanel @@ -101,8 +113,6 @@ public class Interface implements ActionListener { }else { 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))); - //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))); } @@ -156,10 +166,17 @@ public class Interface implements ActionListener { JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. - JFrame frame = new JFrame("SwingApplication"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - + JFrame frame = new JFrame("Clavardage App"); + 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); Component contents = app.createComponents(); @@ -172,7 +189,13 @@ public class Interface implements ActionListener { 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 {