के साथ रिक्त पंक्तियों में सामग्री दिखाई देती है यदि नीचे value.setCellFactory(NUMBER_CELL_FACTORY);
पर टिप्पणी की गई है तो नीचे दिया गया प्रोग्राम ठीक काम करता है।कस्टम सेल फैक्ट्री
यदि यह शामिल है, तो तालिका दृश्य अजीब तरीके से व्यवहार करता है * - यदि आप बस कोड कॉपी करते हैं, इसे लॉन्च करते हैं और +
बटन पर कुछ बार क्लिक करते हैं, तो आप तालिका के नीचे खाली पंक्तियों में दिखाई देने वाली सामग्री देखेंगे।
क्या मुझे कुछ याद आ रही है?
* जावाएफएक्स 8 - बी 106 पर परीक्षण किया गया।
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TestFX extends Application {
@Override
public void start(final Stage stage) throws Exception {
MyTable table = new MyTable();
Scene scene = new Scene(table);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(TestFX.class);
}
static class MyTable extends AnchorPane {
private static final Callback NUMBER_CELL_FACTORY = (p) -> new NumberTableCell<>();
private final Button addRow = new Button("+");
private final TableView<Item> table = new TableView<>();
MyTable() {
super();
initTableView();
addRow.setOnMouseClicked((e) -> addItem());
VBox vbox = new VBox();
vbox.getChildren().addAll(addRow, table);
getChildren().add(vbox);
}
public void addItem() {
table.getItems().addAll(new Item());
}
private void initTableView() {
TableColumn<Item, Double> value = new TableColumn<>("Value");
value.setCellValueFactory(new PropertyValueFactory<>("value"));
value.setCellFactory(NUMBER_CELL_FACTORY);
table.getColumns().add(value);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
}
}
public static class Item {
private final DoubleProperty value = new SimpleDoubleProperty();
public DoubleProperty valueProperty() {
return value;
}
}
static class NumberTableCell<T> extends TableCell<T, Double> {
private final Color fill = Color.LIGHTGREY;
@Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
if (empty) return;
setText(String.valueOf(item));
setTextFill(fill);
}
}
}