What about textfield with popup ?

Tadashi Ohmura omurata at ga2.so-net.ne.jp
Thu Mar 15 08:13:23 PDT 2012


Thank you Jerome, for your quick response.

Whichever we use textField, we use textField itself.

What about a  textfield with popup ?
Please press [down arrow] key on a textfield :-)

I made a mistake.
The first code shows popup is shown in opposite side.
I correct this bug.

//========================================================================
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.BOTTOM, 0, 0 ); //<- this
         }
         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





More information about the openjfx-dev mailing list