Bonjour à tous, je me penche sur un petit problème depuis ce matin j'ai quelques soucis pour le résoudre.
Je m'explique, je cherche à créé une JTable assez particuliere: cette JTable doit contenir 3 colonne, la premiere toute bete contenant du texte, pas de souci, la deuxieme par contre doit contenir une combobox avec a l'intérieur une liste de couleur. La 3eme doit contenir un boutton ( je ne m'en suis pas encore occupé)
Vous l'aurez compris, mon problème réside dans la représentation de la comboBox. Je suis parvenue jusque la à mettre ma comboBox colorée dans ma jtable mais il y a des problème lorsque je clique dessus pour faire défiler les couleurs.
Voici les différents code que j'ai utilisé:
Déclaration de ma JTable
Mon color model retourne un objet de type Color pour la colonne numero 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 colormodel = new ColorModel(tabEtatReservation); jColorTable = new JTable(colormodel); TableColumn c1 = jColorTable.getColumnModel().getColumn(1); ComboBoxPanel cbp = new ComboBoxPanel(); c1.setCellRenderer(cbp); c1.setCellEditor(new MyComboBoxEditor()); jColorTable.setCellSelectionEnabled(true); jColorTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jColorTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); TableColumn column = null; TableRow row = null; for (int i = 0; i < 3; i++) { column = jColorTable.getColumnModel().getColumn(i); column.setPreferredWidth(100); column.setMaxWidth(100); column.setMinWidth(100); column.setResizable(false); }
Classe ComboBoxPanel:
Classe MyComboBoxEditor:
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 import java.awt.Color; import java.awt.Component; import java.awt.FlowLayout; import java.util.ArrayList; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; import planning.Reservation; /** This renderer renders a color value as a panel with the given color. */ class ComboBoxPanel implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Color c = (Color)value; ArrayList colors = new ArrayList(); colors.add(Color.BLUE); colors.add(Color.BLACK); colors.add(Color.WHITE); colors.add(Color.RED); colors.add(Color.PINK); MyComboBoxModel colorsNeeded = new MyComboBoxModel(colors); JComboBox myComboBox = new JComboBox(colorsNeeded); myComboBox.setRenderer(new MyComboBoxRenderer()); if(isSelected == true){ myComboBox.setSelectedItem(c); } return myComboBox; } }
J'utilise les 2 classe MyComboBoxModel & MyComboBoxRenderer fortement inspiré de ce forum
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 import java.awt.Color; import java.awt.Component; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.EventObject; import javax.swing.AbstractCellEditor; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.TableCellEditor; public class MyComboBoxEditor extends AbstractCellEditor implements TableCellEditor { /** * Composant retourné par la fonction getTableCellEditorComponent(..) */ private JComponent component; /** * Nombre de clique à effectuer à la souris pour déclencher l'édition, et donc l'utilisation * du composant <code>component</code>. */ private int clickCountToStart = 1; public MyComboBoxEditor() { // Vide } /** {@inheritDoc} */ public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { ArrayList colors = new ArrayList(); colors.add(Color.BLUE); colors.add(Color.BLACK); colors.add(Color.WHITE); colors.add(Color.RED); colors.add(Color.PINK); MyComboBoxModel colorsNeeded = new MyComboBoxModel(colors); JComboBox myComboBox = new JComboBox(colorsNeeded); myComboBox.setRenderer(new MyComboBoxRenderer()); this.component = myComboBox; return this.component; } public Object getCellEditorValue() { return this.component; } /** * Pour gérer le nb de clique nécessaire à l'edition */ public boolean isCellEditable(EventObject anEvent) { if (anEvent instanceof MouseEvent) { return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart; } return true; } /** * Retourne le nombre de clique nécessaire à l'édition de la cellule. * * @return Retourne le nombre de clique nécessaire à l'édition de la cellule */ public int getClickCountToStart() { return clickCountToStart; } /** * Affecte le nombre de clique nécessaire à l'édition de la cellule. * * @param clickCountToStart nombre de clique */ public void setClickCountToStart(int clickCountToStart) { this.clickCountToStart = clickCountToStart; } }:
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 import javax.swing.ComboBoxModel; import javax.swing.event.ListDataListener; import java.util.ArrayList; import java.awt.Color; /** * Created by IntelliJ IDEA. * User: bebe * Date: Jun 1, 2006 * Time: 6:59:05 PM * To change this template use File | Settings | File Templates. */ public class MyComboBoxModel implements ComboBoxModel { /* My model. Really, think about a model as the data your combobox should contain. how these data should be displayed visually doesn't matter for the moment. */ private ArrayList colors = null; private Color selectedColor = null; /* You need to specify some colors in the constructor. */ public MyComboBoxModel(ArrayList colors) { if (colors == null) { /* if you don't specify any color, you will be notified ;-) */ throw new IllegalArgumentException("I need an arraylist of colors to make my job correctly."); } this.colors = colors; } /* @see javadoc ComboBoxModel. This is a method called by an internal mechanism of Swing. When, why, how comes... The short version is Swing rocks and he knows when and how to do his job! Anyway, thanks to this methid you can keep a reference on the choosen item. */ public void setSelectedItem(Object anItem) { this.selectedColor = (Color)anItem; } /* Also called by Swing. In the previous method, you kept a reference on the selected item. Now, when swing ask you wich item has been choosen, you tell him. @see javadoc ComboBoxModel. */ public Object getSelectedItem() { return selectedColor; } /* Swing needs to know how many item you combobox contains, or if you prefer, how many Color exists in you model. @see javadoc ComboBoxModel. */ public int getSize() { return colors.size(); } /* Don't you think you need to tell Swing what are the datas ? @see javadoc ComboBoxModel. */ public Object getElementAt(int index) { return colors.get(index); } /* These two next methods are imposed by ComboBoxModel We don't need them for what you need. */ public void addListDataListener(ListDataListener l) { //To change body of implemented methods use File | Settings | File Templates. } public void removeListDataListener(ListDataListener l) { //To change body of implemented methods use File | Settings | File Templates. } }Je sais que ca fait beaucoup de code j'en suis désolé, mais le souci viens je pense de la gestion de l'editor pour ma combobox.
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 import javax.swing.JLabel; import javax.swing.ListCellRenderer; import javax.swing.JList; import java.awt.Component; import java.awt.Dimension; import java.awt.Color; import java.awt.Graphics; /** * Created by IntelliJ IDEA. * User: bebe * Date: Jun 1, 2006 * Time: 7:01:53 PM * To change this template use File | Settings | File Templates. */ public class MyComboBoxRenderer extends JLabel implements ListCellRenderer { /* The background color that will be used to paint the label. */ private Color backgroundColor = null; public MyComboBoxRenderer() { /* each color is rendered as a label. [width = 200 and height = 50]. */ setPreferredSize(new Dimension(200, 50)); setOpaque(true); } /* This is also a method called by Swing. Like in the model. When why... swing knows when ;-) For each value of you model, for each colors, swing asks :"you how should I display this item ?" And what you have to do is to answer his question returning him a whatever Component (jlabel, jPanel, jTextField...). here, you return this himself because I extended JLabel, but before doing that, you need to repaint the label using the selected color. */ public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if (value != null) { backgroundColor = (Color) value; } else { backgroundColor = null; } return this; } /* Paint the label with the selected color. */ @Override public void paint(Graphics g) { setBackground(backgroundColor); super.paint(g); } }
J'avoue que je ne suis pas très callé en JTable, si jamais vous avez une quelconque idée concernant mon problème n'hesitez pas.
Merci pour votre aide.








Répondre avec citation



Partager