0

I'm using a JFXDrawer, SalesDrawer containing a VBox and 2 JFXButtons inside it. The contents of the JFXDrawer have a separate controller, SalesDrawerController.java. The controller file, SalesController.java that contains the JFXDrawer consists of 2 anchor panes that I want to set visible on click of the buttons in the JFXDrawer. Till now I was using a set of static boolean variables and making sure on click of a button in the JFXDrawer one of the variables is set to true. Then in the SalesController.java, I used a TimerTask object to check which of these variables were true and set the needed anchor pane to visible. Is there a better way of doing this?

SalesDrawerController.java

public class SalesDrawerController implements Initializable {
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
    @FXML
    private void button1Hit(MouseEvent event) {
        SalesController.SD[0]=true;  
    }
    @FXML
    private void button2Hit(MouseEvent event) {
        SalesController.SD[1]=true;
    }  
}

SalesController.java

 public class SalesController implements Initializable {

        public static boolean SD[]= {false,false};
        static boolean tock=true;
         @FXML
        private AnchorPane eq_newpane;
        @FXML
        private AnchorPane eq_delpane;
        @FXML
        private JFXHamburger SalesHam;
        @FXML
        private JFXDrawer SalesDraw;
         public void initialize(URL url, ResourceBundle rb) {
            eq_newpane.setVisible(true);
            eq_delpane.setVisible(false); 
            eq_newpane.setDisable(false);
            eq_delpane.setDisable(true); 
            VBox box = FXMLLoader.load(getClass().getResource("/fxml/SalesDrawer.fxml"));

        SalesDraw.setSidePane(box);
           HamburgerBackArrowBasicTransition transition = new HamburgerBackArrowBasicTransition(SalesHam);
           transition.setRate(-1);

            SalesHam.addEventHandler(MouseEvent.MOUSE_PRESSED,(e)->{
            transition.setRate(transition.getRate()*-1);
            transition.play();
            if(SalesDraw.isShown()){
          SalesDraw.close(); 
          SalesDraw.toBack();
            }
            else{
                SalesDraw.toFront();
                SalesDraw.open();
            } 
            });
            threadtock();

    }
    public void threadtock() {
      final java.util.Timer timer = new java.util.Timer();
          final TimerTask delayedThreadStartTask;
            delayedThreadStartTask = new TimerTask() {
                public void run() {
                    try
                    {
                        if(tock){
                            if(SD[0])
                            {
                               eq_newpane.setVisible(true);
                               eq_delpane.setVisible(false); 
                               eq_newpane.setDisable(false);
                               eq_delpane.setDisable(true); 

                            }
                            else if(SD[1]){
                                eq_delpane.setVisible(true);
                                eq_newpane.setVisible(false);
                                eq_newpane.setDisable(true);
                                eq_delpane.setDisable(false); 
                            }

                            if(tock)
                            {
                                threadtock();
                            }

                        }
                    }
                        catch(Exception e)
                                {
                                 e.printStackTrace();
                                }

                    }

                };

                timer.schedule(delayedThreadStartTask, 500);
            };
}
SaberSz
  • 115
  • 1
  • 10

1 Answers1

0

Use synchronization mechanisms so you do not inherit deadlocks in your code from 2 or more threads attempting to operate on the same variable at once.

Figuring out how to properly synchronize your threads can be a pain, and it is very hard to explain, let alone debug. So, my best advice is to use google for "Java Concurrency" and "Java Thread Synchronization".

Otherwise, you should start a thread/threads from your main application to preform your intended operations

Joe
  • 1,316
  • 9
  • 17