Reputation: 1256
So I've created an Object of a class called "Homescreen" which is undefined. Once the button is click the object is initialized and the thread starts to check if the boolean 'ScanForSpywares' is true or false. If true it performs a certain task. The boolean is by default false and set true by a button once it is clicked.
Thread liveCheck = new Thread(new Runnable(){
@Override
public synchronized void run(){
while(running){
try{
if(serv.connection!=null){
if(serv.connection.isClosed()){
btnDisconnect.setEnabled(false);
btnConnect.setEnabled(true);
}else if(!serv.connection.isClosed()){
btnConnect.setEnabled(false);
btnDisconnect.setEnabled(true);
}
}else{
btnDisconnect.setEnabled(false);
}
if(isHome){
btnHome.setEnabled(false);
}else if(!isHome){
btnHome.setEnabled(true);
}
/* THIS IS THE PROBLEM (THE ONE BELOW) */
if(sc!=null){
// The code works fine till here
if(sc.ScanForSpywares){
serv.LoadMYSQLSettings();
System.out.println("works");
sc.ScanForSpywares=false;
}
}
}catch(Exception e){
}
}
}
});
This is the code for the other class. Everything works fine, but when I click the button "Scan", nothing happens. So I tried printing out a simple message, turns out it doesn't even go there. Any idea what might it be..I've been working for long hours so might be a silly problem my brain isn't able to detect:/
public class HomeScreen extends JFrame {
private JPanel contentPane;
public JList list;
public DefaultListModel model = new DefaultListModel();
public boolean ScanForSpywares = false;
public HomeScreen() {
setResizable(false);
setTitle("Spyware Interface");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 699, 399);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblFrtzfoxy = new JLabel("FrtzFoxy");
lblFrtzfoxy.setForeground(new Color(30, 144, 255));
lblFrtzfoxy.setFont(new Font("Tahoma", Font.PLAIN, 39));
lblFrtzfoxy.setBounds(10, 11, 168, 41);
contentPane.add(lblFrtzfoxy);
JLabel lblCyberIntelligence = new JLabel("Cyber Intelligence Corporation");
lblCyberIntelligence.setForeground(new Color(0, 0, 255));
lblCyberIntelligence.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblCyberIntelligence.setBounds(12, 47, 141, 14);
contentPane.add(lblCyberIntelligence);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setBounds(10, 67, 143, 263);
contentPane.add(scrollPane);
list = new JList(model);
scrollPane.add(list);
list.setBounds(329, 223, 1, 1);
JLabel lblActiveNan = new JLabel("Active: NaN");
lblActiveNan.setFont(new Font("Roboto Cn", Font.BOLD, 22));
lblActiveNan.setBounds(10, 330, 143, 34);
contentPane.add(lblActiveNan);
JButton btnScan = new JButton("Scan");
btnScan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
ScanForSpywares=true;
}
});
btnScan.setBounds(604, 0, 89, 23);
contentPane.add(btnScan);
}}
Upvotes: 1
Views: 76
Reputation: 46607
There is a few things odd here, all of which are possible causes.
First make sure, sc
is actually the same instance of HomeScreen
(not clear from the snippets given). Second, declare the boolean as volatile
to make sure the 2nd thread sees changes coming from the main/UI thread. However, you should read up more on what volatile
does, it is often mis-used.
Last but not least: since your code silently catches all exceptions, you would not notice if anything (i.e. serv.LoadMYSQLSettings();
) goes wrong.
Upvotes: 1