How to cange a the cursor type by clicking on a bu

2019-09-10 10:16发布

问题:

Maybe this questionis silly but i didn't get any idea about how to change the Cursor type in JavaFX Application but when i run the application and clicking on the button it does anything it should be set the Cursor_WAIT and then loading a page in the WebViewand then return the Cursor_DEFAULT so this is the code i've traied:

  @FXML
private void tabfirst (ActionEvent ee) throws IOException { // for tha Chooser frame text.

            String hh = text11.getText();
            Socket socket = new Socket();

    try {

        //do work
        URL url = new URL (hh);
        url.getContent();
        WebEngine myWebEngine = web1.getEngine();
        myWebEngine.load(url.toExternalForm());
        //close the window chooser
        Stage stage = new Stage();
          Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
          Scene scene = new Scene(root);
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });
        //close cursor
        ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
    }
   catch (IOException e){
       final  Stage stg = new Stage();           
        stg.initModality(Modality.APPLICATION_MODAL);
        stg.initOwner(stg);
        stg.setTitle("Cannot connect to the internet /n Please Verify your connection internet");
        labelno.setText("Cannot connect to the internet...");
        //close chooser
        Stage stage = new Stage();
         Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });

       //set cursor
         ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
   } finally{
       try{ socket.close(); } catch (Exception e){ }
       }

}

So Please can anybody help me Thanks in advance :)

回答1:

Your code is incomplete. Here's how you could do it:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;


public class WaitCursorDemo extends Application {

    Stage primaryStage;

    @Override
    public void start(Stage primaryStage) {

        this.primaryStage = primaryStage;

        HBox root = new HBox();

        Button button = new Button( "Wait 10 Seconds on Scene");
        button.setOnAction(e -> waitAction());
        root.getChildren().add( button);

        Button button2 = new Button( "Wait 10 Seconds On Button 2");
        button2.setOnAction(e -> waitAction( button2));
        root.getChildren().add( button2);

        Button button3 = new Button( "Wait 10 Seconds On Button 3");
        button3.setOnAction(e -> waitAction( button3));
        root.getChildren().add( button3);

        Scene scene = new Scene( root, 1600, 900);

        primaryStage.setScene( scene);
        primaryStage.show();

    }

    private void waitAction() {

        primaryStage.getScene().setCursor(Cursor.WAIT);

        Thread thread = new Thread( new Runnable() {

            @Override
            public void run() {

                try {

                    sleep(3000);

                } catch( Throwable th) {

                    th.printStackTrace();

                } finally {

                    Platform.runLater( () -> { primaryStage.getScene().setCursor(Cursor.DEFAULT); });

                }



            }
        });
        thread.start();

    }

    private void waitAction( Button button) {

        button.setCursor(Cursor.WAIT);

        Thread thread = new Thread( new Runnable() {

            @Override
            public void run() {

                try {

                    sleep(3000);

                } catch( Throwable th) {

                    th.printStackTrace();

                } finally {

                    // null means that the cursor from the scene will be used, see documentation of setCursor
                    Platform.runLater( () -> { button.setCursor( null); });

                }

            }
        });
        thread.start();

    }

    private void sleep( long ms) throws InterruptedException {

       Thread.sleep( ms);

    }

    public static void main(String[] args) {
        launch(args);
    }

}


标签: javafx cursor