JAVAFX ComboBox无法正确显示值名称

时间:2018-12-08 07:22:29

标签: javafx javafx-8 scenebuilder

您好,我试图将数据库中的数据填充到组合框中,但是当我单击下拉菜单时,它没有正确列出项目名称。我不知道为什么。

这是我单击组合框image时出现的内容 任何帮助表示感谢,谢谢!

编辑:我尝试使用String Converter,但它仍然不显示输出。我是否正确使用了字符串转换器?

OrderController类

package sample;


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
 import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;

import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;

import javafx.event.ActionEvent;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;


public class Ordercontroller implements Initializable {
@FXML ComboBox<Orderitems> itemdropdown;
@FXML ComboBox<String> droppax;
@FXML ComboBox<String> tablenum;
@FXML
ObservableList<Orderitems> choice = FXCollections.observableArrayList();
ObservableList<String> pax = 
FXCollections.observableArrayList("1","2","3","4","5","6","7");
ObservableList<String> tableno = 
FXCollections.observableArrayList("1","2","3","4","5","6","7");

@Override
public void initialize(URL location, ResourceBundle resources) {
    itemdrop();


}









public void itemdrop(){
    itemdropdown.setValue(null);
    itemdropdown.setItems(choice);
    droppax.setItems(pax);
    tablenum.setItems(tableno);




            try {
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        String path = "C:\\Users\\franc\\Desktop\\Database\\CSIA1.accdb";
        String url = "jdbc:ucanaccess://" + path;
        Connection conn = DriverManager.getConnection(url);
        Statement st = conn.createStatement();
        String sql = "Select * from Menu";
        ResultSet rs = st.executeQuery(sql);



        while (rs.next()) {
            choice.add(new Orderitems(rs.getString("Item")));



        }


    } catch (Exception e) {
        System.out.println(e.getMessage());


    }



}

}

Orderitems类

 package sample;
 import java.awt.*;
 import javafx.util.StringConverter;

public class Orderitems extends Ordercontroller{
 String Item;


Ordercontroller order = new Ordercontroller();

public Orderitems(String item) {
    this.Item = item;
}

public String getItem() {
    return Item;
}

public void setItem(String item) {
    Item = item;
}

public void start() throws Exception{

itemdropdown.setConverter(new StringConverter<Orderitems>() {
    @Override
    public String toString(Orderitems object) {
        return null;
    }

    @Override
    public Orderitems fromString(String string) {
        return null;
    }


 });}}

订购FXML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/8.0.172-ea" 
xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Ordercontroller">
   <children>
      <TableView fx:id="Ordertable" layoutX="204.0" layoutY="51.0" 
      prefHeight="200.0" prefWidth="345.0">
        <columns>
          <TableColumn fx:id="Oitem" prefWidth="75.0" text="Item" />
          <TableColumn fx:id="Oprice" prefWidth="75.0" text="Price" />
            <TableColumn fx:id="Ospecial" prefWidth="155.0" text="Special 
    Requests" />
    </columns>
  </TableView>
  <Button layoutX="475.0" layoutY="266.0" mnemonicParsing="false" text="Add 
Item" />
  <Label layoutX="38.0" layoutY="238.0" text="Table Number" />
  <TextArea fx:id="specialreq" layoutX="278.0" layoutY="352.0" 
prefHeight="65.0" prefWidth="200.0" text="Special Requests" />
    <Button layoutX="82.0" layoutY="312.0" mnemonicParsing="false" text="Load" />
  <Label layoutX="68.0" layoutY="68.0" text="No. of Pax" />
  <Button layoutX="496.0" layoutY="369.0" mnemonicParsing="false" text="Add" 
/>
  <Button layoutX="473.0" layoutY="308.0" mnemonicParsing="false" 
text="Delete Item" />
  <ComboBox fx:id="itemdropdown" layoutX="308.0" layoutY="270.0" 
onAction="#itemdrop" prefWidth="150.0" />
  <ComboBox fx:id="tablenum" layoutX="34.0" layoutY="270.0" 
prefWidth="150.0" />
  <ComboBox fx:id="droppax" layoutX="34.0" layoutY="96.0" prefWidth="150.0" 
/>
  <Button layoutX="28.0" layoutY="14.0" mnemonicParsing="false" 
onAction="#returnhome" text="Exit" />
</children>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

尝试在您的toString类上实现OrderItem

@Override
public String toString() {
  return item;
}