Pessoal é o seguinte: Fiz um JFrame e quero gravar os dados em um TXT.. está faltando algo para executar a classe de gravação mais não sei o que é :oops:
Seguem os arquivos abaixo:
TELApackage javaapplication;
import javax.swing.JOptionPane;
/**
*
* @author @Lucas
*/
public class Principal extends javax.swing.JFrame {
/** Creates new form Principal */
public Principal() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtNome = new javax.swing.JTextField();
txtSobrenome = new javax.swing.JTextField();
ComboUser = new javax.swing.JComboBox();
radioMacho = new javax.swing.JRadioButton();
radioFemea = new javax.swing.JRadioButton();
jPanel2 = new javax.swing.JPanel();
ckTeste2 = new java.awt.Checkbox();
ckTeste1 = new java.awt.Checkbox();
jScrollPane1 = new javax.swing.JScrollPane();
txtHistorico = new javax.swing.JTextArea();
btSalvar = new javax.swing.JButton();
btSair = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
btSair1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("JFrame 001");
setBackground(new java.awt.Color(204, 204, 204));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jLabel1.setText("Nome");
jLabel2.setText("Sobre Nome");
jLabel3.setText("User");
jLabel4.setText("Sexo");
txtSobrenome.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtSobrenomeActionPerformed(evt);
}
});
ComboUser.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "João", "Jose", "Carlos" }));
ComboUser.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ComboUserActionPerformed(evt);
}
});
buttonGroup1.add(radioMacho);
radioMacho.setText("Macho");
radioMacho.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radioMachoActionPerformed(evt);
}
});
buttonGroup1.add(radioFemea);
radioFemea.setText("Fêmea");
jPanel2.setBackground(new java.awt.Color(153, 153, 153));
ckTeste2.setLabel("teste 2");
ckTeste1.setLabel("teste 1");
ckTeste1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
ckTeste1ItemStateChanged(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ckTeste2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ckTeste1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(95, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(ckTeste1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ckTeste2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
txtHistorico.setColumns(20);
txtHistorico.setRows(5);
jScrollPane1.setViewportView(txtHistorico);
btSalvar.setText("Salvar");
btSalvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSalvarActionPerformed(evt);
}
});
btSair.setText("Sair");
btSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSairActionPerformed(evt);
}
});
jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javaapplication/ajax-loader.gif"))); // NOI18N
jLabel6.setText("Campo texto");
jLabel7.setText("Escolha uma opção:");
btSair1.setText("Ler TXT");
btSair1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSair1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(ComboUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 205, Short.MAX_VALUE)
.addComponent(btSair1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(42, 42, 42)
.addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(txtSobrenome, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btSair, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btSalvar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 158, Short.MAX_VALUE)
.addComponent(jLabel5))
.addComponent(jLabel6)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(radioMacho)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(radioFemea)
.addGap(224, 224, 224))
.addComponent(jLabel7))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(btSalvar)
.addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtSobrenome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btSair))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(ComboUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btSair1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(radioMacho)
.addComponent(radioFemea))
.addGap(27, 27, 27)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(36, 36, 36)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel5))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void ComboUserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtSobrenomeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void radioMachoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
ClasseAuxiliar obj = new ClasseAuxiliar();
private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {
//pegando os campos texto
obj.setNome( txtNome.getText() );
obj.setSobrenome( txtSobrenome.getText() );
obj.setHistorico( txtHistorico.getText() );
//pegando o valor do combo
obj.setUser( (String)ComboUser.getSelectedItem() );
//pegando o valor selecionado do Radio
if ( radioMacho.isSelected() )
obj.setSexo( "Masculino" );
else
obj.setSexo( "Feminino" );
//verificando se o checkbox está marcado
if ( ckTeste1.getState())
obj.setTeste1(true);
if ( ckTeste2.getState())
obj.setTeste2(true);
[color=666666]
//confirmando os valores digitados
JOptionPane.showMessageDialog(null, "Confirmação dos valores digitados: \n" +
"Nome: "+obj.getNome()+ "\nSobrenome: "+obj.getSobrenome() +
"\nUsuario no combo: "+obj.getUser() + "\nSexo: "+obj.getSexo() +
"\nCheckBox 1: "+obj.getTeste1() + "\nCheckBox 2: "+obj.getTeste2() +
"\nCampo Texto: "+obj.getHistorico() + "\n");
//gravando e confirmando o cadastro
String registro = "Nome: " + obj.getNome() + "\n Sobrenome: " + obj.getSobrenome();
GravarDadosTXT dados = new GravarDadosTXT();
dados.setTxt(registro);
JOptionPane.showMessageDialog(null,"Cadastro realizado com sucesso");
[/color]
//zerando a tela e limpando os valores das variáveis
txtNome.setText("");
txtSobrenome.setText("");
txtHistorico.setText("");
if ( radioMacho.isSelected() || radioFemea.isSelected() )
buttonGroup1.clearSelection();
ckTeste1.setState(false);
obj.setTeste1(false);
ckTeste2.setState(false);
obj.setTeste2(false);
ComboUser.setSelectedIndex(-1);
}
private void ckTeste1ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
}
private void btSairActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void btSair1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox ComboUser;
private javax.swing.JButton btSair;
private javax.swing.JButton btSair1;
private javax.swing.JButton btSalvar;
private javax.swing.ButtonGroup buttonGroup1;
private java.awt.Checkbox ckTeste1;
private java.awt.Checkbox ckTeste2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JRadioButton radioFemea;
private javax.swing.JRadioButton radioMacho;
private javax.swing.JTextArea txtHistorico;
private javax.swing.JTextField txtNome;
private javax.swing.JTextField txtSobrenome;
// End of variables declaration
}
package javaapplication;
import java.io.*;
/**
*
* @author @Lucas
*/
public class GravarDadosTXT {
public String txt;
GravarDadosTXT() {
}
public void GravarDadosTXT(String txt) throws IOException {
File f = new File("C:/Documents and Settings/User/Desktop/diret.txt");
if (!f.exists()) {
f.createNewFile();
}
try{
FileWriter fw = new FileWriter(f,true);
fw.write(txt);
fw.flush();
fw.close();
}catch(Exception e){e.printStackTrace();}
}
/**
* @return the txt
*/
public String getTxt() {
return txt;
}
/**
* @param txt the txt to set
*/
public void setTxt(String txt) {
this.txt = txt;
}
}

