Bom dia!
Estava precisando de uma classe para gerar um ComboBox Auto Completável, e encontrei este código em:
package com.eby.autocomplete; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.scene.control.ComboBox; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; public class ComboBoxAuto<T> implements EventHandler<KeyEvent> { private ComboBox comboBox; private StringBuilder sb; private ObservableList<T> data; private boolean moveCaretToPos = false; private int caretPos; public ComboBoxAuto(final ComboBox comboBox) { this.comboBox = comboBox; sb = new StringBuilder(); data = comboBox.getItems(); this.comboBox.setEditable(true); this.comboBox.setOnKeyPressed(new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent t) { comboBox.hide(); } }); this.comboBox.setOnKeyReleased(ComboBoxAuto.this); } @Override public void handle(KeyEvent event) { if(event.getCode() == KeyCode.UP) { caretPos = -1; moveCaret(comboBox.getEditor().getText().length()); return; } else if(event.getCode() == KeyCode.DOWN) { if(!comboBox.isShowing()) { comboBox.show(); } caretPos = -1; moveCaret(comboBox.getEditor().getText().length()); return; } else if(event.getCode() == KeyCode.BACK_SPACE) { moveCaretToPos = true; caretPos = comboBox.getEditor().getCaretPosition(); } else if(event.getCode() == KeyCode.DELETE) { moveCaretToPos = true; caretPos = comboBox.getEditor().getCaretPosition(); } if (event.getCode() == KeyCode.RIGHT || event.getCode() == KeyCode.LEFT || event.isControlDown() || event.getCode() == KeyCode.HOME || event.getCode() == KeyCode.END || event.getCode() == KeyCode.TAB) { return; } ObservableList list = FXCollections.observableArrayList(); for (int i=0; i<data.size(); i++) { if(data.get(i).toString().toLowerCase().startsWith( ComboBoxAuto.this.comboBox .getEditor().getText().toLowerCase())) { list.add(data.get(i)); } } String t = comboBox.getEditor().getText(); comboBox.setItems(list); comboBox.getEditor().setText(t); if(!moveCaretToPos) { caretPos = -1; } moveCaret(t.length()); if(!list.isEmpty()) { comboBox.show(); } } private void moveCaret(int textLength) { if(caretPos == -1) { comboBox.getEditor().positionCaret(textLength); } else { comboBox.getEditor().positionCaret(caretPos); } moveCaretToPos = false; } }
Funciona muito bem para o propósito, o problema vem na hora de pegar o item que foi selecionado neste ComboBox para passar para a base de dados.
Exemplos dos códigos:
Variável da classe do ComboBoxAuto:
private ComboBoxAuto aCbox;
Tipo do ComboBox:
@FXML private ComboBox cboxEGTIEquipi;
Parte do método que popula o ComboBox e converte para o ComboBoxAuto:
cboxEGTIEquipi.setItems (listaEquipGTI);
aCboxUsu =new ComboBoxAuto(cboxEGTIEquipi);
Parte do método que usava para pegar o item do ComboBox:
EquipInstal equipi = new EquipInstal(
cboxEGTIEquipi.getSelectionModel().getSelectedItem());
O erro que gera:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Node.fireEvent(Node.java:8413) at javafx.scene.control.Button.fire(Button.java:185) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) at com.sun.glass.ui.View.handleMouseEvent(View.java:555) at com.sun.glass.ui.View.notifyMouse(View.java:937) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771) ... 48 more Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to modelo.EquipGTI at Adc.AdcEquipInstalController.adcEquipi(AdcEquipInstalController.java:183) ... 58 more
A linha Adc.AdcEquipInstalController.adcEquipi(AdcEquipInstalController.java:183) é exatamente esta:
EquipInstal equipi = new EquipInstal(
cboxEGTIEquipi.getSelectionModel().getSelectedItem());
Acredito que tenha que mudar o jeito de pegar o item do ComboBox que foi convertido, mas não sei como fazer.
Alguém poderia me ajudar nisto?
Obrigado desde já!