Reputation: 15
I'm trying to make TIC TAC TOE game using netbeans and i'm really really new to JAVA when i finished design Jframe netbeans automatically generated codes for my jButtons the problem is all the code which netbeans generated for me is uneditable and very hard for me to understand
The button is for X O where you click on it and X O text appear and i want it to be disable after one click i tried
jButton.setEnabled(false);
but it greyed out my button so i don't use it because i want my button to have color so
i tried
jButton.removeActionlistener(this);
instead but its not working and i dont know what to do anymore because i dont understand the code they generated for me
Here the code they provide me i can't edit it there're so many button so there're lots of code too so i'm just gonna put an example here of what netbeans gave me
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton1.setBackground(new java.awt.Color(204, 204, 204));
jButton1.setFont(new java.awt.Font("Tahoma", 1, 96)); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//my code here
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
}
i hoped that after i used
jButton.removeActionlistener(this);
in the method it will not allow me to click the button after first click and not greyed out my button but nothing happend please help me
thanks in advance.
Upvotes: 1
Views: 983
Reputation:
netbeans automatically generated codes for my jButtons the problem is all the code which netbeans generated for me is uneditable
There is a reason why the code is protected. You should change that code by going through the properties of the GUI designer. E.g. if you want to remove the code for an event, you should do that in the "Events" tab of the component's property sheet:
If you click on Remove
in the dialog, the event handler will be removed completely from your code. You can achieve the same by simply deleting the text in the editable text field (where the name of the method is shown).
but it greyed out my button
That's driven by the look and feel. Disabled components are rendered differently than enabled components.
I tried
jButton.removeActionlistener(this);
That doesn't work, because NetBeans uses an anonymous inner class to register the ActionListener as you can see in the initComponents()
method.
If you want to add and remove the ActionListener dynamically, you need to tell the GUI Designer to use the main class as the listener. This can be done under Tools → Options → Java → GUI Builder:
However once you have changed that, you need to re-generated your event handler. You need to remove and re-add it to apply the new generation style.
I typically create my own methods that handle the events and just call them from within the (generated) event handler:
As you can see, the code in the event handler simply dispatches to another method. The advantage of that is, that even if you delete the event handler through the GUI designer, you don't lose the code that you have already written.
NetBeans' GUI designer is an extremely powerful tool, but it doesn't remove the need to understand the underlying Swing framework and Java concepts. And as with all powerful tools, it needs some time investment to understand it.
You should go through the GUI designer tutorials on the NetBeans homepage.
You might want to start with the following two.
I haven't looked at them (because I prefer text over video), but you might also want to check out NetBeans' YouTube channel if it contains any tutorials for the GUI designer.
Upvotes: 1
Reputation: 6045
Does
jButton.removeActionlistener(this);
even compile?
You could remove all listener from the button like this:
for (ActionListener listener : jButton1.getActionListeners())
{
jButton1.removeActionListener(listener);
}
Or, instead of messing with the listener, you could keep track of the status, something like this pseudocode:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (buttonAlreadyClicked)
// do nothing or play a sound or tell the user somehow else that this button was already clicked
else
changeToXorO
buttonAlreadyClicked = true
}
Upvotes: 0