1
Hello I need to create n Jbuttons, the creation criterion is a table in the database that has n items. I have to create a button for each item and when I click the button it has to show me a message of the number of that item. Creating the button was easy, but I don’t know how to create these methods.
int qtd=cartela.qtdCartelas();
JToggleButton btn[] = new JToggleButton[qtd];
for(int i=0;i<qtd;i++)
{
numeroBotao.next();
btn[i]=new JToggleButton(numeroBotao.getString(1));
panel.add(btn[i]);
System.out.println();
}
Ao clicar no botão necessito gerar um evento.
Conseguir fazer isso:
for(int i=0;i<qtd;i++)
{
//Cria os actionListener dos botões.
btn[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Necessito pegar o Texto do botão ou a posição dele no vetor
int l = Integer.parseInt(btn[i].getText());
//
}
});
}
Does actionPerformed at the end of the code not work? What’s wrong with it? Remembering that you are using Jtogglebutton, they are a little different from the Jbuttons
– user28595