Browse Source

IterationsChange when no more Input

Tom Troppmann 6 years ago
parent
commit
114b91a385
2 changed files with 25 additions and 6 deletions
  1. 10 0
      src/interfaces/FocusListener.java
  2. 15 6
      src/ui/view/TimePanel.java

+ 10 - 0
src/interfaces/FocusListener.java

@@ -0,0 +1,10 @@
+package interfaces;
+
+
+public interface FocusListener extends java.awt.event.FocusListener{
+	@Override
+	public default void focusGained (java.awt.event.FocusEvent evt) {
+    	//Do nothing
+    }
+	//focusLost has to be defined in the lambda expression 
+}

+ 15 - 6
src/ui/view/TimePanel.java

@@ -12,7 +12,11 @@ import javax.swing.plaf.basic.BasicSliderUI;
 import java.awt.*;
 import java.awt.event.*;
 import java.util.Hashtable;
-
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 /**
  * This Class represents a Panel where the User can start and stop the
  * Simulation. He Can also reset the Simulation and click through every
@@ -41,6 +45,7 @@ public class TimePanel extends JPanel implements ActionListener{
 	private final JPanel timeBtnPanel = new JPanel();
 	private final JPanel iterationsPanel=new JPanel();
 	private final JPanel timePanel=new JPanel();
+	private ScheduledFuture<?> futureTask;
 	//private LabelHint iterationsLblHint;
 	JSlider timeSlider = new JSlider() {
 		/**
@@ -290,12 +295,17 @@ public class TimePanel extends JPanel implements ActionListener{
 		iterationsField.setText(""+cont.getModel().getIterations());
 		iterationsField.setToolTipText("0-" + MAX_ITERATIONS  );
 		iterationsField.addActionListener(this);
+		ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(1);
 		iterationsField.addCaretListener((e)->
 			{
 				try{
 					int a=Integer.parseInt(iterationsField.getText());
 					iterationsField.setBackground(Color.WHITE);//red stings
 					//this.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "like Enter")); that is lagging
+					if(futureTask != null){
+						futureTask.cancel(true);
+					}
+					futureTask = s.schedule((Runnable)() -> updateIterationsInput(), 1, TimeUnit.SECONDS);
 					hint.setText(" ");
 				}catch(NumberFormatException n){
 					iterationsField.setBackground(GUI.PALE_RED);//red stings
@@ -303,11 +313,10 @@ public class TimePanel extends JPanel implements ActionListener{
 				}
 			}
 		);
-		iterationsField.addFocusListener(new java.awt.event.FocusAdapter() {
-			    public void focusLost (java.awt.event.FocusEvent evt) {
-			    	updateIterationsInput();
-			    }
-			});
+		iterationsField.addFocusListener((interfaces.FocusListener)(e) ->{
+			//FocusLost is not defined in interface have to be defined here
+			updateIterationsInput();
+		});
 		c.gridy = 1;
 		iterationsPanel.add(iterationsField, c);
 		c.gridy = 2;