2016-09-23 70 views
3

我在做一個連接到文檔數據存儲的JavaFx項目。最近我注意到,在某些特定點上它表現得相當慢。一旦開始運行,它會從文檔中檢索一些數據,這些數據考慮到數據量會相當快,並創建我定義的類的實例,以便將它們加載到表中(至此仍然可以)。我是這樣做的功能:我的JavaFx應用程序執行速度很慢

public void createCheckbox() { 
     List<String> columnnames = Arrays.asList("id", "name", "bg", "country","countryCode", "status", 
       "incorporationDate","aclName","aclDomain"); 
     for (int i = 0; i <= columnnames.size() - 1 ; i++) { 
      ColumnConstraints column1 = new ColumnConstraints(); 
      column1.setPercentWidth(22.0); 

      gridChkBoxContainer.getColumnConstraints().add(column1); 
      CheckBox cbs = new CheckBox(columnnames.get(i)); 
      cbs.selectedProperty().set(true); 
      gridChkBoxContainer.add(cbs, i, 0); 
     } 
    } 


    public void createTable() { 
     int count = 0 ; 
     infoTable.getColumns().clear(); 
     CheckBox cb; 
     ObservableList<Node> childrens = gridChkBoxContainer.getChildren(); 
     for (Node node : childrens) { 
      cb = (CheckBox) node; 
      if (cb.isSelected()) { 
       TableColumn Col = new TableColumn(cb.getText()); 
       Col.prefWidthProperty().bind(infoTable.widthProperty().divide(6)); 
       Col.setCellValueFactory(new PropertyValueFactory<Report, String>(cb.getText())); 
       infoTable.getColumns().add(Col); 
      } 
     } 

     try{ 
      infoTable.getItems().clear(); 
      if (txtKeyword.getText().compareTo("") == 0){ 


      strDQL = "select dm_dbo.cosec_general_view.name , dm_dbo.cosec_general_view.comp_id , dm_dbo.cosec_general_view.bg_name , dm_dbo.cosec_general_view.incorporation_date , dm_dbo.cosec_general_view.status , dm_dbo.cosec_general_view.country_name , cosec_general.acl_domain , cosec_general.acl_name , cosec_general.country_code from dm_dbo.cosec_general_view , cosec_general where dm_dbo.cosec_general_view.comp_id =cosec_general.comp_id"; 
      }else{ 
       strDQL = "select dm_dbo.cosec_general_view.name , dm_dbo.cosec_general_view.comp_id , dm_dbo.cosec_general_view.bg_name , dm_dbo.cosec_general_view.incorporation_date , dm_dbo.cosec_general_view.status , dm_dbo.cosec_general_view.country_name , cosec_general.acl_domain , cosec_general.acl_name , cosec_general.country_code from dm_dbo.cosec_general_view , cosec_general where dm_dbo.cosec_general_view.comp_id =cosec_general.comp_id and (UPPER(dm_dbo.cosec_general_view.comp_id) like UPPER('@aclname') or UPPER(dm_dbo.cosec_general_view.name) like UPPER('@aclname')or UPPER(dm_dbo.cosec_general_view.bg_name) like UPPER('@aclname') or UPPER(dm_dbo.cosec_general_view.incorporation_date) like UPPER('@aclname') or UPPER(dm_dbo.cosec_general_view.country_name) like UPPER('@aclname') or UPPER(dm_dbo.cosec_general_view.status) like UPPER('@aclname') or UPPER(cosec_general.acl_name) like UPPER('@aclname')or UPPER(cosec_general.acl_domain) like UPPER('@aclname')or UPPER(cosec_general.country_code) like UPPER('@aclname'))"; 

       String searchVar = "%"+txtKeyword.getText()+"%"; 
       strDQL = strDQL.replace("@aclname", searchVar); 

      } 
      IDfQuery ACLQuery = new DfQuery(); 
      ACLQuery.setDQL(strDQL); 
      IDfCollection col = ACLQuery.execute(_session, DfQuery.DF_READ_QUERY); 
      while (col.next()) { 
       BGDetails bgInfo = new BGDetails(col.getString("comp_id"), col.getString("name"), col.getString("bg_name"), 
         col.getString("country_name"), col.getString("status"), col.getString("incorporation_date"),col.getString("acl_name"),col.getString("acl_domain"),col.getString("country_code")); 

       infoTable.getItems().add(bgInfo); 
       count++; 

      }}catch(Exception e){ 
       Alert alert = new Alert (Alert.AlertType.ERROR , "An Error was found!"); 
       alert.showAndWait(); 
      } 


    if (count == 0){ 
     Alert alert = new Alert(Alert.AlertType.WARNING , "No search result was found !"); 
     alert.showAndWait(); 

    } 
    } 

