आपको प्रदर्शित करने के लिए अपने टेबल डेटा ऑब्जेक्ट्स में गुणों का उपयोग करने की आवश्यकता नहीं है, हालांकि कुछ परिस्थितियों में गुणों का उपयोग वांछनीय है।
निम्नलिखित कोड एक व्यक्ति वर्ग के आधार पर लोगों की एक तालिका प्रदर्शित करेगा जिसमें केवल स्ट्रिंग फ़ील्ड हैं।
import javafx.application.Application;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class ReadOnlyTableView extends Application {
private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "[email protected]"),
new Person("Isabella", "Johnson", "[email protected]"),
new Person("Ethan", "Williams", "[email protected]"),
new Person("Emma", "Jones", "[email protected]"),
new Person("Michael", "Brown", "[email protected]")
);
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
stage.setScene(new Scene(new Group(vbox)));
stage.show();
}
public static class Person {
private String firstName;
private String lastName;
private String email;
private Person(String fName, String lName, String email) {
this.firstName = fName;
this.lastName = lName;
this.email = email;
}
public String getFirstName() { return firstName; }
public void setFirstName(String fName) { firstName = fName; }
public String getLastName() { return lastName; }
public void setLastName(String lName) { lastName = lName; }
public String getEmail() { return email; }
public void setEmail(String inMail) { email = inMail; }
}
}
स्पष्टीकरण
गुण और ObservableLists का उपयोग करने का उद्देश्य यह है कि इन listenable तत्व हैं है। जब गुणों का उपयोग किया जाता है, तो डेटामैडेल में किसी प्रॉपर्टी विशेषता का मान बदलता है, तो तालिका दृश्य में आइटम का दृश्य स्वचालित डेटामैडेल मान से मेल खाने के लिए स्वचालित रूप से अपडेट किया जाता है। उदाहरण के लिए, यदि किसी व्यक्ति की ईमेल प्रॉपर्टी का मान किसी नए मान पर सेट किया गया है, तो वह अपडेट तालिका दृश्य में दिखाई देगा क्योंकि यह संपत्ति परिवर्तन के लिए सुनता है। यदि इसके बजाय, ईमेल का प्रतिनिधित्व करने के लिए एक सादा स्ट्रिंग का उपयोग किया गया था, तो तालिका दृश्य रीफ्रेश नहीं होगा क्योंकि यह ईमेल मूल्य परिवर्तन से अनजान होगा।
PropertyValueFactory प्रलेखन विस्तार से इस प्रक्रिया का वर्णन:
कैसे इस वर्ग का उपयोग करने का एक उदाहरण है:
TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
इस उदाहरण में, "firstName" स्ट्रिंग एक संदर्भ के रूप में किया जाता है पर व्यक्ति वर्ग प्रकार (जो तालिका तालिका आइटम सूची का वर्ग प्रकार) में पहली नामप्रॉपर्टी() विधि मानी गई है)। इसके अतिरिक्त, इस विधि को एक संपत्ति उदाहरण वापस करना होगा। यदि इन आवश्यकताओं को पूरा करने की कोई विधि मिलती है, तो TableCell इस ObservableValue के साथ पॉप्युलेट किया गया है। इसके अतिरिक्त, TableView स्वचालित रूप से लौटाए गए मूल्य पर पर्यवेक्षक जोड़ देगा, जैसे कि कोई भी परिवर्तन निकाल दिया गया है, तालिका दृश्य द्वारा देखी गई होगी, जिसके परिणामस्वरूप सेल तुरंत अपडेट हो जाएगा।
कोई इस पैटर्न से मेल खाते विधि मौजूद है, वहाँ गिरावट के माध्यम से ) (isFirstName() ऊपर के उदाहरण में है कि, getFirstName() या) कॉल करने के लिए मिलता है (प्रयास करने के लिए समर्थन या है() है।एक विधि मिलान इस पैटर्न मौजूद है, तो मूल्य इस विधि है एक ReadOnlyObjectWrapper में लिपटे से लौटे और TableCell में लौट आए। हालांकि, इस स्थिति में, इसका मतलब है कि TableCell परिवर्तन के लिए ObservableValue निरीक्षण करने के लिए (जैसा कि ऊपर पहले दृष्टिकोण में मामला है) में सक्षम नहीं होगा।
अद्यतन
यहाँ पहले उदाहरण दर्शाता है जो कि कैसे एक tableview का निरीक्षण करने और स्वचालित रूप से कर सकते हैं ताज़ा परिवर्तन के आधार पर यह एक संपत्ति आधारित आइटम के मूल्य के लिए वस्तुओं और परिवर्तन की ObservableList है करने के लिए भिन्न, एक विपरीत उदाहरण है विशेषता।
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.collections.*;
import javafx.event.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class PropertyBasedTableView extends Application {
private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data = FXCollections.observableArrayList();
private void initData() {
data.setAll(
new Person("Jacob", "Smith", "[email protected]"),
new Person("Isabella", "Johnson", "[email protected]"),
new Person("Ethan", "Williams", "[email protected]"),
new Person("Emma", "Jones", "[email protected]"),
new Person("Michael", "Brown", "[email protected]")
);
}
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
initData();
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
table.setPrefHeight(300);
final Button setEmailButton = new Button("Set first email in table to [email protected]");
setEmailButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
if (data.size() > 0) {
data.get(0).setEmail("[email protected]");
}
}
});
final Button removeRowButton = new Button("Remove first row from the table");
removeRowButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
if (data.size() > 0) {
data.remove(0);
}
}
});
final Button resetButton = new Button("Reset table data");
resetButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
initData();
}
});
final VBox vbox = new VBox(10);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, setEmailButton, removeRowButton, resetButton);
stage.setScene(new Scene(new Group(vbox)));
stage.show();
}
public static class Person {
private final StringProperty firstName;
private final StringProperty lastName;
private final StringProperty email;
private Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}
public String getFirstName() { return firstName.get(); }
public void setFirstName(String fName) { firstName.set(fName); }
public StringProperty firstNameProperty() { return firstName; }
public String getLastName() { return lastName.get(); }
public void setLastName(String lName) { lastName.set(lName); }
public StringProperty lastNameProperty() { return lastName; }
public String getEmail() { return email.get(); }
public void setEmail(String inMail) { email.set(inMail); }
public StringProperty emailProperty() { return email; } // if this method is commented out then the tableview will not refresh when the email is set.
}
}
यह एक बुरी तरह लिखित ट्यूटोरियल है। वे जेनरिक्स के लिए बहुत से महत्वपूर्ण बिंदुओं पर चमकते हैं। सब से –