Reputation: 85
I'm creating buttons by this for, and when i click him.. not happening anything... When i click in btn, not calling the function btnActionPerformed... how to make it work?
private void btButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
int[] ret = new int[SQL.freetables().size()];
Iterator<Integer> iterator = SQL.freetables().iterator();
for (int i = 0; i < ret.length; i++)
{
ret[i] = iterator.next().intValue();
int num=SQL.freetables().size() + 1;
this.btn = new JButton();
this.btn.setText("" + ret[i]);
this.btn.setSize(60,20);
int x = 100+(80*i);
this.btn.setLocation(x, 140);
this.btn.setVisible(true);
this.add(btn);
// }
}
this.revalidate();
this.repaint();
}
private void btnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.print("\b Test: " + btn.getText());
}
Upvotes: 1
Views: 5472
Reputation: 11577
you need to register to actionPreformed
this.btn.addActionListener(this);
your code should be:
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// TODO add your handling code here
int[] ret = new int[SQL.freetables().size()];
Iterator<Integer> iterator = SQL.freetables().iterator();
for (int i = 0; i < ret.length; i++)
{
ret[i] = iterator.next().intValue();
int num=SQL.freetables().size() + 1;
this.btn = new JButton();
this.btn.setText("" + ret[i]);
this.btn.setSize(60,20);
int x = 100+(80*i);
this.btn.setLocation(x, 140);
this.btn.setVisible(true);
this.add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// TODO add your handling code here:
System.out.print("\b Test: " + btn.getText());
}
}
this.revalidate();
this.repaint();
}
}
});
Upvotes: 3
Reputation: 308763
You have to implement the ActionListener
interface. Neither of these methods match the required signature that I can see.
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
The method is actionPerformed
. The listener has to be attached to the JButton
. I see neither one in your code.
You appear to be in dire need of a Swing tutorial.
Upvotes: 3