Y a t-il un surdoue pour expliquer un morceau de code - Autocompletion combobox dans jtable
Bonjour à tous,
J'ai un objectif tout simple : mettre une combobox auto-completion dans une jtable. Apres plusieurs recherche j'ai trouve un dieu qui à fait ça dans un forum et je dois avouer et c'est à des années lumières de mon niveau en java.
Alors si quelqu'un pouvait m'expliquer ça serait cool.
Voici le code en question
private TableCellEditor createExampleEditor() {
La il cree la jcombobox, ça c'est facile..
Code:
1 2 3 4 5 6 7 8
|
JComboBox combo = new JComboBox(new Object[] {
"apple",
"orange",
"kiwi",
"pineapple",
"banana",
}) |
La, dèjà je comprend pas comment il possible d'ouvrir des accolades dans une nouvelle fonction
Et la, je comprend plus rien, j'ai l'impression qu'il creer une fonction dans la fonction createExampleEditor()
Mais quelle superclasse il fait appel quand il fait super.processKeyBinding puis je comprend pas il envoi des arguments qu'il à même pas renseigné avant (ks, e, condition, pressed))
Premiere méthode dans la méthode createExampleEditor()
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
int condition, boolean pressed) {
boolean retValue = super.processKeyBinding(ks, e, condition, pressed);
if (!retValue && isStartingCellEdit() && editor != null) {
// this is where the magic happens
// not quite right; sets the value, but doesn't advance the
// cursor position for AC
editor.setItem(String.valueOf(ks.getKeyChar()));
}
return retValue;
} |
Deuxieme méthode dans la méthode createExampleEditor()
Code:
1 2 3 4 5 6 7 8 9 10 11
|
private boolean isStartingCellEdit() {
JTable table = (JTable) SwingUtilities.getAncestorOfClass(
JTable.class, this);
return table != null
&& table.isFocusOwner()
&& !Boolean.FALSE.equals((Boolean) table
.getClientProperty("JTable.autoStartsEdit"));
} |
Enfin il cloture ces méthodes dans les méthodes ... je suis pommé la ...
Enfin il met à jour la combobox enfinje crois
Code:
1 2 3 4 5 6
|
AutoCompleteDecorator.decorate(combo);
return new ComboBoxCellEditor(combo);
} |
Fin de la methode createExampleEditor()
et le code dans dans sa totalité pouyr ce qui veulent tester :
Code:
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
|
import java.awt.event.KeyEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import org.jdesktop.swingx.autocomplete.ComboBoxCellEditor;
public class AutoStartEditTest extends JFrame {
protected void frameInit() {
super.frameInit();
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTable table = new JTable(2, 10);
table.getColumnModel().getColumn(0).setCellEditor(createExampleEditor());
table.setSurrendersFocusOnKeystroke(true);
add(new JScrollPane(table));
pack();
}
private TableCellEditor createExampleEditor() {
JComboBox combo = new JComboBox(new Object[] {
"apple",
"orange",
"kiwi",
"pineapple",
"banana",
})
{
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
int condition, boolean pressed) {
System.out.println("\ntouche");
// super.
boolean retValue = super.processKeyBinding(ks, e, condition, pressed);
if (!retValue && isStartingCellEdit() && editor != null) {
// this is where the magic happens
// not quite right; sets the value, but doesn't advance the
// cursor position for AC
editor.setItem(String.valueOf(ks.getKeyChar()));
}
return retValue;
}
private boolean isStartingCellEdit() {
JTable table = (JTable) SwingUtilities.getAncestorOfClass(
JTable.class, this);
return table != null
&& table.isFocusOwner()
&& !Boolean.FALSE.equals((Boolean) table
.getClientProperty("JTable.autoStartsEdit"));
}
};
AutoCompleteDecorator.decorate(combo);
return new ComboBoxCellEditor(combo);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("error setting l &f " + e);
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AutoStartEditTest().setVisible(true);
}
});
}
} |
Merci pour votre aide