[chore] jtable not displayed correctly
This commit is contained in:
parent
c073af652f
commit
49bc9a038c
2 changed files with 112 additions and 44 deletions
|
@ -58,7 +58,7 @@
|
|||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Search itinerary"/>
|
||||
<text value="Search stop"/>
|
||||
</properties>
|
||||
</component>
|
||||
<vspacer id="8b80">
|
||||
|
@ -100,6 +100,7 @@
|
|||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="4" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<labelFor value="44153"/>
|
||||
<text value="Network"/>
|
||||
</properties>
|
||||
</component>
|
||||
|
@ -109,7 +110,12 @@
|
|||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
<children>
|
||||
<component id="2046e" class="javax.swing.JTable" binding="table" custom-create="true">
|
||||
<constraints/>
|
||||
<properties/>
|
||||
</component>
|
||||
</children>
|
||||
</scrollpane>
|
||||
</children>
|
||||
</grid>
|
||||
|
@ -135,9 +141,18 @@
|
|||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="30e2e" binding="ItineraryPanel" layout-manager="GridLayoutManager" row-count="1" 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>
|
||||
<card name="Card4"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="67039" class="javax.swing.JMenuBar" layout-manager="GridLayoutManager" row-count="7" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="67039" class="javax.swing.JMenuBar" layout-manager="GridLayoutManager" row-count="9" 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>
|
||||
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
|
@ -156,13 +171,6 @@
|
|||
<text value="Home"/>
|
||||
</properties>
|
||||
</component>
|
||||
<vspacer id="9d1fd">
|
||||
<constraints>
|
||||
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="102" height="14"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="43d5e" class="javax.swing.JMenuItem" binding="Network">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
|
@ -204,6 +212,26 @@
|
|||
</grid>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="664f9" class="javax.swing.JMenuItem" binding="Itinerary">
|
||||
<constraints>
|
||||
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="102" height="20"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Itinerary"/>
|
||||
</properties>
|
||||
</component>
|
||||
<vspacer id="936ed">
|
||||
<constraints>
|
||||
<grid row="6" 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>
|
||||
<vspacer id="c45a6">
|
||||
<constraints>
|
||||
<grid row="8" 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>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
|
|
|
@ -4,6 +4,7 @@ import fr.u_paris.gla.project.itinerary.Stop;
|
|||
|
||||
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;
|
||||
|
@ -11,7 +12,7 @@ import java.awt.event.KeyAdapter;
|
|||
import java.awt.event.KeyEvent;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class View extends JFrame{
|
||||
public class View extends JFrame {
|
||||
private JPanel Cardpanel;
|
||||
private JMenuItem Home;
|
||||
private JMenuItem Network;
|
||||
|
@ -27,13 +28,22 @@ public class View extends JFrame{
|
|||
private JTable table;
|
||||
|
||||
private JScrollPane mypane;
|
||||
private JPanel ItineraryPanel;
|
||||
private JMenuItem Itinerary;
|
||||
|
||||
private ArrayList<Stop> StopList;
|
||||
|
||||
private Stop StopCur;
|
||||
private Stop departureCur;
|
||||
|
||||
private Stop arrivalCur;
|
||||
|
||||
private String searchCur;
|
||||
|
||||
private ArrayList<Stop> searchRes;
|
||||
|
||||
private int count = 0;
|
||||
|
||||
|
||||
public View(ArrayList<Stop> s) throws HeadlessException {
|
||||
this.createUIComponents();
|
||||
this.StopList = s;
|
||||
|
@ -43,7 +53,8 @@ public class View extends JFrame{
|
|||
setTitle("app");
|
||||
setExtendedState(JFrame.MAXIMIZED_BOTH);
|
||||
//setUndecorated(true);
|
||||
setVisible(true);;
|
||||
setVisible(true);
|
||||
;
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
Home.addActionListener(new ActionListener() {
|
||||
|
@ -59,8 +70,10 @@ public class View extends JFrame{
|
|||
Network.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
showSearch(s);
|
||||
Cardpanel.removeAll();
|
||||
Cardpanel.add(NetworkPanel);
|
||||
|
||||
Cardpanel.repaint();
|
||||
Cardpanel.revalidate();
|
||||
}
|
||||
|
@ -82,23 +95,40 @@ public class View extends JFrame{
|
|||
public void keyReleased(KeyEvent e) {
|
||||
super.keyReleased(e);
|
||||
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||
System.out.println("Enter key released");
|
||||
|
||||
searchCur = textField1.getText();
|
||||
showSearch(s);
|
||||
System.out.println("Enter key released with text " + searchCur);
|
||||
Cardpanel.removeAll();
|
||||
Cardpanel.add(NetworkPanel);
|
||||
|
||||
Cardpanel.repaint();
|
||||
Cardpanel.revalidate();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
searchButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Cardpanel.removeAll();
|
||||
searchCur = textField1.getText();
|
||||
showSearch(s);
|
||||
System.out.println("search button clicked with text " + searchCur);
|
||||
Cardpanel.add(NetworkPanel);
|
||||
|
||||
Cardpanel.repaint();
|
||||
Cardpanel.revalidate();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Stop> s = new ArrayList<>();
|
||||
s.add(new Stop("M8", "Balard", 1.0315897, 3.0265513));
|
||||
s.add(new Stop("M14", "Gare de Lyon", 2.4658452681, 3.0265513));
|
||||
System.out.println(s.toString());
|
||||
View v = new View(s);
|
||||
v.showSearch(s);
|
||||
|
||||
|
||||
}
|
||||
|
@ -107,51 +137,61 @@ public class View extends JFrame{
|
|||
private void createUIComponents() {
|
||||
// TODO: place custom component creation code here
|
||||
|
||||
table = new JTable();
|
||||
dtm = new DefaultTableModel(0, 0);
|
||||
|
||||
String[] header = new String[] { "Line", "Stop" };
|
||||
dtm = new DefaultTableModel(1, 2);
|
||||
|
||||
String[] header = new String[]{"Line", "Stop"};
|
||||
dtm.setColumnIdentifiers(header);
|
||||
table.setModel(dtm);
|
||||
dtm.setValueAt("test", 0, 0);
|
||||
dtm.setValueAt("test", 0, 1);
|
||||
table = new JTable(dtm);
|
||||
table.setPreferredScrollableViewportSize(table.getPreferredSize());
|
||||
|
||||
mypane = new JScrollPane(table);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void showSearch(ArrayList<Stop> stops) {
|
||||
public void showSearch(ArrayList<Stop> stops) {
|
||||
// Clear existing rows from the table
|
||||
dtm.setRowCount(0);
|
||||
|
||||
// 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(), stop.getName()});
|
||||
dtm.addRow(new Object[]{stop.getLines().iterator().next(), stop.getName()});
|
||||
++count;
|
||||
}
|
||||
this.displayTableValues();
|
||||
dtm.fireTableDataChanged();
|
||||
table.repaint();
|
||||
|
||||
table.setModel(dtm);
|
||||
table.revalidate();
|
||||
Cardpanel.repaint();
|
||||
Cardpanel.revalidate();
|
||||
table.repaint();
|
||||
|
||||
mypane.setViewportView(table);
|
||||
mypane.revalidate();
|
||||
mypane.repaint();
|
||||
|
||||
NetworkPanel.revalidate();
|
||||
NetworkPanel.repaint();
|
||||
|
||||
|
||||
this.displayTableValues();
|
||||
|
||||
}
|
||||
|
||||
private void displayTableValues() {
|
||||
int rowCount = dtm.getRowCount();
|
||||
int columnCount = dtm.getColumnCount();
|
||||
|
||||
// Iterate over each row
|
||||
for (int row = 0; row < rowCount; row++) {
|
||||
// Iterate over each column
|
||||
for (int col = 0; col < columnCount; col++) {
|
||||
// Get the value at the current cell
|
||||
Object value = dtm.getValueAt(row, col);
|
||||
// Display the value
|
||||
System.out.println("Value at row " + row + ", column " + col + ": " + value);
|
||||
public void displayTableValues() {
|
||||
TableModel mod = table.getModel();
|
||||
for (int row = 0; row < mod.getRowCount(); row++) {
|
||||
for (int column = 0; column < mod.getColumnCount(); column++) {
|
||||
System.out.print(mod.getValueAt(row, column).toString() + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.print(";");
|
||||
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue