2016-03-05 73 views
-1

我正在使用組合框從stage.show()中的數據庫中獲取數據,到目前爲止我可以檢索數據,並且還實現了我的changelistener組合框。更改combobox [「example」]中的值到javafx中的「example」

問題是我從數據庫中取得「示例」而不是「示例」。這是我第一次使用javafx,不知道輸出應該是什麼樣子,但這對我來說很奇怪。

下面

是它的屏幕截圖,也是我的代碼

[http://i.stack.imgur.com/QWawZ.png]

package libman; 

import java.sql.*; 
import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.*; 
import javafx.scene.Scene; 
import javafx.scene.layout.*; 
import javafx.stage.Stage; 
import javafx.stage.*; 
import javafx.scene.control.*; 
import javafx.scene.text.*; 
import javafx.collections.*; 
/** 
/** 
* 
* @author kels 
*/ 
public class BorrowMenu extends Application { 

    private ObservableList<ObservableList> data; 
    DBOperator login; 
    ResultSet rs; 
    Statement stat; 
    private String getval; 
    @Override 
    @SuppressWarnings({"static-access", "Convert2Lambda"}) 
    public void start(Stage primaryStage){ 

     //define the UI elements 
     Label lblID = new Label("Name:   "); 
     Label lblTitle = new Label("Book Title:  "); 
     Label lblauthor = new Label("Author:   "); 
     Label lblissue = new Label("Issue Date: "); 
     Label lblreturn = new Label("Return Date: "); 

     ComboBox title = new ComboBox(); 

     TextField txtid = new TextField(); 
     TextField txtitle = new TextField(); 
     TextField txtauthor = new TextField(); 
     TextField txtissue = new TextField(); 
     TextField txtreturn = new TextField(); 
     //set prompt text 
     txtid.setPromptText("Enter Borrower's Name"); 
     txtitle.setPromptText("Enter Book Title"); 
     txtauthor.setPromptText("Enter Author's Name"); 
     txtissue.setPromptText("Enter Issue Date"); 
     txtreturn.setPromptText("Enter Return Date"); 
     title.setPromptText("Fills books from database"); 
//ToolTip ttip = new ToolTip("Back Menu"); 
     Button btn = new Button("Borrow Book"); 
     Button btnexit = new Button("Menu>>"); 
     btnexit.setTooltip(new Tooltip("Back to Menu")); 

     //set the gripane to add in components 
     GridPane gridpane = new GridPane(); 
     gridpane.setPadding(new Insets(20)); 
     gridpane.setHgap(5); 
     gridpane.setVgap(5); 


     //set components 
     gridpane.setHalignment(lblID, HPos.RIGHT); 
     gridpane.add(lblID, 0,0); 

     gridpane.setHalignment(txtid, HPos.RIGHT); 
     gridpane.add(txtid,1,0); 

     gridpane.setHalignment(lblTitle, HPos.RIGHT); 
     gridpane.add(lblTitle, 0,1); 

     gridpane.setHalignment(title, HPos.RIGHT); 
     gridpane.add(title, 1,1); 

     gridpane.setHalignment(lblauthor, HPos.RIGHT); 
     gridpane.add(lblauthor, 0,2); 

     gridpane.setHalignment(txtauthor, HPos.RIGHT); 
     gridpane.add(txtauthor, 1,2); 

      gridpane.setHalignment(lblissue, HPos.RIGHT); 
     gridpane.add(lblissue, 0,3); 

     gridpane.setHalignment(txtissue, HPos.RIGHT); 
     gridpane.add(txtissue, 1,3); 

     gridpane.setHalignment(lblreturn, HPos.RIGHT); 
     gridpane.add(lblreturn, 0,4); 

     gridpane.setHalignment(txtreturn, HPos.RIGHT); 
     gridpane.add(txtreturn, 1,4); 

     gridpane.setHalignment(btn, HPos.RIGHT); 
     gridpane.add(btn, 1,5); 

     gridpane.setHalignment(btnexit, HPos.RIGHT); 
     gridpane.add(btnexit, 2,5); 

     //display the values from db to combobox on windows launch 
     data = FXCollections.observableArrayList(); 
     primaryStage.setOnShowing(new EventHandler<WindowEvent>(){ 
     @Override 
     public void handle(WindowEvent event){ 
      try{ 
      login = new DBOperator(); 
      stat = login.getStatement(); 
      rs=stat.executeQuery("SELECT * FROM BOOKDB"); 
      while(rs.next()){ 
       ObservableList<String> row =FXCollections.observableArrayList(); 
       row.add(rs.getString("Title")); 
       // row.add(rs.getString("Author")); 
      data.add(row); 
      } 
      title.setItems(data); 
      rs.close(); 



      } 
      catch(SQLException ex){ 
       System.out.println("Driver Not Found!!!" + ex); 
        System.exit(0); 
      } 

     } 
    }); 
     //add the gridpane to the stackpane 
     StackPane root = new StackPane(); 
     root.getChildren().add(gridpane); 
     //title.setO 

     Scene scene = new Scene(root, 380,220); 
     primaryStage.setTitle("Borrow Menu"); 
     primaryStage.setScene(scene); 
     primaryStage.setResizable(false); 
     primaryStage.show(); 



     //System.out.println(row); 
     //activate the combo listener at selection 
     title.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>(){ 

      @Override 
      public void changed(ObservableValue<? extends Object> observable, Object oldvalue, Object newvalue){ 
       System.out.println(newvalue.toString()); 
       // txtauthor.setText(newvalue.toString()); 
      } 
     }); 



    } 
    public static void main(String [] args){ 
     launch(args); 
    } 
} 

