boubavic | bon ca a un peu changer ce que je voulais faire ...Mais j'arrive pas encore a le faire ...donc Voici ce que je voudrais faire maintenant :
Donc ca part toujours d'un JComboBox editable
Qd je commence a saisir ma chaine, des que je commence a saisir, la premiere chaine qui convient doit apparaitre dans le champs editable... ensuite je continue a saisir...Je tape 1 caractere, si celui ci correspond toujours a la chaine actuelle ca ben je fais juste avance le curseur de saisi...Jusqu'a la fin du mot...
Si le mot n'apparait pas ben je peux saisir normalement...
J'ai deja fait un truc mais ca marche pas terrible terrible ...
Et en fait j'ai modifier cette classe pour pouvoir gerer les evenements clavier sur l'editor associé au JComboBox
Code :
- /*
- * Created on 4 juin 2004
- *
- * To change the template for this generated file go to
- * Window - Preferences - Java - Code Generation - Code and Comments
- */
- package IHM;
- /**
- * @author Vic
- *
- * To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Generation - Code and Comments
- */
- /*
- * @(#)BasicComboBoxEditor.java 1.25 03/01/23
- *
- * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
- import javax.swing.*;
- import javax.swing.border.Border;
- import java.awt.*;
- import java.awt.event.*;
- import java.lang.reflect.Method;
- /**
- * The default editor for editable combo boxes. The editor is implemented as a JTextField.
- *
- * @version 1.25 01/23/03
- * @author Arnaud Weber
- * @author Mark Davidson
- */
- public class MyBasicComboBoxEditor implements ComboBoxEditor,FocusListener {
- protected JTextField editor;
- private Object oldValue;
- public MyBasicComboBoxEditor() {
- editor = new BorderlessTextField("",9);
- editor.setBorder(null);
- }
- public Component getEditorComponent() {
- return editor;
- }
- /**
- *
- * @param i
- */
- public void setCursorPosition(int i) {
- editor.setCaretPosition(i);
- }
- /**
- *
- * @return
- */
- public int getCursorPosition() {
- return editor.getCaretPosition();
- }
- /**
- *
- * @param start
- * @param end
- */
- public void textSelection(int start,int end) {
- editor.select(start,end);
- }
- /**
- *
- * @param string
- */
- public void replaceSelection(String string) {
- editor.replaceSelection(string);
- }
- /**
- *
- * @return
- */
- public int getSelectionStart() {
- return editor.getSelectionStart();
- }
- /**
- *
- * @return
- */
- public int getSelectionEnd() {
- return editor.getSelectionEnd();
- }
- /**
- * Sets the item that should be edited.
- *
- * @param anObject the displayed value of the editor
- */
- public void setItem(Object anObject) {
- if ( anObject != null ) {
- editor.setText(anObject.toString());
- oldValue = anObject;
- } else {
- editor.setText("" );
- }
- }
- public Object getItem() {
- Object newValue = editor.getText();
- if (oldValue != null && !(oldValue instanceof String)) {
- // The original value is not a string. Should return the value in it's
- // original type.
- if (newValue.equals(oldValue.toString())) {
- return oldValue;
- } else {
- // Must take the value from the editor and get the value and cast it to the new type.
- Class cls = oldValue.getClass();
- try {
- Method method = cls.getMethod("valueOf", new Class[]{String.class});
- newValue = method.invoke(oldValue, new Object[] { editor.getText()});
- } catch (Exception ex) {
- // Fail silently and return the newValue (a String object)
- }
- }
- }
- return newValue;
- }
- public void selectAll() {
- editor.selectAll();
- editor.requestFocus();
- }
- // This used to do something but now it doesn't. It couldn't be
- // removed because it would be an API change to do so.
- public void focusGained(FocusEvent e) {}
- // This used to do something but now it doesn't. It couldn't be
- // removed because it would be an API change to do so.
- public void focusLost(FocusEvent e) {}
- public void addActionListener(ActionListener l) {
- editor.addActionListener(l);
- }
- public void removeActionListener(ActionListener l) {
- editor.removeActionListener(l);
- }
- public void addKeyListener(KeyListener k) {
- editor.addKeyListener(k);
- }
- public void removeKeyListener(KeyListener k) {
- editor.removeKeyListener(k);
- }
- static class BorderlessTextField extends JTextField {
- public BorderlessTextField(String value,int n) {
- super(value,n);
- }
- // workaround for 4530952
- public void setText(String s) {
- if (getText().equals(s)) {
- return;
- }
- super.setText(s);
- }
- public void setBorder(Border b) {}
- }
- /**
- * A subclass of BasicComboBoxEditor that implements UIResource.
- * BasicComboBoxEditor doesn't implement UIResource
- * directly so that applications can safely override the
- * cellRenderer property with BasicListCellRenderer subclasses.
- * <p>
- * <strong>Warning:</strong>
- * Serialized objects of this class will not be compatible with
- * future Swing releases. The current serialization support is
- * appropriate for short term storage or RMI between applications running
- * the same version of Swing. As of 1.4, support for long term storage
- * of all JavaBeans<sup><font size="-2">TM</font></sup>
- * has been added to the <code>java.beans</code> package.
- * Please see {@link java.beans.XMLEncoder}.
- */
- public static class UIResource extends MyBasicComboBoxEditor
- implements javax.swing.plaf.UIResource {
- }
- }
|
Message édité par boubavic le 04-06-2004 à 20:51:26
|