gpt4 book ai didi

mysql - 在JavaFX中如何在 TableView 中添加带有数据的组合框

转载 作者:行者123 更新时间:2023-11-29 21:57:51 24 4
gpt4 key购买 nike

我已经尝试了很多,但无法将数据库中的所有值填充到我的组合框表格单元格中。

Controller.java

public class controller {
GetConnection gc = new GetConnection();
PreparedStatement pst;
ResultSet rs;
Statement st;
private ObservableList<Users> datas = FXCollections.observableArrayList();

public controller() {
}

@FXML
private TableView<Users> table;
@FXML
private TableColumn<Users, String> c1;

@FXML
private void editable() {
List<String> names = new ArrayList<String>();
try {
ObservableList<Users> datas = FXCollections.observableArrayList();
String sql = "select * from itemsadd";
pst = gc.getConnection().prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
String name = rs.getString("itemcode");
names.add(name);
System.out.println("probs" + names);
}
ResultSet rs2 = gc.getConnection().createStatement()
.executeQuery("SELECT * FROM itemsadd WHERE itemcode=1001");

while (rs2.next()) {
datas.add(new Users(rs2.getString("itemcode")));
}
for (String name : names) {
c1.setCellValueFactory(new PropertyValueFactory("Itemc"));
c1.setCellFactory(ComboBoxTableCell.forTableColumn(name));
//not getting full items here
System.out.println("hell3" + name);// am getting full items here
}
table.setItems(null);
table.setItems(datas);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}
}

public static class Users {
private StringProperty Itemc;

private Users(String Itemc) {
this.Itemc = new SimpleStringProperty(Itemc);
}

public String getItemc() {
return Itemc.get();
}

public void setItemc(String Itemc) {
this.Itemc.set(Itemc);
}

public StringProperty ItemcProperty() {
return Itemc;
}
}
}

表.fxml

        <?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication47.controller">
<children>
<TableView fx:id="table" editable="true" layoutX="136.0" layoutY="58.0" onKeyPressed="#editable" prefHeight="200.0" prefWidth="335.0">
<columns>
<TableColumn fx:id="c1" prefWidth="333.0" text="Name" />
</columns>
</TableView>
</children>
</AnchorPane>

Main.java加载器

public class Tableveiw extends Application {
private Stage primaryStage;
private AnchorPane pane;

@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");

showPerson();
}

public void showPerson() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Tableveiw.class.getResource("table.fxml"));
pane = (AnchorPane) loader.load();

// Show the scene containing the root layout.
Scene scene = new Scene(pane);
primaryStage.setScene(scene);

primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}

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

数据库连接 公共(public)类 GetConnection {

public Connection getConnection() throws Exception {

Connection con = null;
try {

System.out.println("MySQL Connect Example.");

String url = "jdbc:mysql://localhost:3306/";
String dbName = "login";
String driver = "com.mysql.jdbc.Driver";
// Accessing driver from the jar file
Class.forName(driver).newInstance();
// Creating a veriable for Connection Called conn
con = DriverManager.getConnection(url + dbName, "root", "");

} catch (Exception e) {
e.printStackTrace();
}
return con;

}

public static void main(String arg[]) {
GetConnection con = new GetConnection();
System.out.println("Connection" + con);

}
}

我的代码问题是,我没有将数据库记录中的完整项目获取到我的 c1 组合框表格单元格中。我只获取了我的组合框表格单元格中数据库记录的最后一项。我已经创建了数组,但仍然没有帮助。为什么无法将数据库中的整个项目填充到我的组合框表格单元中。请帮助我对代码进行必要的更改。

最佳答案

这只是基本功能。当您双击单元格组合框时,您可以选择值。要查看直接组合框,您必须编写自己的 TableCell 类 see this you ll understand 。我希望这能帮到您。任何人发表评论

private void editable() {
try {
ObservableList<String> names = FXCollections.observableArrayList();
ObservableList<Users> datas = FXCollections.observableArrayList();
String sql = "select * from itemsadd";
pst = gc.getConnection().prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
String name = rs.getString("itemcode");
names.add(name);
System.out.println("probs" + names);
}
ResultSet rs2 = gc.getConnection().createStatement()
.executeQuery("SELECT * FROM itemsadd WHERE itemcode=1001");

while (rs2.next()) {
datas.add(new Users(rs2.getString("itemcode")));
}
c1.setCellValueFactory(new PropertyValueFactory("Itemc"));
c1.setCellFactory(ComboBoxTableCell.forTableColumn(name));
table.setEditable(true);
table.getItems().clear();
table.setItems(datas);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}

关于mysql - 在JavaFX中如何在 TableView 中添加带有数据的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32962914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com