gpt4 book ai didi

java - 如何正确地将 simpleStringProperty 中的字符串值显示到 listView 上

转载 作者:行者123 更新时间:2023-12-02 05:10:35 30 4
gpt4 key购买 nike

我想在 ListView 中显示 ManagerData 类型的 ObservableList 中的值。

ManagerData类由2个SimpleStringProperty的帐户名和密码组成。

但是当我用数据填充 ListView 时,它在 ListView 中将 SimpleStringProperty 的值显示为“model.ManagerData@1a697fac”、“model.ManagerData@3c42f0f0”和“model.ManagerData@fc87ab8”,而不是SimpleStringProperty 的值。

Controller 类:

package manager;

import com.jfoenix.controls.JFXListView;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import misci.AlertBuilder;
import misci.DB;
import model.ManagerData;

import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class Controller implements Initializable {

@FXML private JFXListView listView;
@FXML private TextField txtAccount, txtPassword;

private final ObservableList listData = FXCollections.observableArrayList();
private String sql;
private ResultSet rs;


@FXML private void loadAction() {
sql = "select * from account";
rs = DB.executeQuery(sql);

if (rs != null) {
try {
while (rs.next()) {
listData.addAll(new ManagerData(rs.getString("name")));
listView.setItems(listData);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}

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

ManagerData 包装类:

package model;

import javafx.beans.property.SimpleStringProperty;

public class ManagerData {

private final SimpleStringProperty accountName, password;

public ManagerData(){
this.accountName = new SimpleStringProperty();
this.password = new SimpleStringProperty();
}

public ManagerData(String name){
this.accountName = new SimpleStringProperty(name);
this.password = new SimpleStringProperty();
}

public ManagerData(String name, String password){
this.accountName = new SimpleStringProperty(name);
this.password = new SimpleStringProperty(password);
}


//setter methods
public void setAccountName(String accountName){
this.accountName.set(accountName);
}

public void setPassword(String password){
this.password.set(password);
}


//getter methods
public String getAccountName() {
return this.accountName.get();
}

public String getPassword() {
return this.password.get();
}
}

图形用户界面

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

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXListView?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="850.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="manager.Controller">
<children>
<StackPane prefHeight="150.0" prefWidth="200.0" style="-fx-background-color: white;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="30.0">
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<JFXListView fx:id="listView" depth="1" editable="true" focusTraversable="false" layoutX="530.0" layoutY="41.0" showTooltip="true" stylesheets="@listViewStyles.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="500.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="15.0" />
</children></AnchorPane>
</children></StackPane>
<AnchorPane layoutX="39.0" layoutY="41.0" prefHeight="45.0" style="-fx-background-color: white;" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<effect>
<DropShadow blurType="GAUSSIAN" color="#616161" height="5.63" offsetY="2.5" radius="0.9075" width="0.0" />
</effect>
<children>
<JFXButton contentDisplay="GRAPHIC_ONLY" focusTraversable="false" layoutX="47.0" layoutY="19.0" prefWidth="45.0" ripplerFill="#8d8d8dd6" style="-fx-background-radius: 0em; -fx-border-radius: 0em;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<graphic>
<MaterialDesignIconView fill="#616161" glyphName="MENU" size="20" />
</graphic>
</JFXButton>
<JFXButton contentDisplay="GRAPHIC_ONLY" focusTraversable="false" layoutX="715.0" onAction="#addAction" prefWidth="45.0" ripplerFill="#8d8d8dd6" style="-fx-background-radius: 0em; -fx-border-radius: 0em;" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="90.0" AnchorPane.topAnchor="0.0">
<graphic>
<MaterialDesignIconView fill="#616161" glyphName="PLUS" size="20" />
</graphic>
</JFXButton>
<JFXButton contentDisplay="GRAPHIC_ONLY" focusTraversable="false" layoutX="760.0" onAction="#loadAction" prefWidth="45.0" ripplerFill="#8d8d8dd6" style="-fx-background-radius: 0em; -fx-border-radius: 0em;" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="45.0" AnchorPane.topAnchor="0.0">
<graphic>
<MaterialDesignIconView fill="#616161" glyphName="DOWNLOAD" size="20" />
</graphic>
</JFXButton>
<JFXButton contentDisplay="GRAPHIC_ONLY" focusTraversable="false" layoutX="661.0" layoutY="38.0" prefWidth="45.0" ripplerFill="#8d8d8dd6" style="-fx-background-radius: 0em; -fx-border-radius: 0em;" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<graphic>
<MaterialDesignIconView fill="#616161" glyphName="DOTS_VERTICAL" size="20" />
</graphic>
</JFXButton>
<HBox alignment="CENTER" layoutX="92.0" layoutY="10.0" spacing="20.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="92.0" AnchorPane.rightAnchor="138.0" AnchorPane.topAnchor="10.0">
<children>
<TextField fx:id="txtAccount" alignment="CENTER" focusTraversable="false" prefWidth="300.0" promptText="Account Name" style="-fx-background-radius: 1em; -fx-border-radius: 1em; -fx-background-insets: 0; -fx-background-color: #F5F5F5; -fx-text-fill: #616161;" />
<TextField fx:id="txtPassword" alignment="CENTER" focusTraversable="false" prefWidth="300.0" promptText="Password" style="-fx-background-radius: 1em; -fx-border-radius: 1em; -fx-background-insets: 0; -fx-background-color: #F5F5F5; -fx-text-fill: #616161;" />
</children>
</HBox>
</children>
</AnchorPane>
</children>
</AnchorPane>

最佳答案

默认情况下, ListView 将显示由 toString() 定义的对象的字符串表示形式,因此您应该重写 ManagerData 中的 toString() 类并使其返回您认为与显示相关的任何信息。除非您定义 CellFactory 来配置单元格中应显示的内容,否则这比为了显示而调整 toString 更合适

listView.setCellFactory(param -> new ListCell< ManagerData >() {
@Override
protected void updateItem(ManagerData item, boolean empty) {
super.updateItem(item, empty);

if (empty || item.getAccountName() == null) {
setText("");
} else {
setText(item.getAccountName());
}
}

});

关于java - 如何正确地将 simpleStringProperty 中的字符串值显示到 listView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56327969/

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