Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Scoping with ActionListeners
    text
    copied!<p>I have an application that needs to scan for bluetooth devices every 10 minutes for a duration set by the user. </p> <p>I have two javax.swing.Timer (not java.util.Timer) - one controls invoking the Scanning method every 10 minutes, the other is stopping the first timer (therfore stopping the scanning) once the duration limit has been hit. </p> <p>The durationTimer is created and started within an actionListener. </p> <p>My problem is that because durationTimer is created within an ActionListener, i cannot stop the timer from another ActionListener, as the program cannot "see" the variable name "durationTimer". </p> <p>The reduced code is shown below....</p> <pre><code>public class mainGui extends JFrame { public mainGui() { final ActionListener timerActionEvent = new ActionListener(){ public void actionPerformed(ActionEvent evt){ //start a task here Timer myTimer2 = (Timer) evt.getSource(); //Invoke BluetoothScan method BluetoothScan(myTimer2); } }; final Timer timerDuration; final Timer myTimer = new Timer(5000, timerActionEvent); final ActionListener timerDurationActionEvent = new ActionListener(){ public void actionPerformed(ActionEvent evt){ //Stops myTimer, so that Bluetooth Stops scanning every 10mins. myTimer.stop(); } }; ActionListener btnScanAction = new ActionListener(){ //Action listener for reading data from db public void actionPerformed(ActionEvent e){ int roomID = 0; int lecturer = 0; int unit; int roomIDIndex; int lectIDIndex; int yearIDIndex; int unitIDIndex; String[] roomArray; String[] lecturerArray; String[] unitArray = null; int durationIndex; String DURATION; int durationInt; //System.out.println(unitArray.length); durationIndex = durCB.getSelectedIndex(); DURATION = itemDuration[durationIndex]; durationInt = Integer.parseInt(DURATION); //User Selected Duration converted to Milliseconds int durationMilliSec = (int)(durationInt*60000); ArrayList&lt;String[]&gt; unitYear = null; //Store the index ID of the JComboBox Selections roomIDIndex = roomCB.getSelectedIndex(); lectIDIndex = lectCB.getSelectedIndex(); unitIDIndex = unitCB.getSelectedIndex(); yearIDIndex = yearCB.getSelectedIndex(); switch(yearIDIndex) { case 1: unitYear = Units1; break; case 2: unitYear = Units2; break; case 3: unitYear = Units3; break; case 4: unitYear = UnitsMasters; break; } //Get the Array contents at index location roomArray = rooms.get(roomIDIndex); lecturerArray = Lecturers.get(lectIDIndex); unitArray = unitYear.get(unitIDIndex); if(unitArray==null){ System.out.println("Please select a unit"); System.exit(0); } roomID = Integer.parseInt(roomArray[0]); lecturer = Integer.parseInt(lecturerArray[0]); unit = Integer.parseInt(unitArray[0]); populateComboBoxes pcb = new populateComboBoxes(); pcb.LabSessionInfo(roomID, lecturer, unit); myTimer.start(); Timer timerDuration = new Timer(durationMilliSec, timerDurationActionEvent); timerDuration.start(); } }; public void BluetoothScan(Timer myTimer) { BluetoothDeviceDiscovery scan = new BluetoothDeviceDiscovery(); try { myTimer.stop(); scan.main(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } myTimer.start(); }; } </code></pre> <p>Thankyou in advance for any help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload