当我打开轻量级页面时,所有它都可以,但是当我打开一些更大的GUI块并且在网站加载之前没有响应时。我试着把它交给后台线程,但它没有用。
MainWindowController:
WebControl webcontroller = new WebControl();
DatabaseControl datacontroller;
@FXML
private ToggleButton PowypadkoweBt;
@FXML
private ToggleButton UszkodzoneBt;
@FXML
private ToggleButton MailBt;
@FXML
private ToggleButton SMSBt;
@FXML
private Button SendBt;
@FXML
private ScrollPane WynikiScroll;
@FXML
private TableView DoneTable;
@FXML
private TableColumn CheckCol, OpisCol, LoginCol;
@FXML
private TextArea MessageArea;
@FXML
private WebView WebControl;
@FXML
private Button NEXT;
@FXML
private Button PREVIOUS;
@FXML
private Label PAGE;
@Override
public void initialize(URL url, ResourceBundle rb) {
this.CheckCol.setCellValueFactory(
new Callback<CellDataFeatures<UsedTableRow, Boolean>, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(CellDataFeatures<UsedTableRow, Boolean> param) {
return param.getValue().getCheckedProperty();
}
});
this.CheckCol.setCellFactory(CheckBoxTableCell.forTableColumn(this.CheckCol));
this.CheckCol.setEditable(false);
this.CheckCol.setMinWidth(50);
this.CheckCol.setMaxWidth(50);
this.OpisCol.setCellValueFactory(new PropertyValueFactory<UsedTableRow, String>("Description"));
this.LoginCol.setCellValueFactory(new PropertyValueFactory<UsedTableRow, String>("Login"));
ObservableList<UsedTableRow> data = FXCollections.observableArrayList();
try {
datacontroller = new DatabaseControl();
data.addAll(datacontroller.getRowsFromEntity());
} catch (ClassNotFoundException ex) {
Alert alert = new Alert(AlertType.WARNING);
alert.setContentText("Nie udało się nawiązać połączenia z bazą danych. Błąd klasy.");
alert.setHeaderText("Błąd klasy.");
alert.setTitle("Błąd");
alert.show();
} catch (SQLException ex) {
Alert alert = new Alert(AlertType.WARNING);
alert.setHeaderText("Błąd SQL.");
alert.setContentText("Nie udało się nawiązać połączenia z bazą danych. Błąd SQL.");
alert.setTitle("Błąd");
alert.show();
}
this.DoneTable.setItems(data);
this.WebControl.getEngine().load("http://otomoto.pl");
this.PAGE.setText((this.webcontroller.getIteratorAuctions() + this.webcontroller.getIteratorItems() - 10) + " z " + this.webcontroller.getItemsIds().size());
// loadNewSite();
this.WebControl.autosize();
this.WebControl.widthProperty().addListener(new ChangeListener<Object>() {
public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
Double width = (Double) newValue;
WebControl.setPrefWidth(width);
WebControl.autosize();
}
});
this.WebControl.requestLayout();
this.WebControl.setContextMenuEnabled(false);
this.WebControl.getEngine().setJavaScriptEnabled(true);
this.WebControl.getEngine().setUserAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36");
this.PowypadkoweBt.setSelected(true);
this.UszkodzoneBt.setSelected(true);
}
@FXML
private void PowypadkoweAction(ActionEvent event) {
if (this.PowypadkoweBt.isSelected()) {
this.webcontroller.refresh(true);
} else {
this.webcontroller.refresh(false);
}
loadNewSite();
}
@FXML
private void UszkodzoneAction(ActionEvent event) {
}
@FXML
private void SMSAction(ActionEvent event) {
}
@FXML
private void MailAction(ActionEvent event) {
}
@FXML
private void SendAction(ActionEvent event) {
}
@FXML
private void PREVIOUSAction(ActionEvent event) {
webcontroller.previousAuction();
loadNewSite();
}
@FXML
private void NEXTAction(ActionEvent event) {
webcontroller.nextAuction();
loadNewSite();
this.WebControl.autosize();
}
private void updateDoneTable() {
ObservableList<UsedTableRow> data = FXCollections.observableArrayList();
data.addAll(datacontroller.getRowsFromEntity());
this.DoneTable.getItems().clear();
this.DoneTable.setItems(data);
}
private void loadNewSite() {
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
//Background work
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
WebControl.getEngine().load(webcontroller.getSelectedURL());
PAGE.setText((webcontroller.getIteratorAuctions() + webcontroller.getIteratorItems() - 10) + " z " + webcontroller.getItemsIds().size());
} finally {
latch.countDown();
}
}
});
latch.await();
//Keep with the background work
return null;
}
};
}
};
service.start();
/* Task task = new Task(new Runnable() {
@Override
public void run() {
WebControl.getEngine().load(webcontroler.getSelectedURL());
}
});
tr.run();
this.PAGE.setText((this.webcontroler.getIteratorAuctions() + this.webcontroler.getIteratorItems() - 10) + " z " + this.webcontroler.getItemsIds().size());
*/
}}
保险申请类:
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/cinsurance/MainWindow.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("/cinsurance/mainwindow.css").toExternalForm());
primaryStage.setTitle("Aplikacja Ubezpieczeniowa CInsurance");
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException ex) {
Logger.getLogger(CInsurance.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}}
感谢您的帮助。
答案 0 :(得分:0)
我失去了很多时间思考为什么,我只是继续编写这个应用程序的模块,我在控制器的初始化方法中添加了一些多线程......这很奇怪。一切都工作正常,即使我正在加载沉重的页面。我仍然不知道为什么多线程解锁了封锁...但它也可能是JDK的变化,因为我必须将它改为32位64位。毕竟这很奇怪。