จะให้ฉันเพิ่มเป็นผู้ฟังที่ต้องแต่ละ ListCell กสร้างขึ้นในของฉัน ListView ต้อ 1 เลย เอาข้อมูลในมือถือแล้ว 2 ต่อได้. โทรหาวิธีการ?

0

คำถาม

ฉันกำลังสร้างเป็น mockup ของจาวาในแบบของในบ้านหลังฐานข้อมูลสำหรับบริษัทของผม ฉันเรียบร้อยแล้แสดงรายการทั้งหมด(ในกรณีนี้ลูกค้า)ในมุมมองรายการแต่ฉันต้องการที่จะสามารถที่จะโทรหาวิธีเอาข้อมูลจากมือถือ ตัวอย่างเช่นถ้าฉันมีการคลิกเป็นลูกค้ากับลูกค้าหมายเลขของ 498 ฉันอยากจะต้องมีทางที่จะเข้าถึงที่หมายเลข(498)และจากนั้นแสดงข้อมูลเพิ่มเติมสำหรับลูกค้าบัตรประจำตัวครรับ ฉันสามารถจัดการแสดงข้อมูลทุกอย่างที่ตัวเองฉันแค่กำลังพยายามอย่างหนักกับวิธีการเข้าถึงข้อมูลอยู่ในห้องขังของตัวเอง ที่เกี่ยวข้องรหัส:

Controller เรียน Snippet นั่น populates รายการในมุมมอง:

package com.verus.techtracker_2;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;

import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class MainViewController {
ObservableList<ObservableList> data = 
FXCollections.observableArrayList();
@FXML
private Label welcomeText;
@FXML
private ListView<ObservableList> OrgTbl2;
private orgmodel org;
@FXML
private static Connection conn;
private static final String url = "jdbc:sqlserver://10.9.32.46:1433;database=TechTracker;integratedSecurity=true";

public MainViewController() {
}

public static Connection connect() throws SQLException {
    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    }catch(ClassNotFoundException cnfe){
        System.err.println("Error: "+cnfe.getMessage());
    }catch(InstantiationException ie){
        System.err.println("Error: "+ie.getMessage());
    }catch(IllegalAccessException iae){
        System.err.println("Error: "+iae.getMessage());
    }

    conn = DriverManager.getConnection(url);
    return conn;
}

public static Connection getConnection() throws SQLException, ClassNotFoundException{
    if(conn !=null && !conn.isClosed())
        return conn;
    connect();
    return conn;


}

public void initialize (URL url, ResourceBundle resourceBundle) throws SQLException, ClassNotFoundException {
    loadData();
}
ResultSet rs = null;
public void loadData() throws SQLException, ClassNotFoundException {
    getConnection();


    ObservableList<orgmodel> OrganizationObservableList = FXCollections.observableArrayList();

    try{
        PreparedStatement ps = connect().prepareStatement("SELECT CustomerName,CustomerID, Inactive FROM dbo.tbCustomers");
        rs=ps.executeQuery();
        System.out.println(rs);


        /**
         * ******************************
         * Data added to ObservableList *
         *******************************
         */
        while (rs.next()) {
            //Iterate Row
            ObservableList<String> row = FXCollections.observableArrayList();
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                //Iterate Column
                row.add(rs.getString(i));
            }
            System.out.println("Row [1] added " + row);
            data.add(row);
            org = new orgmodel(rs.getString(1), rs.getString(2), rs.getBoolean(3));
            OrganizationObservableList.add(org);


        }

        //FINALLY ADDED TO TableView
        OrgTbl2.setItems(data);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error on Building Data");
    }
}


}

FXML แฟ้มของฉากนี้:

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

<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1000.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.verus.techtracker_2.MainViewController">
   <children>
      <VBox layoutX="130.0" layoutY="58.0" prefHeight="200.0" prefWidth="100.0" />
      <ListView fx:id="OrgTbl2" layoutX="7.0" layoutY="6.0" onMouseClicked="#loadData" prefHeight="987.0" prefWidth="958.0" />
   </children>
</AnchorPane>

java javafx
2021-11-22 19:53:02
1

คำตอบที่ดีที่สุด

2

ฉันคิดว่าตอนที่คุณพูด

ตัวอย่างเช่นถ้าฉันมีการคลิกเป็นลูกค้ากับลูกค้าหมายเลขของ 498,

คุณหมายถึงตอบสนองว่าผู้ใช้การเลือกเป็นรายการใน ListView. ทั้งหมดที่คุณต้องการจะทำก็คือลงทะเบียนเป็นผู้ฟังที่กับการเลือกใช้

orgTbl2.getSelectionModel().selectedItemProperty().addListener(...)

