From c30835654927ab793dd84d9b1dfebc8d030149d2 Mon Sep 17 00:00:00 2001 From: MAMBILA TETE jean philipp <@mambilat> Date: Thu, 25 Apr 2024 23:51:55 +0200 Subject: [PATCH] [feat] jtable display fix and listener --- .../java/fr/u_paris/gla/project/gui/View.form | 39 +++++++- .../java/fr/u_paris/gla/project/gui/View.java | 98 +++++++++++++------ 2 files changed, 105 insertions(+), 32 deletions(-) diff --git a/src/main/java/fr/u_paris/gla/project/gui/View.form b/src/main/java/fr/u_paris/gla/project/gui/View.form index 21d3701..928c8a8 100644 --- a/src/main/java/fr/u_paris/gla/project/gui/View.form +++ b/src/main/java/fr/u_paris/gla/project/gui/View.form @@ -83,7 +83,7 @@ - + @@ -104,14 +104,45 @@ - + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/fr/u_paris/gla/project/gui/View.java b/src/main/java/fr/u_paris/gla/project/gui/View.java index d6db5f1..e6bc8ef 100644 --- a/src/main/java/fr/u_paris/gla/project/gui/View.java +++ b/src/main/java/fr/u_paris/gla/project/gui/View.java @@ -6,11 +6,9 @@ import javax.swing.*; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; +import java.awt.event.*; import java.util.ArrayList; +import java.util.stream.Collectors; public class View extends JFrame { private JPanel Cardpanel; @@ -23,19 +21,22 @@ public class View extends JFrame { private JButton searchButton; private JPanel HomePanel; private JPanel MainPanel; - private DefaultTableModel dtm; + private DefaultTableModel model; private JTable table; private JScrollPane mypane; private JPanel ItineraryPanel; private JMenuItem Itinerary; + private JPanel stationsPanel; + private JLabel departText; + private JLabel arrText; private ArrayList StopList; - private Stop departureCur; + private String departureCur; - private Stop arrivalCur; + private String arrivalCur; private String searchCur; @@ -45,9 +46,11 @@ public class View extends JFrame { public View(ArrayList s) throws HeadlessException { - this.createUIComponents(); + model = (DefaultTableModel) table.getModel(); + model.setColumnCount(2); + model.setColumnIdentifiers(new Object[]{"Line", "Stop"}); this.StopList = s; - this.StopCur = StopList.get(0); + setContentPane(MainPanel); setTitle("app"); @@ -121,8 +124,51 @@ public class View extends JFrame { Cardpanel.revalidate(); } }); + table.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + super.mouseClicked(e); + System.out.println("MouseClick: " + e.getX() + ";" + e.getY()); + showOptionsDialog(table, e.getX(), e.getY()); + + } + }); + + + mypane.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + super.mouseClicked(e); + } + }); } + private void showOptionsDialog(JTable table, int x, int y) { + int selectedRow = table.rowAtPoint(new Point(x, y)); + if (selectedRow != -1) { // If a row is selected + + String stationSel = (String) table.getValueAt(selectedRow, 1); + + // Options to set Departure, Arrival, or Cancel + Object[] options = {"Departure", "Arrival", "Cancel"}; + int choice = JOptionPane.showOptionDialog(null, "What action would you like to perform for " + stationSel + "?", "Action Selection", + JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]); + + // Handling the choice + if (choice == 0) { + this.departureCur = stationSel; + this.departText.setText("Departure: " + stationSel); + } else if (choice == 1) { + this.arrivalCur = stationSel; + this.arrText.setText("Arrival: " + stationSel); + } else { + System.out.println("rien"); + } + System.out.println("Départ: " + this.departureCur + "; Arrivée: " + this.arrivalCur); + } + } + + public static void main(String[] args) { ArrayList s = new ArrayList<>(); @@ -134,38 +180,34 @@ public class View extends JFrame { } - private void createUIComponents() { - // TODO: place custom component creation code here - dtm = new DefaultTableModel(1, 2); - - String[] header = new String[]{"Line", "Stop"}; - dtm.setColumnIdentifiers(header); - dtm.setValueAt("test", 0, 0); - dtm.setValueAt("test", 0, 1); - table = new JTable(dtm); - table.setPreferredScrollableViewportSize(table.getPreferredSize()); - - mypane = new JScrollPane(table); - - - } - public void showSearch(ArrayList stops) { // Clear existing rows from the table - dtm.setRowCount(0); + + model.setRowCount(0); + model.setColumnCount(2); + // Add new rows based on the search results count = 0; for (Stop stop : stops) { // Add a row to the table with Stop's line in the first column and Stop's name in the second column - dtm.addRow(new Object[]{stop.getLines().iterator().next(), stop.getName()}); + model.addRow(new Object[]{String.join(",", stop.getLines()), stop.getName()}); ++count; } - table.setModel(dtm); + System.out.println(stops.toString()); + for (int i = 0; i < model.getRowCount(); i++) { + for (int j = 0; j < model.getColumnCount(); j++) { + System.out.print("valeur at coord " + i +";" + j +": " + model.getValueAt(i, j) + "\t"); + } + System.out.println(); + } + + System.out.println(count); + table.revalidate(); table.repaint();