請幫助謝謝

+0

這些字符串看起來正是從數據庫返回的內容。我希望你不要問我們如何刪除一個字符串的括號......順便說一句:你不應該連接到應用程序線程上的數據庫。 – fabian

回答

0

你得到了你實際上是在做什麼正確的結果:ObservableList<ObservableList> data捆綁了集合。

對於comboBox的每個項目,您都會有一個集合,所以這就是爲什麼當它呈現時看到「[]」的原因。

這個最小樣本再現您的情況:

@Override 
public void start(Stage primaryStage) { 
    ComboBox title = new ComboBox(); 

    StackPane root = new StackPane(); 
    root.getChildren().add(title); 

    ObservableList<ObservableList> data = FXCollections.observableArrayList(); 

    List<String> test = Arrays.asList("Test", "Example"); 

    for(String s : test) { 
     ObservableList<String> row =FXCollections.observableArrayList(); 
     row.add(s); 
     data.add(row); 
    } 
    title.setItems(data); 

    Scene scene = new Scene(root, 300, 250); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

與你有相同的結果:

error

您需要簡化您的收藏:ObservableList<String> data

@Override 
public void start(Stage primaryStage) { 
    ComboBox<String> title = new ComboBox(); 

    StackPane root = new StackPane(); 
    root.getChildren().add(title); 

    ObservableList<String> data = FXCollections.observableArrayList(); 

    List<String> test = Arrays.asList("Test", "Example"); 

    for(String s : test) { 
     data.add(s); 
    } 
    title.setItems(data); 

    Scene scene = new Scene(root, 300, 250); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

,你將有預期的結果:

solution

編輯

在你想從數據庫中添加若干項目組合的每個選項的情況下,您只需要提供一種使用ComboBox.setCellFactory()來呈現數據的方法,以便您可以覆蓋生成字符串[item1, item2, ... ]的默認方法。

+0

非常感謝你!我現在看到是什麼原因造成的,現在知道如何以及在哪裏應用這樣的......謝謝 –

+0

我編輯了答案,以防萬一您也想將完整結果添加到組合中的每個項目,但我看到情況並非如此。 –

+0

是的,但我會注意到,以供將來使用.....再次感謝 –