私はまだJavaとJavaFXの初心者です。ボタンをクリックしたときに起こっているイベントを停止/中断する方法について、現時点では苦労しています。 したがって、Products(クラス)をTableViewに追加するプログラムを構築しています。これまでのところ、すべてがうまくいき、商品が正常に追加され、ユーザーが名前/価格/金額フィールドにそれぞれ文字列/二重/整数値を入力したかどうかに関わらず、パラメータが自分のカスタム関数でチェックされます。Javafxブレイクまたはストップボタンイベントハンドラ
私のチェック機能では、入力した値が間違っているとエラーが表示されますが、間違った値を入力すると3つのフィールドに入力されたすべてのデータが無効/消去されるようにしたい。そのため、「追加」ボタンのイベントハンドラは何とか停止/中断する必要があります。コンソールにエラーが表示されても、名前/価格/金額のいずれかが誤って入力されても、正しく入力された他の値は引き続き表に印刷されるため、これを実行したいと思います。例えば、 のようなデータが得られます。名前:魚 価格: 金額:3 ^価格の値が正しい倍数ではなく、文字列などの場合。私は、テーブルから任意のデータを削除することができますので、間違って入力された削除ボタンがありますが、私はそれを "時間を節約"したいだけ "賢い"プログラムを作る。 また、私のcheckString/Double/Int関数はtry/catchブロックを使用するので、エラーがあったときにメッセージが出力され、いくつかの "デフォルト"値が返されるようにしました。関数が戻り値を必要とするため)。しかし、私はちょうどあなたのコードを表示してみましょう:
Productクラス:
public class Product{
private SimpleStringProperty name;
private SimpleDoubleProperty price;
private SimpleIntegerProperty amount;
//private SimpleIntegerProperty ID=0;
public Product(){
this.name =new SimpleStringProperty("");
this.price = new SimpleDoubleProperty(0);
this.amount = new SimpleIntegerProperty(0);
//this.ID=0;
}
public Product(String name, double price, int amount /* int id */){
this.name =new SimpleStringProperty(name);
this.price = new SimpleDoubleProperty(price);
this.amount = new SimpleIntegerProperty(amount);
//this.ID=id;
}
public SimpleStringProperty nameProperty() {
return name;
}
public void setName(SimpleStringProperty name) {
this.name = name;
}
public SimpleDoubleProperty priceProperty() {
return price;
}
public void setPrice(SimpleDoubleProperty price) {
this.price = price;
}
public SimpleIntegerProperty amountProperty() {
return amount;
}
public void setAmount(SimpleIntegerProperty amount) {
this.amount = amount;
}
}
今すぐ "追加" ボタン:
//Add Button function
addButton.setOnAction(e->{
Product product = new Product();
if(checkString(nameInput.getText()).equals(""))
{
//break the whole process
}
else
{
//value is correct, so it will be saved
product.setName(checkString(nameInput.getText()));
}
//product.setName(checkString(nameInput.getText()));
product.setPrice(checkDouble(priceInput.getText()));
product.setAmount(checkInt(amountInput.getText()));
table.getItems().add(product);
nameInput.clear();
priceInput.clear();
amountInput.clear();
});
あなたは、私が2例 "のsetName" を持っていることがわかります。最初のものは "if"ステートメントとどのようにしたいのですか、またはプロセスがいつ停止する必要があるかを検出するためにそれを行う方法を考えました。 2番目の方法は、以前と同じように使用され、価格と金額の方法が同じであることがわかります。 Nameプロパティでそれを行う方法を理解すると、PriceプロパティとAmountプロパティで同じことを行います。ここ は、私が書いた3チェック機能です:
public SimpleStringProperty checkString(String messageInput){
SimpleStringProperty stringProperty = new SimpleStringProperty();
try{
Double.parseDouble(messageInput);
System.err.println("You must input a proper name");
stringProperty.setValue("");
}
catch(NumberFormatException e){
stringProperty.setValue(messageInput);
}
return stringProperty;
}
public SimpleIntegerProperty checkInt(String messageInput){
SimpleIntegerProperty integerProperty = new SimpleIntegerProperty();
try{
integerProperty.setValue(Integer.parseInt(messageInput));
}
catch(NumberFormatException e){
System.err.println("You must input a valid amount value");
integerProperty.set(0);
}
return integerProperty;
}
public SimpleDoubleProperty checkDouble(String messageInput){
SimpleDoubleProperty doubleProperty = new SimpleDoubleProperty();
try{
doubleProperty.setValue(Double.parseDouble(messageInput));
}
catch(NumberFormatException e){
System.err.println("You must input a valid price value");
doubleProperty.set(0);
}
return doubleProperty;
}
を最初に、私は1「ユニバーサル」チェック機能を作りたかったが、それは、テンプレートの種類となどで行うことは不可能であるように思わそれはまた別の話です。
メインコードの他の部分が必要な場合は誰でも私を助けることができると私は本当にうれしいですが、それらは関連する部分だと思います。
ありがとうございます。明白なことは明らかですが、それでもかなりうまく動作しません。まず、論理演算子を&&から||に変更しました。そのうちの1つが間違っていても、AND演算子を使用している間はプロセス全体を停止しなければならず、すべて3が偽でなければならないため、プロセスは停止します。それでも、アイデアをありがとう。問題は、何らかの理由で、 "if"ステートメントが機能せず、 "else"ステートメントに移動することになります。これは、 "equals()"関数の戻り値を持つものでなければなりません。 – DracoGrim