public class main extends javax.swing.JFrame {
public main() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel4 = new javax.swing.JLabel();
labelID = new javax.swing.JLabel();
labelName = new javax.swing.JLabel();
labelDep = new javax.swing.JLabel();
id = new javax.swing.JTextField();
name = new javax.swing.JTextField();
dep = new javax.swing.JTextField();
save = new javax.swing.JButton();
load = new javax.swing.JButton();
jLabel4.setText("jLabel4");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
labelID.setText("ID");
labelName.setText("Name");
labelDep.setText("Department");
id.setText("2234");
name.setText("name");
dep.setText("dep");
save.setText("Save");
save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
load.setText("Load");
load.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loadActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(labelID)
.addComponent(labelName)
.addComponent(labelDep))
.addGap(42, 42, 42)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(id)
.addComponent(name)
.addComponent(dep, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(save)
.addGap(18, 18, 18)
.addComponent(load)))
.addContainerGap(34, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelID)
.addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelName)
.addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelDep)
.addComponent(dep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(save)
.addComponent(load))
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
saveToFile();//Show the save dialog.
}
private void saveToFile(){
JFileChooser fileChooser=new JFileChooser(System.getProperty("user.dir"));
fileChooser.setDialogTitle("Save as...");
fileChooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter restric=new FileNameExtensionFilter("(*.txt)","txt");
fileChooser.addChoosableFileFilter(restric);
fileChooser.showSaveDialog(this);
File file=fileChooser.getSelectedFile();
try{
FileWriter fwriter=new FileWriter(file);
BufferedWriter bwriter=null;
bwriter=new BufferedWriter(fwriter);
bwriter.write(saveData());
bwriter.flush();
bwriter.close();
fwriter.close();
}catch(IOException e){
e.printStackTrace();
}
private String saveData(){
String a="";
a=a+id.getText()+System.lineSeparator()+name.getText()+System.lineSeparator()+dep.getText();
System.out.println(a);
return a;
}
}