Bonjour
je me tourne vers vous car je ne parviens pas à afficher des lignes dans une JTable. Je m'explique :
J'ai un JPanel qui contient une JTable "Table1" et un JButton "ouvrir". Ce bouton permet d'ouvrir une JDialog qui contient également une JTable "Table2" et un bouton "ajouter".
J'aimerais récupérer les éléments sélectionnés dans cette Table2 et en cliquant sur ajouter qu'ils apparaissent dans la table1.
Je ne parviens pas à trouver l'erreur dans le code
Merci d'avance de votre aide
Voici le code
Le panneau principal
La table 1
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JScrollPane; public class Pan extends JPanel implements ActionListener { private final JButton plusButton; private final JScrollPane scrollPane; private final Dialog propertySelection; private final Table1 propertyTable; public Pan (Dialog propertySelection) { super(new GridBagLayout()); this.propertyTable = new Table1(); this.plusButton = new JButton("Ouvrir"); this.scrollPane = new JScrollPane(); this.propertySelection = propertySelection; initializeGUI(); initializeListener(); } private void initializeGUI() { scrollPane.setViewportView(propertyTable); add(plusButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); add(scrollPane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); setVisible(true); } private void initializeListener() { plusButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { if ((e.getSource() == plusButton)) { propertySelection.setVisible(true); } } }
La JDialog
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 import java.awt.Component; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JTable; import javax.swing.SwingConstants; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableRowSorter; public class Table1 extends JTable { private static final String[] columnName = { "Col1", "Col2", "Col3" }; private final TableRowSorter<Model1> sorter; private final Model1 model; private final List<String> liste = new ArrayList<String>(); public Table1() { super(); model = new Model1(); setModel(model); sorter = new TableRowSorter<Model1>(model); setDefaultRenderer(JComponent.class, new TableRenderer()); setRowSelectionAllowed(true); model.fireTableStructureChanged(); } public void setNames(final List<String> items) { model.setNames(items); } public class TableRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { setHorizontalAlignment(SwingConstants.CENTER); final int rowModel = sorter.convertRowIndexToModel(row); if (value instanceof JButton) return (JButton) value; else return this; } } // Implementation du modele de Table1 class Model1 extends AbstractTableModel { public void setNames(final List<String> ajout) { liste.clear(); final Set<String> uniquesNames = new TreeSet<String>(ajout); liste.addAll(uniquesNames); System.out.println("/******Ajout********/"); Object [] obj = liste.toArray(); for (Object object : obj) System.out.println(object); fireTableStructureChanged(); } public int getRowCount() { return Math.max(0, liste.size()); } public int getColumnCount() { return columnName.length; } public Object getValueAt(int rowIndex, int columnIndex) { if (columnIndex == 0) return new JButton("btn1"); else if (columnIndex == 2) return new JButton("btn2"); else return liste.get(rowIndex); } public String getColumnName(int columnIndex) { return columnName[columnIndex]; } public Class<?> getColumnClass(int column) { if ((column == 0)||(column == 2)) return JButton.class; else return String.class; } public boolean isCellEditable(int row, int column) { return false; } } }
La Table2
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JScrollPane; public class Dialog extends JDialog implements ActionListener{ private final JScrollPane scrollPane; private final JButton buttonAdd; protected Table2 panelParameters; private Table1 table1; public Dialog(Table1 table1) { this.table1 = table1; this.setSize(350, 500); this.setLocationRelativeTo(null); this.scrollPane = new JScrollPane(); this.panelParameters = new Table2(); this.buttonAdd = new JButton("Ajouter"); initializeListener(); initializeGUI(); this.setVisible(false); } private void initializeListener() { buttonAdd.addActionListener(this); } private void initializeGUI() { scrollPane.setViewportView(panelParameters); this.setLayout(new GridBagLayout()); this.add(scrollPane, new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); this.add(buttonAdd, new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonAdd) { final List<String> selection = panelParameters.getSelectedNames(); table1.setNames(selection); } } }Le main
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 import java.awt.Component; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JTable; import javax.swing.SwingConstants; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableRowSorter; public class Table2 extends JTable { private static final String[] columnName = { "Prenoms"}; private final TableRowSorter<Model2> sorter; private final Model2 model; private List<String> prenoms; String[] tab = {"prenom1", "prenom2", "prenom3"}; public Table2() { this.prenoms= new ArrayList<String>(); prenoms.add(tab[0]); prenoms.add(tab[1]); prenoms.add(tab[2]); Object[][] data = { {prenoms.get(0)}, {prenoms.get(1)}, {prenoms.get(2)} }; this.model = new Model2(data, columnName); sorter = new TableRowSorter<Model2>(model); setModel(model); setDefaultRenderer(JComponent.class, new TableRenderer()); setRowSelectionAllowed(true); model.fireTableStructureChanged(); } public void setAllSelected(final boolean selectAll) { final int viewRowCount = model.getRowCount(); if (viewRowCount <= 0) return; if (selectAll) addRowSelectionInterval(0, viewRowCount - 1); else removeRowSelectionInterval(0, viewRowCount - 1); } public List<String> getSelectedNames() { final List<String> selection = new ArrayList<String>(getSelectedRowCount()); for (int i : getSelectedRows()) { selection.add(prenoms.get(sorter.convertRowIndexToModel(i))); } return selection; } //Renderer pour definir la façon de dessiner les composants dans les cellules public class TableRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { setHorizontalAlignment(SwingConstants.CENTER); final int rowModel = sorter.convertRowIndexToModel(row); return this; } } class Model2 extends AbstractTableModel { Object[][] data; String[] columnName; public Model2(Object[][] data, String[] columnName) { this.data = data; this.columnName = columnName; } @Override public int getColumnCount() { return columnName.length; } @Override public int getRowCount() { if (data == null) return 0; return data.length; } @Override public Object getValueAt(int rowIndex, int columnIndex) { return data[rowIndex][columnIndex]; } @Override public String getColumnName(int columnIndex) { return columnName[columnIndex]; } // On retourne le type de la cellule à la colonne demandée @Override public Class<?> getColumnClass(int column) { return this.data[0][column].getClass(); } @Override public boolean isCellEditable(int row, int column) { return false; } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import javax.swing.JFrame; public class Test { public static void main(String[] args) { Table1 table1 = new Table1(); JFrame frame = new JFrame(); Pan panneau = new Pan(new Dialog(table1)); frame.setContentPane(panneau); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
Partager