[feat] jtable display fix and listener

This commit is contained in:
MAMBILA TETE jean philipp 2024-04-25 23:51:55 +02:00
parent 49bc9a038c
commit c308356549
2 changed files with 105 additions and 32 deletions

View file

@ -83,7 +83,7 @@
</hspacer> </hspacer>
</children> </children>
</grid> </grid>
<grid id="184ef" binding="NetworkPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="184ef" binding="NetworkPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<card name="Card2"/> <card name="Card2"/>
@ -104,14 +104,45 @@
<text value="Network"/> <text value="Network"/>
</properties> </properties>
</component> </component>
<scrollpane id="44153" binding="mypane" custom-create="true"> <grid id="7f3c9" binding="stationsPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <children>
<component id="2046e" class="javax.swing.JTable" binding="table" custom-create="true"> <component id="35d9e" class="javax.swing.JLabel" binding="departText">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Departure:"/>
</properties>
</component>
<vspacer id="f3812">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="33aa0" class="javax.swing.JLabel" binding="arrText">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Arrival:"/>
</properties>
</component>
</children>
</grid>
<scrollpane id="44153" binding="mypane">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="2046e" class="javax.swing.JTable" binding="table">
<constraints/> <constraints/>
<properties/> <properties/>
</component> </component>

View file

@ -6,11 +6,9 @@ import javax.swing.*;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel; import javax.swing.table.TableModel;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.stream.Collectors;
public class View extends JFrame { public class View extends JFrame {
private JPanel Cardpanel; private JPanel Cardpanel;
@ -23,19 +21,22 @@ public class View extends JFrame {
private JButton searchButton; private JButton searchButton;
private JPanel HomePanel; private JPanel HomePanel;
private JPanel MainPanel; private JPanel MainPanel;
private DefaultTableModel dtm; private DefaultTableModel model;
private JTable table; private JTable table;
private JScrollPane mypane; private JScrollPane mypane;
private JPanel ItineraryPanel; private JPanel ItineraryPanel;
private JMenuItem Itinerary; private JMenuItem Itinerary;
private JPanel stationsPanel;
private JLabel departText;
private JLabel arrText;
private ArrayList<Stop> StopList; private ArrayList<Stop> StopList;
private Stop departureCur; private String departureCur;
private Stop arrivalCur; private String arrivalCur;
private String searchCur; private String searchCur;
@ -45,9 +46,11 @@ public class View extends JFrame {
public View(ArrayList<Stop> s) throws HeadlessException { public View(ArrayList<Stop> s) throws HeadlessException {
this.createUIComponents(); model = (DefaultTableModel) table.getModel();
model.setColumnCount(2);
model.setColumnIdentifiers(new Object[]{"Line", "Stop"});
this.StopList = s; this.StopList = s;
this.StopCur = StopList.get(0);
setContentPane(MainPanel); setContentPane(MainPanel);
setTitle("app"); setTitle("app");
@ -121,7 +124,50 @@ public class View extends JFrame {
Cardpanel.revalidate(); 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) { public static void main(String[] args) {
@ -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<Stop> stops) { public void showSearch(ArrayList<Stop> stops) {
// Clear existing rows from the table // Clear existing rows from the table
dtm.setRowCount(0);
model.setRowCount(0);
model.setColumnCount(2);
// Add new rows based on the search results // Add new rows based on the search results
count = 0; count = 0;
for (Stop stop : stops) { 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 // 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; ++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.revalidate();
table.repaint(); table.repaint();