I'm trying out a simple threading application, wherein, I try to detect a list of Bluetooth users in the vicinity.
In its implementation, I've got a Swings form, which contains a 'Start' button and a TextArea.
On clicking 'Start', I look for the devices by calling a method detectData, which has an implementation of its own. When this happens, I ought to have a "Searching for devices..." message being screened on the TextArea. During this time, the 'Start' button is supposed to be disabled, I should be able to minimize the window, perform any other task on the machine and come back to it.
I've threaded the detectData() method. When this method done processing, it's supposed to show the found list in the TextArea.
The problem is, in the current implementation, the thread is eating up the entire functionality. I don't even get the "Searching for devices.." message on the textarea. I'm unable to close the window. Once the thread is done processing, things get back to normal, with the list shown and other functionality retained.
How do I fix this issue?
This is the code that I'm working with, as of now.
Expand|Select|Wrap|Line Numbers
- //This is the action listener for the Search button
- private void StartActionPerformed(java.awt.event.ActionEvent evt)
- {
- Start.setEnabled(false);
- Refresh.setEnabled(false);
- TextArea.setText("Searching for Devices...\n");
- t = new Thread(this, "Detect");
- t.start();
- try {
- t.join();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- if(detect_data.deviceCount <= 0) {
- TextArea.setText("No Devices Found");
- }
- else {
- TextArea.setText("Devices Found:\n");
- for (int i = 0; i <detect_data.deviceCount; i++) {
- TextArea.append(detect_data.Address[i]+" ("+detect_data.Name[i]+")\n");
- }
- Start.setEnabled(true);
- Refresh.setEnabled(true);
- }
- }
- //This is the Run() method
- public void run() {
- detect_data = new Main();
- try {
- detect_data.detect();
- }
- catch(Exception e)
- {
- //Catch exceptions
- }
- }