[REVIEW REQUEST] Add text property to ComboBox

Tadashi Ohmura omurata at ga2.so-net.ne.jp
Thu Mar 15 07:32:01 PDT 2012


Hello Jerome
I understand your opinion.
String type editable combobox is so common.
Such combobox has TextField for user key input.

What about a rough workaround code titled as TextField05 ?
we have two textfield in a VBox, and initial values set.
we directly type TAB to go to the next textfield.
Please press down array key on a textfield :-)

//========================================================================
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventTarget;
import javafx.event.EventType;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;

public class TextField05 extends Application {
   ContextMenu popup;
   TextField currentParentField;

   public void start(Stage stage) throws Exception {
     VBox root = new VBox();
     root.setSpacing(10);
     Scene scene = new Scene( root );
     //
     popup = new ContextMenu();
     MenuItem item1 = new MenuItem("1111");
     MenuItem item2 = new MenuItem("2222");
     MenuItem item3 = new MenuItem("3333");
     popup.getItems().addAll( item1, item2, item3 );
     popup.setOnAction( popupAction );
     //
     TextField textField_1 = new TextField();
     TextField textField_2 = new TextField();
     textField_1.addEventHandler( KeyEvent.ANY, keyEventHandler );
     textField_2.addEventHandler( KeyEvent.ANY, keyEventHandler );
     //
     root.getChildren().addAll( textField_1, textField_2 );

     stage.setScene(scene);
     stage.setTitle("TextField05");
     stage.setX(300); stage.setY(200); stage.setWidth(300); 
stage.setHeight(320);
     stage.show();
   }
   //
   EventHandler<KeyEvent> keyEventHandler = new EventHandler<KeyEvent>() {
     public void handle(KeyEvent e){
       EventTarget target = e.getTarget();
       currentParentField = (TextField)target;
       EventType<? extends Event> type = e.getEventType();
       if( type == KeyEvent.KEY_PRESSED ){
         KeyCode code = e.getCode();
         if( code == KeyCode.DOWN ){
           popup.show( currentParentField, Side.TOP, 0, 0 );
         }
         else{
           popup.hide();
         }
       }
     }
   };
   //
   EventHandler<ActionEvent> popupAction = new EventHandler<ActionEvent>() {
     public void handle( ActionEvent e ){
       MenuItem src = (MenuItem)e.getTarget();
       String text = src.getText();
       currentParentField.setText( text );
     }
   };
   //----------------------------------------
   public static void main(String[] args) {
     launch(args);
   }
}
//========================================================================

Best regards
   Tadashi Ohmura

(2012/03/15 22:32), Jerome Cambon wrote:
> Exposing TextInputControl is exactly what we need. At least it is the 
> purpose of the Jira which is the base of this discussion.
> We have concrete requirements in Scene Builder for this.
>
> Our use case is the following :
> - we have a set of editable ComboBox in a VBox, with initial values set
> - for convenience, once modified, we directly type TAB to go to the 
> next ComboBox
> - its value is selected so that we can directly enter the new value 
> without having to remove the old one
>
> This seems to be a very common use case...
>
> Jerome



More information about the openjfx-dev mailing list