[RESOLVIDO] Habilitando botão de uma outra classe

2 respostas
K

Olá galera … estou utilizando o NetBeans, possuo duas classes a principal que tem um botão (bt1) e uma JDesktopPane (extends JFrame) e uma outra classe que tem um botão(btOK) (extends JInternalFrame) quando eu clico no bt1 no actionPerformed eu adiciono a JInternalPane e seto o bt1 como false (setEnabled(false)), até ai tudo bem! minha dúvida é, quando eu aperto o btOk gostaria que meu bt1 ficasse ENABLED de novo mas não estou conseguindo, já tentei criar uma outra classe auxiliar, já tentei mandar por parâmetro TRUE e utilizar o repaint e não funfa, alguém poderia me dar uma luz?? obrigado!

classe principal

public class teste extends javax.swing.JFrame {
    intFrame frame = new intFrame(); //criei uma variável do tipo da classe intFrame para
                                     // adicionar ao JDesktopPane

    /** Creates new form teste */
    public teste(boolean vf) {      //coloquei para receber por parametro o false ou true para setar no botão
        initComponents();
        bt1.setEnabled(vf);  //seta o valor recebido por parametro
    }

    /** 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() {

        desktop = new javax.swing.JDesktopPane();
        bt1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        bt1.setText("jButton1");
        bt1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(desktop, javax.swing.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(342, 342, 342)
                        .addComponent(bt1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(bt1)
                .addGap(18, 18, 18)
                .addComponent(desktop, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>
    
    private void bt1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        desktop.add(frame); //aqui adiciono a classe intFrame ao JDesktopPane
        desktop.repaint();
        bt1.setEnabled(false); // seto o botão para false
}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new teste(true).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    public static javax.swing.JButton bt1;
    private javax.swing.JDesktopPane desktop;
    // End of variables declaration
}

classe intFrame

public class intFrame extends javax.swing.JInternalFrame {
    /** Creates new form intFrame */
    public intFrame() {
        initComponents();
    }

    /** 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() {

        btOK = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setClosable(true);
        setTitle("Teste");
        setFocusCycleRoot(false);
        setFocusable(false);
        setRequestFocusEnabled(false);
        setVerifyInputWhenFocusTarget(false);
        setVisible(true);

        btOK.setText("OK");
        btOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(149, 149, 149)
                .addComponent(btOK, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(176, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(102, 102, 102)
                .addComponent(btOK)
                .addContainerGap(148, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void btOKActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        new teste(true);
        dispose();
}


    // Variables declaration - do not modify
    private javax.swing.JButton btOK;
    // End of variables declaration

}

2 Respostas

ivela

Olá!

Coloque um actionEvent no seu botão de OK e nele, defina o setEnabled como true (ex.: Teste.getBotao1().setEnabled(true);).

Abraços!

K

nossa cara muito obrigado!!! … Deus que te envio pra me responder essa Dúvida! :smiley: … Deus Abençoe sua semana! :smiley:

Criado 13 de julho de 2009
Ultima resposta 13 de jul. de 2009
Respostas 2
Participantes 2