Reputation: 93
I have jbutton1 which has a text "Connect" when the user clicks the jbutton1 the application runs and the text of jbutton1 changes from "Connect" to "Disconnect". Now again when the user clicks jbutton1 and the text is "Disconnect" so the application should close.
The code i have given here does not checks anything it automatically closes the application which i don't want. I want to check the text of the button if it is connect i should do a different process and if it is disconnect it shouls close my application when the button is clicked.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(jComboBox2.getSelectedItem()==null)
{
System.out.println("Select one name");
}
else
{
try {
Runtime r = Runtime.getRuntime();
p = Runtime.getRuntime().exec("C:\\Program Files");
AS a4sObj = new AS(new String[]{jComboBox2.getSelectedItem().toString()});
} catch (IOException ex) {
Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
}
jButton1.setText("Disconnect"); //This sets connect button to disconnect
if(jButton1.getText()== "Disconnect") // I wanted to change this line of code
{
System.exit(0);
}
Upvotes: 0
Views: 61
Reputation: 36987
Just move setText
to the end:
if(jButton1.getText()== "Disconnect") // I wanted to change this line of code
{
System.exit(0);
}
jButton1.setText("Disconnect"); //This sets connect button to disconnect
Upvotes: 1
Reputation: 48287
This is wrong
if(jButton1.getText()== "Disconnect")
, take a look at this great answer to check the reason and learn how to compare Strings in java
You should do:
if("Disconnect".equalsIgnoreCase(jButton1.getText()))
instead
Upvotes: 1