Example usage for javax.swing Timer restart

List of usage examples for javax.swing Timer restart

Introduction

In this page you can find the example usage for javax.swing Timer restart.

Prototype

public void restart() 

Source Link

Document

Restarts the Timer, canceling any pending firings and causing it to fire with its initial delay.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JLabel("Auto Hide"));
    frame.pack();/*  w w  w .ja va 2  s .  c  o m*/
    frame.setVisible(true);

    Timer autoHideTimer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });
    autoHideTimer.setRepeats(false);

    frame.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseExited(MouseEvent e) {
            System.out.println("Restart...");
            autoHideTimer.restart();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("Stop");
            autoHideTimer.stop();
        }
    });
}

From source file:com.view.PortfolioManagerWindow.java

/**
 * Creates new form MainJFrame//from  w  w w. j ava  2 s.com
 */
public PortfolioManagerWindow() {
    super();
    //The following line is for the exit confirmation

    addWindowListener(new AreYouSure());
    initComponents();

    //START TIMER TO UPDATE ORDERS
    Timer timer = new Timer(5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ArrayList<SingleOrder> ordersDone = new ArrayList<>();
            try {
                ordersDone = (ArrayList) CPMOrderMANIAC.updateOrders();
                CPMEndOfDay.getEODData();
            } catch (ExecutionException | InterruptedException | IOException | JSONException ex) {
                Logger.getLogger(PortfolioManagerWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
            ArrayList<SingleOrder> ordersPending = new ArrayList<>();
            ArrayList<SingleOrder> ordersExecuted = new ArrayList<>();

            if (null != ordersDone) {
                for (int i = 0; i < ordersDone.size(); i++) {
                    String currStatus = ordersDone.get(i).getStatus();
                    if (currStatus.equals("Pending")) {
                        ordersPending.add(ordersDone.get(i));
                    }
                    if (currStatus.equals("Executed")) {
                        ordersExecuted.add(ordersDone.get(i));
                    }
                }
                CPMOrderMANIAC.setPendings(ordersPending);
                CPMOrderMANIAC.setExecuted(ordersExecuted);
                PMOrderHistoryTable.setModel(CPMOrderMANIAC.getOHTableModel());
                PMPendingOrdersTable.setModel(CPMOrderMANIAC.getPRTableModel());
                //System.out.println("ABOUT TO SET MODEL OF EOD TABLE");
                PMEODBoughtTable.setModel(CPMEndOfDay.getBuyTableModel());
                PMEODSoldTable.setModel(CPMEndOfDay.getSellTableModel());
            } else {
                System.out.println("ERROR UPDATING ORDERS");
            }

        }
    });
    timer.start();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    timer.restart();
}

From source file:com.view.TradeWindow.java

/**
 * Creates new form TradeWindow/*  w w w .  j a v a  2s. c  o m*/
 */
public TradeWindow() {
    super();
    //The following line is for the exit confirmation

    addWindowListener(new AreYouSure());
    initComponents();

    //START TIMER TO UPDATE ORDERS
    Timer timer = new Timer(10000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ArrayList<SingleOrder> ordersDone = new ArrayList<>();
            ArrayList<SingleOrder> pendingOrders = new ArrayList<>();
            ArrayList<Block> blockHistory = new ArrayList<>();

            //MAKE PENDING ORDERS FROM PM UPDATE
            try {
                ordersDone = (ArrayList) CTraderOrderMANIAC.updateOrders();
            } catch (InterruptedException | IOException | JSONException | ExecutionException ex) {
                Logger.getLogger(TradeWindow.class.getName()).log(Level.SEVERE, null, ex);
            }

            //MAKE BLOCK ORDER HISTORY UPDATE
            try {
                blockHistory = (ArrayList) CTraderOrderMANIAC.updateBlockOrderHistory();
            } catch (InterruptedException | ExecutionException | IOException | JSONException ex) {
                Logger.getLogger(TradeWindow.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (null != ordersDone) {
                for (SingleOrder o : ordersDone) {
                    if (o.getStatus().equals("Pending")) {
                        pendingOrders.add(o);
                    }
                }
                int currentlyInPendings = CTraderOrderMANIAC.getPendings().size();
                int updatedPendings = pendingOrders.size();
                System.out.println(
                        "Size of currentPend = " + currentlyInPendings + " and of updated: " + updatedPendings);
                CTraderOrderMANIAC.setPendings(pendingOrders);
                TraderIncomingRequestsTable.setModel(CTraderOrderMANIAC.getPRTableModel());
                if (currentlyInPendings < updatedPendings) {
                    showMessageDialog(null, "You have received new orders.");
                }
            } else {
                System.out.println("ERROR UPDATING ORDERS");
            }

            if (null != blockHistory) {
                CTraderOrderMANIAC.setBlockHistory(blockHistory);

                TraderBlockHistoryTable.setModel(CTraderOrderMANIAC.getBlockHistoryTableModel());
            } else {
                System.out.println("ERROR UPDATING BLOCKS");
            }

        }
    });
    timer.start();
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
    timer.restart();

}