我的程序正在尝试模拟存储产品信息的数据库。
我正在努力学习如何使用TableView他们仍然是我不理解的东西。在我的表中,我希望能够选择多个项目并对这些项目执行某些操作。我已经做过了。
但如果选择第1行和第1行,则会出现一个问题。 2,右键单击,单击“ProductName”。然后选择第3行并右键单击,单击ProductName。它给了我一个JavaFX应用程序线程"显示java.lang.NullPointerException。
我不明白为什么要这样做。如果你能帮忙请做。谢谢。 抱歉我的英文。
package TestTableView;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import static javafx.application.Application.launch;
public class TestTableView extends Application {
private Stage Listwindow;
private TableView<Product> table;
private String name;
private ObservableList<Product> itemSelected;
private WebView browser = new WebView();
private WebEngine webEngine = browser.getEngine();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage){
//Load UnitTable
Listwindow = primaryStage;
Listwindow.setTitle("Product Table");
createTable();
createContextMenu();
VBox vBox = new VBox();
vBox.getChildren().addAll(table);
Scene scene = new Scene(vBox);
Listwindow.setScene(scene);
Listwindow.show();
}
public void createContextMenu(){
//Multiple selection
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
itemSelected = table.getSelectionModel().getSelectedItems();
//ContextMenu
ContextMenu contextMenu = new ContextMenu();
//menu items
MenuItem item1 = new MenuItem("ProductName");
//Add item to context menu
contextMenu.getItems().addAll(item1);
//set context menu to table
table.setContextMenu(contextMenu);
item1.setOnAction(e -> getProductName());
}
public void getProductName(){
for(int x = 0; x < itemSelected.size(); x++){
name = itemSelected.get(x).getName();
//do Something -> Name
}
table.getSelectionModel().clearSelection();
}
public void createTable(){
TableColumn<Product, String> nameCol = new TableColumn<>("Name");
nameCol.setMinWidth(200);
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Product, Double> priceCol = new TableColumn<>("Price");
priceCol.setMinWidth(200);
priceCol.setCellValueFactory(new PropertyValueFactory<>("price"));
TableColumn<Product, Integer> quantityCol = new TableColumn<>("Quantity");
quantityCol.setMinWidth(200);
quantityCol.setCellValueFactory(new PropertyValueFactory<>("quantity"));
table = new TableView<>();
table.setItems(getUnits());
table.getColumns().addAll(nameCol,priceCol,quantityCol);
}
public ObservableList<Product> getUnits(){
ObservableList<Product> unit = FXCollections.observableArrayList();
unit.add(new Product("Toilet",90.00,100));
unit.add(new Product("Chair",200.00,200));
unit.add(new Product("Avengers DVD",15.00,50));
return unit;
}
}
产品类
package TestTableView;
public class Product {
private String name;
private int quantity;
private double price;
Product(){
this.name = "";
this.price = 0;
this.quantity = 0;
}
Product(String Name, double price, int quantity) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
//
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at TestTableView.TestTableView.getProductName(TestTableView.java:73)
at TestTableView.TestTableView.lambda$createContextMenu$0(TestTableView.java:68)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:2)
由于BadVegan建议在循环之前添加itemSelected.sorted()来修复问题
public void getProductName(){
itemSelected.sorted();
for(int x = 0; x < itemSelected.size(); x++){
name = itemSelected.get(x).getName();
//do Something -> Name
}
table.getSelectionModel().clearSelection();
}
答案 1 :(得分:0)
这必须是
Product
class.Don的正确实现 当你更改表格没有得到的值时,使用getter会导致 刷新:
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Product {
private StringProperty name;
private DoubleProperty price;
private IntegerProperty quantity;
/**
* Constructor
*/
protected Product() {
name = new SimpleStringProperty("");
price = new SimpleDoubleProperty(0.0);
quantity = new SimpleIntegerProperty(0);
}
/**
* Constructor
*
* @param name
* @param price
* @param quantity
*/
protected Product(String name, double price, int quantity) {
this.name = new SimpleStringProperty(name);
this.price = new SimpleDoubleProperty(price);
this.quantity = new SimpleIntegerProperty(quantity);
}
public StringProperty nameProperty() {
return name;
}
public DoubleProperty priceProperty() {
return price;
}
public IntegerProperty quantityProperty() {
return quantity;
}
public void setName(String name) {
this.name.set(name);
}
public void setPrice(double price) {
this.price.set(price);
}
public void setQuantity(int quantity) {
this.quantity.set(quantity);
}
}