I'm developing an application to detect phishing websites, in javafx. I have a "predict" button that does all the necessary processing within its EventHandler. I also want to be able to update a ProgressBar with information about how far it's gotten during processing. I'm using a Task for this, calling updateProgress and updateValue with the final result.
However, in the event of an exception, I want to update the UI then immediately terminate execution of the EventHandler, using updateProgress with some error value. However, updateProgress does not update the UI immediately. Is there something that exists that can not only update the UI from inside an EventHandler, like Task, but also give me control over exactly when the UI updates?
For reference, here's my complete event handler code:
predict.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event)
{
Task<Result> task = new Task<Result>()
{
@Override
protected Result call() throws Exception
{
String url = urlText.getText();
ArffData arffData = new ArffData();
try
{
updateProgress(1, 10);
URL uri = new URL(url);
String domain = uri.getHost();
arffData.setUrlSimilarity(DataGatherer.readLevenshtein(domain));
updateProgress(2, 10);
boolean redirection = DataGatherer.getRedirectionStatus(url);
arffData.setRedirection(redirection);
updateProgress(3, 10);
Response response = Jsoup.connect(url).execute();
arffData.setSpellingErrors(DataGatherer.getSpellingErrors(response).size());
}
catch (IOException e1)
{
updateProgress(-1, 10); //Should update UI before terminating
return null;
}
Classifier rf;
Instances instances;
try
{
updateProgress(4, 10);
rf = (Classifier) SerializationHelper.read("RF100.model");
instances = new DataSource("phishingData.arff").getDataSet();
}
catch (Exception e1)
{
updateProgress(-1, 10);
return null;
}
if (instances.classIndex() == -1)
instances.setClassIndex(instances.numAttributes() - 1);
String offers = offerText.getValue();
String lf = lfText.getValue();
updateProgress(5, 10);
Instance inst = InstanceSetup.setUpInstance(arffData, offers, lf, instances);
try
{
updateProgress(6, 10);
double clsLabel = rf.classifyInstance(inst);
instances.add(inst);
rf.buildClassifier(instances);
SerializationHelper.write("RF100.model", rf);
Evaluation eval = new Evaluation(instances);
eval.crossValidateModel(rf, instances, 10, new Random(1));
boolean phishing = clsLabel ==0 ?true: false;
Result result = new Result(phishing, eval.pctCorrect());
updateProgress(10, 10);
if(clsLabel == 0)
{
predictionLabel.setText("the given website IS a phishing website.");
}
else
{
predictionLabel.setText("the given website IS NOT a phishing website.");
}
updateValue(result);
accuracyLabel.setText("PhishGuard is " + String.format("%.4f%%", eval.pctCorrect()) +
" confident in this prediction.");
return result;
}
catch (Exception e)
{
updateProgress(-1, 10);
return null;
}
}
};
task.progressProperty().addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
switch(newValue.intValue())
{
case 1:
{
pBar.setProgress(10);
progressLabel.setText(progressLabels[1]);
}
case 2:
{
pBar.setProgress(30);
progressLabel.setText(progressLabels[2]);
}
case 3:
{
pBar.setProgress(50);
progressLabel.setText(progressLabels[3]);
}
case 4:
{
pBar.setProgress(70);
progressLabel.setText(progressLabels[4]);
}
case 5:
{
pBar.setProgress(80);
progressLabel.setText(progressLabels[5]);
}
case 6:
{
pBar.setProgress(90);
progressLabel.setText(progressLabels[6]);
}
case 10:
{
pBar.setProgress(100);
progressLabel.setText(progressLabels[0]);
}
case -1:
{
predictionLabel.setText("a prediction could not be made.");
accuracyLabel.setText("");
pBar.setProgress(0);
progressLabel.setText(progressLabels[0]);
}
}
}
});
task.valueProperty().addListener(new ChangeListener<Result>(){
@Override
public void changed(ObservableValue<? extends Result> observable, Result oldValue, Result newValue)
{
// TODO Auto-generated method stub
boolean phishing = newValue.isPhishing();
if(phishing)
{
predictionLabel.setText("the given website IS a phishing website.");
}
else
{
predictionLabel.setText("the given website IS NOT a phishing website.");
}
accuracyLabel.setText("PhishGuard is " + String.format("%.4f%%", newValue.getAccuracy()) +
" confident in this prediction.");
}
});
new Thread(task).start();
}});