2016-04-05 131 views
0

我從來沒有嘗試過創建一個GUI,而且我卡在某些可能不是我應該卡住的東西上。如何讓文本字段在沒有操作的情況下設置文本?我試圖使用setText ...我知道我可以設置它,當我點擊搜索按鈕,但我不認爲它允許我這樣做,並將其追加到查詢字符串。任何幫助或方向,非常感激。JavaFx文本字段設置文本

import javafx.application.Application; 
    import javafx.geometry.Insets; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.control.ComboBox; 
    import javafx.scene.control.Label; 
    import javafx.scene.control.TextField; 
    import javafx.scene.layout.GridPane; 
    import javafx.stage.Stage; 

    public class gui extends Application { 

     Stage window; 
     ComboBox<String> dropDown; 
     Button button; 
     TextField searchString; 
     String dropField; 
     String inputText; 
     String queryText; 


     public static void main(String[] args) { 

      Application.launch(args); 
     } 

     @Override 
     public void start(Stage primaryStage) throws Exception { 
      window = primaryStage; 
      window.setTitle("Deep Search"); 

      GridPane layout = new GridPane(); 
      layout.setPadding(new Insets(10, 10, 10, 10)); 
      layout.setVgap(8); 
      layout.setHgap(10); 

      //ComboBox Label 
      Label comboLabel = new Label("Field:"); 
      GridPane.setConstraints(comboLabel, 0,0); 

      //ComboBox 
      dropDown = new ComboBox<>(); 
      dropDown.getItems().addAll(
        "all_fields", 
        "identifier", 
        "component", 
        "de_manager", 
        "de_priority", 
        "engineer", 
        "found", 
        "headline", 
        "integrated_releases_text", 
        "product", 
        "project", 
        "severity", 
        "status", 
        "submitted_on", 
        "submitter_id", 
        "version_text" 
      ); 
      dropDown.setPromptText("Select Search Field"); 
      dropDown.setOnAction(e -> setSearchField()); 
      GridPane.setConstraints(dropDown, 2, 0); 

      //search box 
      searchString = new TextField(); 
      searchString.setOnAction(e -> setSearchText()); 
      searchString.setPromptText("Enter search string..."); 
      GridPane.setConstraints(searchString, 2, 2); 

這是文本字段。這裏,當用戶輸入時設置文本。我想知道如何在沒有任何操作的情況下設置文本,因爲它被添加到查詢中,並且單擊按鈕時在上面的組合框中進行選擇。

  //Search button 
      button = new Button("Search"); 
      button.setOnAction(e -> queryMethod()); 
      GridPane.setConstraints(button, 2, 10); 

      layout.getChildren().addAll(comboLabel, dropDown, searchString, button); 

      Scene scene = new Scene(layout, 300, 200); 
      window.setScene(scene); 
      window.show(); 
     } 

     private void setSearchField(){ 

      dropField = dropDown.getValue(); 
     } 

     private void setSearchText(){ 

      inputText = searchString.getText(); 

     } 

     private void queryMethod(){ 

      queryText = "SELECT identifier, component, de_manager, de_priority, engineer, found, headline, " + 
        "integrated_releases_text, product, project, severity, status, submitted_on, submitter_id, " + 
        "version_text FROM BUG WHERE " + dropField + ":" + inputText; 
     } 

    } 

回答

0

這不是很清楚你問什麼,但我覺得你只是想從文本字段中queryMethod()

private void queryMethod(){ 

     queryText = "SELECT identifier, component, de_manager, de_priority, engineer, found, headline, " + 
       "integrated_releases_text, product, project, severity, status, submitted_on, submitter_id, " + 
       "version_text FROM CDETS_ETL_BUG WHERE " + dropField + ":" + searchString.getText(); 
    } 
+0

是啊,這正是我想要的。非常感謝! – anshanno