Olá esou tentando adicionar uma ação em diversos JButtons criados dinamicamente para que ao clicar nele chame uma outra tela passando o parametro…
Está tela é do tipo JDialog e não estou conseguindo chama-la, se chamo um JFrame funciona sem problemas…
o trecho:
[code]
botao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
new Acao(AcaoPreventiva(),true).setVisible(true);
}
});[/code]
Tentei também:
[code] botao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
new Acao(this,true).setVisible(true);
}
});[/code]
O que está em uso:
public class AcaoPreventiva extends javax.swing.JFrame {
/**
* Creates new form AcaoPreventiva
*/
Etapa etp = new Etapa();
EtapaDAO dao = new EtapaDAO();
public AcaoPreventiva(){
initComponents();
}
public AcaoPreventiva(Equipamento equipa) {
//super(parent, modal);
initComponents();
//Deixa a tela no centro
setLocationRelativeTo(getRootPane());
//Define o titulo da janela
this.setTitle("Preventiva "+ equipa.getNome());
//Seta o Layout como box para adicionar os botões um abaixo do outro
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
//Faz uma busca pelo TIPO do equipamento retornando as etapas dele
List<Etapa> etapas = dao.getListaWhereTipo(equipa.getTipo());
for(Etapa etapa : etapas){
etp.setDescricao(etapa.getDescricao());
etp.setIdetapa(etapa.getIdetapa());
etp.setNome(etapa.getNome());
etp.setTipo(etapa.getTipo());
JButton botao;
botao = new JButton(etapa.getNome());
//Seta o tamanho do botão
Dimension dim = new Dimension();
dim.setSize(2060, 30);
botao.setMinimumSize(dim);
botao.setMaximumSize(dim);
//Adiciona o actionlistener
//botao.addActionListener(this);
[color=red]botao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
new Acao(AcaoPreventiva(),true).setVisible(true);
}
}); [/color]
//Muda as letras apenas
//botao.setForeground(Color.red);
// NÂO FUNCIONANDO botao.setBackground(Color.BLACK);
//Adiciona o botão no pane(Jpanel que está dentro do Scrollpane)
pane.add(botao);
scroll.getViewport().setView(pane);
}
}[/code]
A classe inteira:
[code]/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package view;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import model.DAO.EtapaDAO;
import model.TO.Equipamento;
import model.TO.Etapa;
/**
*
* @author F8051921
*/
public class AcaoPreventiva extends javax.swing.JFrame {
/**
* Creates new form AcaoPreventiva
*/
Etapa etp = new Etapa();
EtapaDAO dao = new EtapaDAO();
public AcaoPreventiva(){
initComponents();
}
public AcaoPreventiva(Equipamento equipa) {
//super(parent, modal);
initComponents();
//Deixa a tela no centro
setLocationRelativeTo(getRootPane());
//Define o titulo da janela
this.setTitle("Preventiva "+ equipa.getNome());
//Seta o Layout como box para adicionar os botões um abaixo do outro
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
//Faz uma busca pelo TIPO do equipamento retornando as etapas dele
List<Etapa> etapas = dao.getListaWhereTipo(equipa.getTipo());
for(Etapa etapa : etapas){
etp.setDescricao(etapa.getDescricao());
etp.setIdetapa(etapa.getIdetapa());
etp.setNome(etapa.getNome());
etp.setTipo(etapa.getTipo());
JButton botao;
botao = new JButton(etapa.getNome());
//Seta o tamanho do botão
Dimension dim = new Dimension();
dim.setSize(2060, 30);
botao.setMinimumSize(dim);
botao.setMaximumSize(dim);
//Adiciona o actionlistener
//botao.addActionListener(this);
botao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
new Acao(AcaoPreventiva(),true).setVisible(true);
}
});
//Muda as letras apenas
//botao.setForeground(Color.red);
// NÂO FUNCIONANDO botao.setBackground(Color.BLACK);
//Adiciona o botão no pane(Jpanel que está dentro do Scrollpane)
pane.add(botao);
scroll.getViewport().setView(pane);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
scroll = new javax.swing.JScrollPane();
pane = new javax.swing.JPanel();
jComboBox1 = new javax.swing.JComboBox();
lblPreventiva = new javax.swing.JLabel();
btnOk = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
javax.swing.GroupLayout paneLayout = new javax.swing.GroupLayout(pane);
pane.setLayout(paneLayout);
paneLayout.setHorizontalGroup(
paneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 595, Short.MAX_VALUE)
);
paneLayout.setVerticalGroup(
paneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 306, Short.MAX_VALUE)
);
scroll.setViewportView(pane);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Não", "Sim" }));
lblPreventiva.setText("Preventiva completa?");
btnOk.setText("Ok");
btnCancel.setText("Cancelar");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scroll)
.addGroup(layout.createSequentialGroup()
.addComponent(lblPreventiva)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnOk)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnCancel)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(scroll)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblPreventiva)
.addComponent(btnOk)
.addComponent(btnCancel))
.addContainerGap())
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AcaoPreventiva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AcaoPreventiva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AcaoPreventiva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AcaoPreventiva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AcaoPreventiva().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnOk;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel lblPreventiva;
private javax.swing.JPanel pane;
private javax.swing.JScrollPane scroll;
// End of variables declaration
}