再有就是文本字段:頂部是通過數據搜索(FX ID =「txtKeyword」)。值得一提的是,爲了達到這個目的而不是存儲我之前提取的數據,我會創建並定製一個新的DQL,以便再次從數據存儲中重新獲取數據。我注意到,在這一點上,並不是需要很長時間才能運行的數據檢索,而是在搜索字段中輸入單詞的鍵值明顯滯後。或者換句話說,每次我想用鍵盤或鼠標瀏覽應用程序時,它都會開始滯後,其他時間也可以。任何想法如何提高性能的速度? 在此先感謝:)

+0

幽州,當在文本字段中鍵入出現的性能問題 - 這代碼運行當你這樣做?據推測你有一些'ChangeListener'註冊它?或者是每次對文本進行更改時調用的'createTable'方法? – Itai

+0

@sillyfly當我點擊文本字段(txtKeyword)來觸發它鍵入某些文本時,它會滯後(換句話說,文本字段需要一段時間才能理解我點擊了它),而當我插入數據時進入該文本字段使用鍵盤顯示器總是遠遠落後於我(考慮到我的打字速度不是那麼高:D) –

+0

我明白那個部分,我問的是 - 你的代碼的哪個部分被稱爲每個鍵 - 按/ TextField中的每個文本更改?它是'createTable'方法嗎?或者其他一些代碼不包含在問題中? – Itai

回答

2

任何與JavaFX Thread無關的事情都可以在外部Thread上完成,特別是數據庫查詢的東西。 JavaFX的服務的

實施例結構[tutorial]下面是使用SQLITE爲例):

public class DataLoader extends Service<Boolean> { 

    int total; 

    /** 
    * Constructor 
    */ 
    public DataLoader() { 

     // if succeeded 
     setOnSucceeded(s -> { 
      //code if Service succeeds 
     }); 

     // if failed 
     setOnFailed(fail -> { 
      //code it Service fails 
     }); 

     //if canceled 
     setOnCancelled(cancelled->{ 
      //code if Service get's cancelled 
     }); 
    } 

    @Override 
    protected Task<Boolean> createTask() { 
     return new Task<Boolean>() { 
      @Override 
      protected Void call() throws Exception { 

       // -------------------------- Load all the libraries 
       try (ResultSet resultSet = connection1.createStatement().executeQuery("SELECT* FROM LIBRARIES;"); 
         ResultSet dbCounter = connection1.createStatement() 
           .executeQuery("SELECT COUNT(*) FROM LIBRARIES;");) { 

        total = dbCounter.getInt(1); 
        Main.logger.info("Uploading libraries...."); 

        // Refresh the text 
        Platform.runLater(() -> Main.updateScreen.label.setText("Uploading Libraries...")); 
        updateProgress(1, 2); 

        // Load all the libraries 
        while (resultSet.next()) { 

         Library library = new Library(resultSet.getString("NAME"), resultSet.getString("TABLENAME"), 
           resultSet.getDouble("STARS"), resultSet.getString("DATECREATED"), 
           resultSet.getString("TIMECREATED"), resultSet.getString("DESCRIPTION"), 
           resultSet.getInt("SAVEMODE"), resultSet.getInt("POSITION"), 
           resultSet.getString("LIBRARYIMAGE"), resultSet.getBoolean("OPENED")); 

         //Using Platform Later to Modify JavaFX Nodes 
         Platform.runLater(()->{ 
          //code here 
         }); 

         updateProgress(resultSet.getRow(), total); 
        } 

       } catch (Exception ex) { 
        Main.logger.log(Level.SEVERE, "", ex); 
        return false //something bad happened 
       } 

       return true; 
      } 
     }; 
    } 

} 
相關問題