在javafx中将组合框[“example”]中的值更改为“example”

时间:2016-03-05 19:13:38

标签: javafx combobox

我正在使用组合框从stage.show()上的数据库中获取数据,到目前为止,我可以检索数据并在组合框上实现我的changelistener。

问题是我从数据库而不是“示例”获得[“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);
    }
}

请帮助谢谢

1 个答案:

答案 0 :(得分: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, ... ]