Reputation: 17
This post seems to be duplicated, but I not understand the solution in the same. I can not understand why the jbutton only works when I click two times.
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String address = jTextField1.getText();
Socket sock = null;
try {
InetAddress addr;
sock = new Socket(address, 80);
addr = sock.getInetAddress();
sock.close();
jLabel2.setText("Status: Online");
} catch (IOException ex) {
jLabel2.setText("Status: Offline");
}
}
});
}
Upvotes: 0
Views: 416
Reputation: 347184
jButton1MouseClicked
would suggest that a MouseListener
is already registered against the button through Netbeans.
Remove the registration of the ActionListener
from with the jButton1MouseClicked
handler...
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
String address = jTextField1.getText();
Socket sock = null;
try {
InetAddress addr;
sock = new Socket(address, 80);
addr = sock.getInetAddress();
sock.close();
jLabel2.setText("Status: Online");
} catch (IOException ex) {
jLabel2.setText("Status: Offline");
}
}
Having said that, go back into Netbeans form editor and remove the mouseClicked
handler associated with the button and either add the ActionListener
manually (after the call to initComponents
) or a "action" handler through the Netbeans form editor
The reason been is, buttons can be actioned through other means, like the keyboard
Upvotes: 1