我看不到 TableView 的元素 - JavaFX
I cant see the element of a TableView - JavaFX
我遇到了这个问题,当 运行 我的应用程序无法看到我之前添加到我的 table 的元素时,我创建了一个 Class(角色)并使用属性值工厂。感谢和抱歉语言错误,我说西班牙语。这是代码:
package ejemplo.tableview;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class EjemploTableView extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<Personas> data = FXCollections.observableArrayList(
new Personas("Diego","Maradona"),
new Personas("Lionel","Messi")
);
TableView<Personas> tabla = new TableView();
TableColumn<Personas,String> c1 = new TableColumn("Nombre");
c1.setMinWidth(200d);
c1.setCellValueFactory(new PropertyValueFactory<Personas,String>("nombre"));
TableColumn<Personas,String> c2 = new TableColumn("Apellido");
c2.setMinWidth(200d);
c2.setCellValueFactory(new PropertyValueFactory<>("apellido"));
tabla.getColumns().addAll(c1,c2);
tabla.setItems(data);
StackPane root = new StackPane();
root.getChildren().add(tabla);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
这是 class 角色:
package ejemplo.tableview;
public class Personas {
private String nombre;
private String apellido;
public Personas(String nombre,String apellido){
this.nombre = nombre;
this.apellido = apellido;
}
}
查看 PropertyValueFactory 的文档:
A convenience implementation of the Callback interface, designed
specifically for use within the TableColumn cell value factory. An
example of how to use this class is:
TableColumn firstNameCol = new
TableColumn("First Name");
firstNameCol.setCellValueFactory(new
PropertyValueFactory("firstName"));
In this example, the "firstName" string is used as a reference to an
assumed firstNameProperty() method in the Person class type (which is
the class type of the TableView items list). Additionally, this method
must return a Property instance. If a method meeting these
requirements is found, then the TableCell is populated with this
ObservableValue. In addition, the TableView will automatically add an
observer to the returned value, such that any changes fired will be
observed by the TableView, resulting in the cell immediately updating.
If no method matching this pattern exists, there is fall-through
support for attempting to call get() or is() (that
is, getFirstName() or isFirstName() in the example above). If a method
matching this pattern exists, the value returned from this method is
wrapped in a ReadOnlyObjectWrapper and returned to the TableCell.
However, in this situation, this means that the TableCell will not be
able to observe the ObservableValue for changes (as is the case in the
first approach above).
在此基础上修改Personas
class:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Personas {
private StringProperty nombre = new SimpleStringProperty();
private StringProperty apellido = new SimpleStringProperty();
public StringProperty nombreProperty() {return nombre;};
public StringProperty apellidoProperty() {return apellido;};
public Personas(String nombre, String apellido) {
this.nombre.set(nombre);
this.apellido.set(apellido);
}
public String getNombre() {return nombre.get();}
public String getApellido() {return apellido.get();}
}
我遇到了这个问题,当 运行 我的应用程序无法看到我之前添加到我的 table 的元素时,我创建了一个 Class(角色)并使用属性值工厂。感谢和抱歉语言错误,我说西班牙语。这是代码:
package ejemplo.tableview;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class EjemploTableView extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<Personas> data = FXCollections.observableArrayList(
new Personas("Diego","Maradona"),
new Personas("Lionel","Messi")
);
TableView<Personas> tabla = new TableView();
TableColumn<Personas,String> c1 = new TableColumn("Nombre");
c1.setMinWidth(200d);
c1.setCellValueFactory(new PropertyValueFactory<Personas,String>("nombre"));
TableColumn<Personas,String> c2 = new TableColumn("Apellido");
c2.setMinWidth(200d);
c2.setCellValueFactory(new PropertyValueFactory<>("apellido"));
tabla.getColumns().addAll(c1,c2);
tabla.setItems(data);
StackPane root = new StackPane();
root.getChildren().add(tabla);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
这是 class 角色:
package ejemplo.tableview;
public class Personas {
private String nombre;
private String apellido;
public Personas(String nombre,String apellido){
this.nombre = nombre;
this.apellido = apellido;
}
}
查看 PropertyValueFactory 的文档:
A convenience implementation of the Callback interface, designed specifically for use within the TableColumn cell value factory. An example of how to use this class is:
TableColumn firstNameCol = new TableColumn("First Name"); firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
In this example, the "firstName" string is used as a reference to an assumed firstNameProperty() method in the Person class type (which is the class type of the TableView items list). Additionally, this method must return a Property instance. If a method meeting these requirements is found, then the TableCell is populated with this ObservableValue. In addition, the TableView will automatically add an observer to the returned value, such that any changes fired will be observed by the TableView, resulting in the cell immediately updating.
If no method matching this pattern exists, there is fall-through support for attempting to call get() or is() (that is, getFirstName() or isFirstName() in the example above). If a method matching this pattern exists, the value returned from this method is wrapped in a ReadOnlyObjectWrapper and returned to the TableCell. However, in this situation, this means that the TableCell will not be able to observe the ObservableValue for changes (as is the case in the first approach above).
在此基础上修改Personas
class:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Personas {
private StringProperty nombre = new SimpleStringProperty();
private StringProperty apellido = new SimpleStringProperty();
public StringProperty nombreProperty() {return nombre;};
public StringProperty apellidoProperty() {return apellido;};
public Personas(String nombre, String apellido) {
this.nombre.set(nombre);
this.apellido.set(apellido);
}
public String getNombre() {return nombre.get();}
public String getApellido() {return apellido.get();}
}