gpt4 book ai didi

JavaFX:可编辑 TextFieldTableCell 中的异常处理

转载 作者:行者123 更新时间:2023-12-02 05:02:54 29 4
gpt4 key购买 nike

我的 TableView 是可编辑的,因此我可以直接从表格更新价格和数量,而无需按任何按钮。我的价格和数量都是整数,那么当我输入字符串值时如何处理异常?我希望我的程序显示带有消息或类似内容的 DialogPane?

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.converter.IntegerStringConverter;
import model.Produs;
import service.Service;
import validator.ValidationException;

import javax.persistence.criteria.CriteriaBuilder;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class ControllerAdmin implements Initializable {

@FXML
private TableView<Produs> table;

@FXML
private TableColumn<Produs, String> colNume;

@FXML
private TableColumn<Produs, Integer> colPret;

@FXML
private TableColumn<Produs, Integer> colCantitate;

@FXML
private TextField lblNume;

@FXML
private TextField lblCant;

@FXML
private TextField lblPret;

private ObservableList<Produs> observableList;

private Service service;
public ControllerAdmin(){}
public void setService(Service service){
this.service=service;
observableList = FXCollections.observableList(StreamSupport.stream(service.getAllProducts().spliterator(), false).collect(Collectors.toList()));
observableList.remove(new Produs("b",2,2));
loadTables();
}

private void loadTables() {
table.setItems(observableList);
table.getSelectionModel().selectedItemProperty().addListener((observer, oldData, newData) -> showDetails(newData));
table.setEditable(true);
colPret.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
colCantitate.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));


}

private void showDetails(Produs t) {if(t!=null){
lblNume.setText(t.getDenumire());
lblCant.setText(String.valueOf(t.getCantitate()));
lblPret.setText(String.valueOf(t.getPret()));
}

}

public void clickAdd(ActionEvent event) {
try {
service.saveProduct(new Produs(lblNume.getText(), Integer.parseInt(lblPret.getText()), Integer.parseInt(lblCant.getText())));
observableList.add(new Produs(lblNume.getText(), Integer.parseInt(lblPret.getText()), Integer.parseInt(lblCant.getText())));
table.setItems(observableList);
}catch (ValidationException e){
showAllert(e);
}
}
public void clickDelete(ActionEvent event) {
try {
Produs p = new Produs(lblNume.getText(), Integer.parseInt(lblPret.getText()), Integer.parseInt(lblCant.getText()));
service.deleteProduct(p);
observableList.clear();
observableList = FXCollections.observableList(StreamSupport.stream(service.getAllProducts().spliterator(), false).collect(Collectors.toList()));
table.setItems(observableList);
}catch (ValidationException e){
showAllert(e);
}
}

@Override
public void initialize(URL location, ResourceBundle resources) {
colNume.setCellValueFactory(new PropertyValueFactory<model.Produs,String>("denumire"));
colPret.setCellValueFactory(new PropertyValueFactory<model.Produs,Integer>("pret"));
colCantitate.setCellValueFactory(new PropertyValueFactory<model.Produs,Integer>("cantitate"));

}

public void onPretChange(TableColumn.CellEditEvent<Produs, ?> pretIntegerCellEditEvent) {

try{
Produs produs=table.getSelectionModel().getSelectedItem();
produs.setPret((Integer) pretIntegerCellEditEvent.getNewValue());
service.updateProdus(produs);}
catch (NumberFormatException e){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("EroareMare");
alert.setHeaderText(null);
alert.setContentText("Pretul trebuie sa fie un numar natural");
DialogPane dialogPane = alert.getDialogPane();
dialogPane.getStylesheets().add(getClass().getResource("/cssDesign/myDialogs.css").toExternalForm());
dialogPane.getStyleClass().add("myDialog");
dialogPane.setHeader(null);
alert.show();
}
}

public void onCantitateChange(TableColumn.CellEditEvent<Produs, Integer> pretIntegerCellEditEvent) {

try{
Produs produs=table.getSelectionModel().getSelectedItem();
produs.setCantitate(pretIntegerCellEditEvent.getNewValue());
service.updateProdus(produs);}
catch (NumberFormatException e){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("EroareMare");
alert.setHeaderText(null);
alert.setContentText("Cantitatea trebuie sa fie un numar natural");
DialogPane dialogPane = alert.getDialogPane();
dialogPane.getStylesheets().add(getClass().getResource("/cssDesign/myDialogs.css").toExternalForm());
dialogPane.getStyleClass().add("myDialog");
dialogPane.setHeader(null);
alert.show();
}
}


private void showAllert(ValidationException e){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Eroare");
alert.setHeaderText(null);
alert.setContentText(e.getMessage());
DialogPane dialogPane = alert.getDialogPane();
dialogPane.getStylesheets().add(getClass().getResource("/cssDesign/myDialogs.css").toExternalForm());
dialogPane.getStyleClass().add("myDialog");
dialogPane.setHeader(null);
alert.show();
}
}

异常(exception):

Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException: For input string: "a"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.valueOf(Integer.java:983)
at javafx.base/javafx.util.converter.IntegerStringConverter.fromString(IntegerStringConverter.java:49)
at javafx.base/javafx.util.converter.IntegerStringConverter.fromString(IntegerStringConverter.java:35)
at javafx.controls/javafx.scene.control.cell.CellUtils.lambda$createTextField$1(CellUtils.java:255)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8865)
at javafx.controls/com.sun.javafx.scene.control.behavior.TextFieldBehavior.fire(TextFieldBehavior.java:184)
at javafx.controls/com.sun.javafx.scene.control.behavior.TextInputControlBehavior.lambda$keyMapping$62(TextInputControlBehavior.java:330)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$KeyHandler.process(Scene.java:4083)
at javafx.graphics/javafx.scene.Scene$KeyHandler.access$1600(Scene.java:4029)
at javafx.graphics/javafx.scene.Scene.processKeyEvent(Scene.java:2146)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2620)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$1(GlassViewEventHandler.java:248)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
at javafx.graphics/com.sun.glass.ui.View.handleKeyEvent(View.java:547)
at javafx.graphics/com.sun.glass.ui.View.notifyKey(View.java:971)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)

最佳答案

您可以在 ControllerAdmin 类中编写自定义 IntegerStringConverter:

public static class CustomIntegerStringConverter extends IntegerStringConverter {
private final IntegerStringConverter converter = new IntegerStringConverter();

@Override
public String toString(Integer object) {
try {
return converter.toString(object);
} catch (NumberFormatException e) {
showAlert(e);
}
return null;
}

@Override
public Integer fromString(String string) {
try {
return converter.fromString(string);
} catch (NumberFormatException e) {
showAlert(e);
}
return -1;
}
}

然后您可以像IntegerStringConverter一样使用它:

colPret.setCellFactory(TextFieldTableCell.forTableColumn(new CustomIntegerStringConverter()));
colCantitate.setCellFactory(TextFieldTableCell.forTableColumn(new CustomIntegerStringConverter()));

并让 showAlert()Exception 作为参数而不是 ValidationException:

private void showAlert(Exception e){
/* show an alret */
}

关于JavaFX:可编辑 TextFieldTableCell 中的异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56376182/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com