Есть класс и метод:
class Example {
public void metod_1(){
// for(int i; i<10;i++) цикл напрмер;
}
}
В классе Main{}, присходить реализация окна, в котором Button и ProgessBarr, В классе Controller как реализивать ProgressBar, чтобы его зноченя изменилься от 'i'-из metod_1 из класс Example,cпосиба!
В классе javafx.concurrent.Task есть удобный метод updateProgress() для отчета о прогрессе выполнения, поэтому можно сделать ваш класс Example его наследником:
// Example.java:
package progressbar;
import javafx.concurrent.Task;
public class Example extends Task<Void> {
@Override
public Void call() {
metod_1();
return null;
}
final int MAX_WORK = 10;
private void metod_1() {
updateMessage("Идет работа");
for (int i = 0; i < MAX_WORK; i++) {
if (isCancelled()) {
updateMessage("Работа прервана!");
return;
}
System.out.print("Немного поработали. ");
System.out.println(i + 1);
updateProgress(i + 1, MAX_WORK);
try {
Thread.sleep(300);
} catch (InterruptedException interrupted) {
if (isCancelled()) {
updateMessage("Работа прервана!");
return;
}
}
}
updateMessage("Работа завершена");
}
protected void updateMessage(String message) {
System.out.println(message);
super.updateMessage(message);
}
}
// Controller.java:
package progressbar;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
public class Controller {
public ProgressBar progressBar1;
public Button buttonStart;
public Button buttonStop;
public Label labelMessage;
private Example example;
public void initialize() {
}
public void startTask(ActionEvent event) {
if (example != null && example.isRunning()) {
example.cancel();
}
example = new Example();
Thread thread = new Thread(example);
thread.setDaemon(true);
thread.start();
progressBar1.progressProperty().bind(example.progressProperty());
labelMessage.textProperty().bind(example.messageProperty());
buttonStart.disableProperty().bind(example.runningProperty());
buttonStop.disableProperty().bind(example.runningProperty().not());
}
public void cancelTask(ActionEvent event) {
if (example != null) example.cancel();
}
}
// sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<VBox alignment="CENTER" xmlns="http://javafx.com/javafx/8.0.92" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="progressbar.Controller">
<Label fx:id="labelMessage"/>
<ProgressBar fx:id="progressBar1" progress="0.0"/>
<HBox>
<Button fx:id="buttonStart" onAction="#startTask" text="Поехали!"/>
<Button fx:id="buttonStop" disable="true" onAction="#cancelTask" text="Стоп"/>
</HBox>
</VBox>
// Main.java:
package progressbar;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости