Galera como faço para chamar um novo form usando swing, preciso de algo parecido com um poupup, onde chamaria através de um botão e fecharia depois de uma ação algo similiar a um window.open e window.close na web?
Como chamar um novo form em SWING?
9 Respostas
Mas esse form é modal(Como uma JDialog), ou é inerente(está contido) junto ao frame pai que o chamou(no caso seria uma internalFrame)?
Seria um form indepedente do principal, um novo form com somente um label um combo e um botão, não sei em que tipo se encaixa, minha praia é web.
O meu caso é tenho o evento do botão aqui:
private void btnTransferenciaActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("transferencia");
}
Preciso chamar essa classe
package br.com.xx.view;
/**
*
* @author fabio.pedrosa
*/
public class Transferencia extends javax.swing.JFrame {
/** Creates new form Transferencia */
public Transferencia() {
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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Transfer\u00eancia"));
jLabel1.setText("Para:");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jButton1.setText("Tranferir");
jButton2.setText("Cancelar");
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(jButton1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton2))
.add(jComboBox1, 0, 181, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 14, Short.MAX_VALUE)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton1)
.add(jButton2))
.addContainerGap())
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Transferencia().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration//GEN-END:variables
}
Mas preciso retirar esse método main da classe Transferencia, pois contém na outra classe como fazer para funcionar essa chamada?
Se for oq fiz, eu coloquei no evento ActionPerformed de um botao:
formAtual.setVisibleTrue(false);
if (novoForm == null)
novoForm = new Form2();
novoForm.setVisible(true);
Funcionou, fiz assim:
private void btnTransferenciaActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("transferencia");
Transferencia transferencia = new Transferencia();
transferencia.setVisible(true);
}
Mas quando clico no botão fechar da janela fecha os 2 forms, eu preciso manter o principal aberto, o que devo fazer?
Consegui, retirei a linha, da classe Transferencia
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Agora preciso fechar pelo botão cancelar também somente esse form, o que usar? sendo que System.exit(0); fecha tudo
esse negocio d fechar e abrir sem detonar o objeto vc faz alternando setVisible(boolean b) c/ b=true ou false, ok?
Certinho valeu mano!
Consegui, retirei a linha, da classe TransferenciaAgora preciso fechar pelo botão cancelar também somente esse form, o que usar? sendo que
System.exit(0); fecha tudo
Não remova essa linha, apenas mude ela para:
E para fechar o form ‘transferencia’, basta usar o método dispose();