มันจะง่ายมากเลยถ้าคุณทำของคุณ ListView แสดงตัวอย่าโหแทนที่จะใช้ List ต้องเป็นตัวแทนของแต่ละแถวด้วย คุณไปแล้วเหมือนจะเป็นนางแบบชั้นเรียนโทรมา OrgModel ได้ถูกกำหนดไว้

นี่คือเวอร์ชั่นของรหัสของคุณซึ่งทำเรื่องนี้ ฉันต้องถูกเอาออกทั้งหมดไม่จำเป็นต้อรหัส reverted ต้องมาตรฐานตั้งชื่อ conventions และทำบางอย่างอื่น"สะอาดแบบ"ดังนั้นอื่นทะเบียนผู้ใช้สามารถอ่านรหัสอย่างง่าย

package com.verus.techtracker_2;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;

import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class MainViewController {

    @FXML
    private Label welcomeText;
    @FXML
    private ListView<OrgModel> orgTbl2;

    private static Connection conn ;
    private static final String url = "jdbc:sqlserver://10.9.32.46:1433;database=TechTracker;integratedSecurity=true";
    
    public void initialize (URL url, ResourceBundle resourceBundle) throws SQLException, ClassNotFoundException {
        loadData();

        orgTbl2.getSelectionModel()
            .selectedItemProperty()
            .addListener((obs, oldSelection, newSelection) -> {

            if (newSelection != null) {
                // assuming names of property accessor methods:
                String customerName = newSelection.getCustomerName();
                String id = newSelection.getId();
                boolean inactive = newSelection.isInactive();
                // do whatever you need with the data:
                System.out.println("Selected customer id: " + id);
        });

        // display customer name in listview:
        orgTbl2.setCellFactory(lv -> new ListCell<>() {
            @Override
            protected void updateItem(OrgModel item, boolean empty) {
                super.updateItem(item, empty);
                if (empty || item == null) {
                    setText("");
                } else {
                    setText(item.getCustomerName());
                }
            }
        });
    }

    public void loadData() throws SQLException, ClassNotFoundException {
    
        ObservableList<OrgModel> organizationObservableList = FXCollections.observableArrayList();
    
        try{
            PreparedStatement ps = connect().prepareStatement("SELECT CustomerName,CustomerID, Inactive FROM dbo.tbCustomers");
            ResultSet rs=ps.executeQuery();
            System.out.println(rs);

            while (rs.next()) {
                OrgModel org = new OrgModel(rs.getString(1), rs.getString(2), rs.getBoolean(3));
                organizationObservableList.add(organizationObservableList);
            }
            orgTbl2.setItems(data);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error on Building Data");
        }
    }

    private static Connection connect() throws SQLException {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        }catch(ClassNotFoundException cnfe){
            System.err.println("Error: "+cnfe.getMessage());
        }catch(InstantiationException ie){
            System.err.println("Error: "+ie.getMessage());
        }catch(IllegalAccessException iae){
            System.err.println("Error: "+iae.getMessage());
        }
    
        conn = DriverManager.getConnection(url);
        return conn;
    }
    
    public static Connection getConnection() throws SQLException, ClassNotFoundException{
        if(conn !=null && !conn.isClosed())
            return conn;
        conn = connect();
        return conn;
    }    

}

ถ้าคุณต้องการโดยเฉพาะตอบสนองต่อการรักษาการคลิกเมาส์ภายแทนที่จะเป็นส่วนที่เลือกเปลี่ยนแปลงคุณสามารถทำเช่นนั้นในห้องขังนะ ข้อคนี่มันหมายความว่าตัวอย่างเช่นถ้าผู้ใช้เปลี่ยนแปลงการเลือกโดยใช้แป้นพิมพ์หลังจากนั้นจะไม่มีการตอบสนองนี้เปลี่ยนแปลงได้ นี่อาจจะเป็นประโยชน์ถึงแม้ว่าถ้าคุณต้องการเพียงเพื่อตอบสนองต่อสองลิกบนเซลล์เป็นต้น

public void initialize (URL url, ResourceBundle resourceBundle) throws SQLException, ClassNotFoundException {
    loadData();

    // display customer name in listview:
    orgTbl2.setCellFactory(lv -> new ListCell<>() {

        {
            setOnMouseClicked(event -> {
                OrgModel item = getItem();
                if (item != null) {
                    String id = item.getId();
                    String customerName = item.getCustomerName();
                    boolean inactive = item.isInactive();
                    // etc ...
                }
            });
        }

        @Override
        protected void updateItem(OrgModel item, boolean empty) {
            super.updateItem(item, empty);
            if (empty || item == null) {
                setText("");
            } else {
                setText(item.getCustomerName());
            }
        }
    });
}
2021-11-22 20:49:18

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่