我正在使用組合框從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);
}
}
請幫助謝謝
這些字符串看起來正是從數據庫返回的內容。我希望你不要問我們如何刪除一個字符串的括號......順便說一句:你不應該連接到應用程序線程上的數據庫。 – fabian