나는 Tableview를 배우려고했는데 몇 가지 예가있다.javaFX에서 StringProperty 유형의 최종 값을 변경하는 방법은 무엇입니까?
StringProperty의 작동 방식을 알지 못합니다.
Person 클래스의 필드가 최종 instanace 있지만
,setEmailButton 그 값을 변경할 수 있습니다.
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]")
);
}
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 VBox vbox = new VBox(10);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, setEmailButton);
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.
}
}
그러면 결론을 내 렸습니다. "유레카! 최종 StringProperty 유형 값을 변경할 수 있습니다!"
는 그래서 난package zzzzDelete;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.stage.Stage;
class A{
void someTest(){
B insB = new B("why");
System.out.println(insB.getString());
insB.setString("omg");
System.out.println(insB.getString());
}
class B{
private final StringProperty someString;
private B(String someString){
this.someString = new SimpleStringProperty(someString);
}
public String getString(){
return someString.get();
}
public void setString(String newString){
this.someString = new SimpleStringProperty(newString); // error
}
}
}
public class SomeTest {
public static void main(String[] args){
A a = new A();
a.someTest();
}
}
오류 때문에 최종 키워드로 발생되는 테스트했다.
첫 번째 예제와 두 번째 예제는 매우 혼동 스럽습니다.
죄송합니다. 그리고 감사합니다. 그러나 아직도 나는 조금 얻지 않는다. 그래서 나는 "최종 값"을 ".set"으로 propertymethod (setString)을 통해 바꿀 수 있습니까? 최종 키워드는 변경할 수 없으므로 변경해서는 안된다는 것을 알았습니다. 그게 내가 혼란스러워하는 것. –
아니요'someString' 참조를 변경할 수는 없지만 참조하는 객체는 변경할 수 있습니다. – isnot2bad