Bình luận
|
Do Trung Duc [T2008A]
2021-04-12 03:42:02
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package PersonXML;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author TrungDuc
*/
public class PersonParse extends DefaultHandler {
List<Person> personList = new ArrayList<>();
Person person = null;
boolean isPerson = false;
boolean isName = false;
boolean isAge = false;
boolean isLanguage = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("person")) {
isPerson = true;
person = new Person();
}
if (isPerson) {
if (qName.equalsIgnoreCase("name")) {
isName = true;
} else if (qName.equalsIgnoreCase("age")) {
isAge = true;
} else if (qName.equalsIgnoreCase("language")) {
isLanguage = true;
}
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String str = String.valueOf(ch, start, length);
if (isPerson) {
if (isName) {
person.setName(str);
} else if (isAge) {
person.setAge(Integer.parseInt(str));
} else if (isLanguage) {
person.setLanguage(str);
}
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("person")) {
isPerson = false;
personList.add(person);
person = null;
}
if (isPerson) {
if (qName.equalsIgnoreCase("name")) {
isName = false;
} else if (qName.equalsIgnoreCase("age")) {
isAge = false;
} else if (qName.equalsIgnoreCase("language")) {
isLanguage = false;
}
}
}
}
|
Do Trung Duc [T2008A]
2021-04-12 03:41:48
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package PersonXML;
/**
*
* @author TrungDuc
*/
public class Person {
String name, language;
int age;
public Person() {
}
public Person(String name, int age, String language) {
this.name = name;
this.language = language;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toXML() {
return " <person>\n"
+ " <name>" + name + "</name>\n"
+ " <age>" + age + "</age>\n"
+ " <language>" + language + "</language>\n"
+ " </person>\n";
}
}
|
Do Trung Duc [T2008A]
2021-04-12 03:41:25
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package PersonXML;
import XMLFile.LibraryParse;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author TrungDuc
*/
public class Main extends javax.swing.JFrame {
List<Person> personList = new ArrayList<>();
DefaultTableModel tableModel;
int index = -1;
/**
* Creates new form Main
*/
public Main() {
initComponents();
tableModel = (DefaultTableModel) tblPerson.getModel();
tblPerson.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
index = tblPerson.getSelectedRow();
Person person = personList.get(index);
txtname.setText(person.getName());
txtage.setText(String.valueOf(person.getAge()));
txtlanguage.setText(person.getLanguage());
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
readFileXML();
Display();
}
/**
* 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() {
txtname = new java.awt.TextField();
label1 = new java.awt.Label();
label2 = new java.awt.Label();
txtage = new java.awt.TextField();
label3 = new java.awt.Label();
txtlanguage = new java.awt.TextField();
btnSave = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
btnEdit = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblPerson = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
label1.setText("Age");
label2.setText("Name");
label3.setText("Language");
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
btnEdit.setText("Edit");
tblPerson.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"No", "Name", "Age", "Language"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane1.setViewportView(tblPerson);
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(20, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(label1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(btnSave)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnDelete)
.addGap(18, 18, 18)
.addComponent(btnEdit))
.addComponent(txtage, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
.addComponent(txtlanguage, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
.addComponent(txtname, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(410, 410, 410))
.addComponent(jScrollPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(21, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtlanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSave)
.addComponent(btnDelete)
.addComponent(btnEdit))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String name = txtname.getText();
int age = Integer.parseInt(txtage.getText());
String language = txtlanguage.getText();
Person person = new Person(name, age, language);
if (index >= 0) {
personList.set(index, person);
} else {
personList.add(person);
}
saveFileXML();
Display();
}
public String toXML(String personListString) {
return "<personList>\n"
+ "\n" + personListString
+ " </personList>";
}
public void saveFileXML(){
FileOutputStream fos = null;
try {
fos = new FileOutputStream("person.xml");
String personListString = "";
for (Person person : personList) {
personListString += person.toXML();
}
String contentString = toXML(personListString);
byte[] data = contentString.getBytes("utf8");
fos.write(data);
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally{
if(fos != null){
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void readFileXML(){
File file = new File("person.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parse;
try {
parse = factory.newSAXParser();
PersonParse personParse = new PersonParse();
parse.parse(file, personParse);
personList = personParse.personList;
} catch (ParserConfigurationException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (index >= 0) {
System.err.println(index);
int option = JOptionPane.showConfirmDialog(rootPane, "Are you sure to delete!");
System.err.println(option);
personList.remove(index);
index = -1;
} else {
JOptionPane.showMessageDialog(rootPane, "Chua chon phan tu de xoa");
}
saveFileXML();
Display();
}
public void Display() {
int i = 1;
tableModel.setRowCount(0);
for (Person person : personList) {
tableModel.addRow(new Object[]{i, person.getName(), person.getAge(), person.getLanguage()});
i++;
}
}
/**
* @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnEdit;
private javax.swing.JButton btnSave;
private javax.swing.JScrollPane jScrollPane1;
private java.awt.Label label1;
private java.awt.Label label2;
private java.awt.Label label3;
private javax.swing.JTable tblPerson;
private java.awt.TextField txtage;
private java.awt.TextField txtlanguage;
private java.awt.TextField txtname;
// End of variables declaration
}
|
Nguyễn Tiến Đạt [T2008A]
2021-04-08 16:50:48
|
#Interest.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2.lesson8.PersonalAnalyse;
import java.util.ArrayList;
/**
*
* @author MyPC
*/
public class Interest {
String sport, game;
ArrayList<String> music = new ArrayList<>();
public Interest() {
}
public String getSport() {
return sport;
}
public void setSport(String sport) {
this.sport = sport;
}
public String getGame() {
return game;
}
public void setGame(String game) {
this.game = game;
}
public ArrayList<String> getMusic() {
return music;
}
public void setMusic(String music1) {
this.music.add(music1);
}
@Override
public String toString() {
return "sport=" + sport + ", game=" + game + ", music=" + music;
}
}
#LearningEquipment.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2.lesson8.PersonalAnalyse;
import java.util.ArrayList;
/**
*
* @author MyPC
*/
public class LearningEquipment {
String laptop;
ArrayList<String> bookList = new ArrayList<>();
public LearningEquipment() {
}
public String getLaptop() {
return laptop;
}
public void setLaptop(String laptop) {
this.laptop = laptop;
}
public ArrayList<String> getBookList() {
return bookList;
}
public void setBookList(String book) {
this.bookList.add(book);
}
@Override
public String toString() {
return "laptop=" + laptop + ", bookList=" + bookList;
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2.lesson8.PersonalAnalyse;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author MyPC
*/
public class Main {
public static void main(String[] args) {
Personal person;
try {
File file = new File("thongtincanhan.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
PersonalParser personalParser = new PersonalParser();
parser.parse(file, personalParser);
person = personalParser.person;
System.out.println(person);
} catch (ParserConfigurationException | SAXException | IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
#Personal.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2.lesson8.PersonalAnalyse;
import java.util.ArrayList;
/**
*
* @author MyPC
*/
public class Personal {
String name, gender, birthday, status;
ArrayList<Interest> interestList = new ArrayList<>();
String programLanguage;
ArrayList<LearningEquipment> equipmentList = new ArrayList<>();
public Personal() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public ArrayList<Interest> getInterestList() {
return interestList;
}
public void setInterestList(ArrayList<Interest> interestList) {
this.interestList = interestList;
}
public String getProgramLanguage() {
return programLanguage;
}
public void setProgramLanguage(String programLanguage) {
this.programLanguage = programLanguage;
}
public ArrayList<LearningEquipment> getEquipmentList() {
return equipmentList;
}
public void setEquipmentList(ArrayList<LearningEquipment> equipmentList) {
this.equipmentList = equipmentList;
}
@Override
public String toString() {
return "Personal{" + "name=" + name + ", gender=" + gender + ", birthday=" + birthday + ", status=" + status + ", interestList=" + interestList + ", programLanguage=" + programLanguage + ", equipmentList=" + equipmentList + '}';
}
}
#PersonalFrame.form
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<NonVisualComponents>
<Component class="java.awt.Label" name="label1">
<Properties>
<Property name="text" type="java.lang.String" value="label1"/>
</Properties>
</Component>
</NonVisualComponents>
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="jPanel1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="jPanel1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JPanel" name="jPanel1">
<Properties>
<Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
<Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
<TitledBorder title="My profile"/>
</Border>
</Property>
</Properties>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel5" min="-2" pref="66" max="-2" attributes="0"/>
<Component id="jLabel8" alignment="0" min="-2" max="-2" attributes="0"/>
<Group type="103" alignment="0" groupAlignment="1" attributes="0">
<Component id="txtBirthday" min="-2" pref="343" max="-2" attributes="0"/>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="1" max="-2" attributes="0">
<Component id="jLabel4" max="32767" attributes="0"/>
<Component id="jLabel3" max="32767" attributes="0"/>
<Component id="jLabel2" max="32767" attributes="0"/>
<Component id="jLabel1" max="32767" attributes="0"/>
</Group>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="94" max="-2" attributes="0"/>
<Component id="txtStatus" min="-2" pref="343" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="txtGender" alignment="1" min="-2" pref="343" max="-2" attributes="0"/>
<Component id="txtName" alignment="1" min="-2" pref="343" max="-2" attributes="0"/>
</Group>
</Group>
</Group>
</Group>
</Group>
<Component id="jLabel12" alignment="0" min="-2" pref="176" max="-2" attributes="0"/>
<Group type="103" alignment="0" groupAlignment="1" max="-2" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel6" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jLabel7" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jLabel9" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jLabel10" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jLabel11" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="27" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="txtVPOP" max="32767" attributes="0"/>
<Component id="txtUSUK" max="32767" attributes="0"/>
<Component id="txtLanguage" pref="342" max="32767" attributes="0"/>
<Component id="txtGame" alignment="1" max="32767" attributes="0"/>
<Component id="txtSport" alignment="0" max="32767" attributes="0"/>
</Group>
</Group>
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel13" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jLabel15" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jLabel16" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="112" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="txtLaptop" pref="341" max="32767" attributes="0"/>
<Component id="txtBook1" max="32767" attributes="0"/>
<Component id="txtBook2" max="32767" attributes="0"/>
</Group>
</Group>
</Group>
<Component id="jLabel14" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="244" max="-2" attributes="0"/>
<Component id="btnImport" min="-2" pref="70" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="39" max="-2" attributes="0"/>
<Component id="btnExport" min="-2" pref="70" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace pref="174" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="27" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="txtName" min="-2" max="-2" attributes="0"/>
<Component id="jLabel1" min="-2" pref="24" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="27" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="jLabel2" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="31" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Group type="102" attributes="0">
<Component id="jLabel3" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="29" max="-2" attributes="0"/>
<Component id="jLabel4" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Component id="txtBirthday" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="txtStatus" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
</Group>
<Component id="txtGender" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="28" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Group type="102" attributes="0">
<Component id="jLabel5" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="22" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="jLabel6" min="-2" max="-2" attributes="0"/>
<Component id="txtSport" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="28" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel7" min="-2" max="-2" attributes="0"/>
<Component id="txtGame" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="22" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Group type="102" attributes="0">
<Component id="jLabel8" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="22" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="jLabel9" min="-2" max="-2" attributes="0"/>
<Component id="txtUSUK" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="23" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="jLabel10" min="-2" max="-2" attributes="0"/>
<Component id="txtVPOP" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="27" max="-2" attributes="0"/>
<Component id="jLabel11" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="txtLanguage" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="31" max="-2" attributes="0"/>
<Component id="jLabel12" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="27" max="-2" attributes="0"/>
<Component id="jLabel13" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="txtLaptop" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="26" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Group type="102" attributes="0">
<Component id="jLabel14" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="26" max="-2" attributes="0"/>
<Component id="jLabel15" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="txtBook1" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="29" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="jLabel16" min="-2" max="-2" attributes="0"/>
<Component id="txtBook2" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="28" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="btnImport" min="-2" pref="35" max="-2" attributes="0"/>
<Component id="btnExport" min="-2" pref="35" max="-2" attributes="0"/>
</Group>
<EmptySpace max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="13" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Name:"/>
</Properties>
</Component>
<Component class="java.awt.TextField" name="txtName">
<Properties>
<Property name="cursor" type="java.awt.Cursor" editor="org.netbeans.modules.form.editors2.CursorEditor">
<Color id="Text Cursor"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="txtNameActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="jLabel2">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="13" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Gender:"/>
</Properties>
</Component>
<Component class="java.awt.TextField" name="txtGender">
</Component>
<Component class="javax.swing.JLabel" name="jLabel3">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="13" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Birthday:"/>
</Properties>
</Component>
<Component class="java.awt.TextField" name="txtBirthday">
</Component>
<Component class="javax.swing.JLabel" name="jLabel4">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="13" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Status:"/>
</Properties>
</Component>
<Component class="java.awt.TextField" name="txtStatus">
</Component>
<Component class="javax.swing.JLabel" name="jLabel5">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="3"/>
</Property>
<Property name="text" type="java.lang.String" value="Interest:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel6">
<Properties>
<Property name="text" type="java.lang.String" value="Sport:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel7">
<Properties>
<Property name="text" type="java.lang.String" value="Game"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel8">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="13" style="2"/>
</Property>
<Property name="text" type="java.lang.String" value="Music:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel9">
<Properties>
<Property name="text" type="java.lang.String" value="US-UK:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel10">
<Properties>
<Property name="text" type="java.lang.String" value="V-POP:"/>
</Properties>
</Component>
<Component class="java.awt.TextField" name="txtVPOP">
</Component>
<Component class="javax.swing.JLabel" name="jLabel11">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="13" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Program Language:"/>
</Properties>
</Component>
<Component class="java.awt.TextField" name="txtSport">
</Component>
<Component class="java.awt.TextField" name="txtGame">
</Component>
<Component class="java.awt.TextField" name="txtUSUK">
</Component>
<Component class="java.awt.TextField" name="txtLanguage">
</Component>
<Component class="javax.swing.JLabel" name="jLabel12">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="3"/>
</Property>
<Property name="text" type="java.lang.String" value="Learning Equipment:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel13">
<Properties>
<Property name="text" type="java.lang.String" value="Laptop:"/>
</Properties>
</Component>
<Component class="java.awt.TextField" name="txtLaptop">
</Component>
<Component class="javax.swing.JLabel" name="jLabel14">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="13" style="2"/>
</Property>
<Property name="text" type="java.lang.String" value="Book List:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel15">
<Properties>
<Property name="text" type="java.lang.String" value="Book 1:"/>
</Properties>
</Component>
<Component class="java.awt.TextField" name="txtBook1">
</Component>
<Component class="javax.swing.JLabel" name="jLabel16">
<Properties>
<Property name="text" type="java.lang.String" value="Book 2:"/>
</Properties>
</Component>
<Component class="java.awt.TextField" name="txtBook2">
</Component>
<Component class="java.awt.Button" name="btnImport">
<Properties>
<Property name="label" type="java.lang.String" value="Import"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnImportActionPerformed"/>
</Events>
</Component>
<Component class="java.awt.Button" name="btnExport">
<Properties>
<Property name="label" type="java.lang.String" value="Export"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnExportActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Form>
#PersonalFrame.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2.lesson8.PersonalAnalyse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author MyPC
*/
public class PersonalFrame extends javax.swing.JFrame {
/**
* Creates new form PersonalFrame
*/
public PersonalFrame() {
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">//GEN-BEGIN:initComponents
private void initComponents() {
label1 = new java.awt.Label();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtName = new java.awt.TextField();
jLabel2 = new javax.swing.JLabel();
txtGender = new java.awt.TextField();
jLabel3 = new javax.swing.JLabel();
txtBirthday = new java.awt.TextField();
jLabel4 = new javax.swing.JLabel();
txtStatus = new java.awt.TextField();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
txtVPOP = new java.awt.TextField();
jLabel11 = new javax.swing.JLabel();
txtSport = new java.awt.TextField();
txtGame = new java.awt.TextField();
txtUSUK = new java.awt.TextField();
txtLanguage = new java.awt.TextField();
jLabel12 = new javax.swing.JLabel();
jLabel13 = new javax.swing.JLabel();
txtLaptop = new java.awt.TextField();
jLabel14 = new javax.swing.JLabel();
jLabel15 = new javax.swing.JLabel();
txtBook1 = new java.awt.TextField();
jLabel16 = new javax.swing.JLabel();
txtBook2 = new java.awt.TextField();
btnImport = new java.awt.Button();
btnExport = new java.awt.Button();
label1.setText("label1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("My profile"));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
jLabel1.setText("Name:");
txtName.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
txtName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtNameActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
jLabel2.setText("Gender:");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
jLabel3.setText("Birthday:");
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
jLabel4.setText("Status:");
jLabel5.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
jLabel5.setText("Interest:");
jLabel6.setText("Sport:");
jLabel7.setText("Game");
jLabel8.setFont(new java.awt.Font("Tahoma", 2, 13)); // NOI18N
jLabel8.setText("Music:");
jLabel9.setText("US-UK:");
jLabel10.setText("V-POP:");
jLabel11.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
jLabel11.setText("Program Language:");
jLabel12.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
jLabel12.setText("Learning Equipment:");
jLabel13.setText("Laptop:");
jLabel14.setFont(new java.awt.Font("Tahoma", 2, 13)); // NOI18N
jLabel14.setText("Book List:");
jLabel15.setText("Book 1:");
jLabel16.setText("Book 2:");
btnImport.setLabel("Import");
btnImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportActionPerformed(evt);
}
});
btnExport.setLabel("Export");
btnExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExportActionPerformed(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()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(94, 94, 94)
.addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtGender, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel7)
.addComponent(jLabel9)
.addComponent(jLabel10)
.addComponent(jLabel11))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtVPOP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtUSUK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtLanguage, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
.addComponent(txtGame, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtSport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel13)
.addComponent(jLabel15)
.addComponent(jLabel16))
.addGap(112, 112, 112)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtLaptop, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
.addComponent(txtBook1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtBook2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addComponent(jLabel14)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(244, 244, 244)
.addComponent(btnImport, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(39, 39, 39)
.addComponent(btnExport, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(174, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(29, 29, 29)
.addComponent(jLabel4))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(txtBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(22, 22, 22)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel6)
.addComponent(txtSport, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7)
.addComponent(txtGame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(22, 22, 22)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel8)
.addGap(22, 22, 22)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel9)
.addComponent(txtUSUK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(23, 23, 23)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel10)
.addComponent(txtVPOP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addComponent(jLabel11))
.addComponent(txtLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addComponent(jLabel12)
.addGap(27, 27, 27)
.addComponent(jLabel13))
.addComponent(txtLaptop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel14)
.addGap(26, 26, 26)
.addComponent(jLabel15))
.addComponent(txtBook1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel16)
.addComponent(txtBook2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnImport, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnExport, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
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()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtNameActionPerformed
private void btnImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImportActionPerformed
// TODO add your handling code here:
FileOutputStream fos = null;
try {
fos = new FileOutputStream("thongtincanhan.xml");
String str = importXML();
byte[] b = str.getBytes();
fos.write(b);
} catch (FileNotFoundException ex) {
Logger.getLogger(PersonalFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PersonalFrame.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(PersonalFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
JOptionPane.showMessageDialog(rootPane, "Import thanh cong!!");
}
}//GEN-LAST:event_btnImportActionPerformed
private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportActionPerformed
// TODO add your handling code here:
Personal person;
try {
File file = new File("thongtincanhan.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
PersonalParser personalParser = new PersonalParser();
parser.parse(file, personalParser);
person = personalParser.person;
txtName.setText(person.getName());
txtGender.setText(person.getGender());
txtBirthday.setText(person.getBirthday());
txtStatus.setText(person.getStatus());
txtSport.setText(person.getInterestList().get(0).getSport());
txtGame.setText(person.getInterestList().get(0).getGame());
txtUSUK.setText(person.getInterestList().get(0).getMusic().get(0));
txtVPOP.setText(person.getInterestList().get(0).getMusic().get(1));
txtLanguage.setText(person.getProgramLanguage());
txtLaptop.setText(person.getEquipmentList().get(0).getLaptop());
txtBook1.setText(person.getEquipmentList().get(0).getBookList().get(0));
txtBook2.setText(person.getEquipmentList().get(0).getBookList().get(1));
JOptionPane.showMessageDialog(rootPane, "Export thanh cong!!");
} catch (ParserConfigurationException | SAXException | IOException ex) {
Logger.getLogger(PersonalFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_btnExportActionPerformed
private String importXML(){
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<profile>\n" +
" <name>"+txtName.getText()+"</name>\n" +
" <gender>"+txtGender.getText()+"</gender>\n" +
" <birthday><![CDATA["+txtBirthday.getText()+"]]></birthday>\n" +
" <status>"+txtStatus.getText()+"</status>\n" +
" <interest>\n" +
" <sport>"+txtSport.getText()+"</sport>\n" +
" <game>"+txtGame.getText()+"</game>\n" +
" <music>\n" +
" <us-uk>"+txtUSUK.getText()+"</us-uk>\n" +
" <v-pop>"+txtVPOP.getText()+"</v-pop>\n" +
" </music>\n" +
" </interest>\n" +
" <program-language>"+txtLanguage.getText()+"</program-language>\n" +
" <learning-equipment>\n" +
" <laptop>\n" +
" <name>"+txtLaptop.getText()+"</name>\n" +
" </laptop>\n" +
" <book-list>\n" +
" <book>"+txtBook1.getText()+"</book>\n" +
" <book>"+txtBook2.getText()+"</book>\n" +
" </book-list>\n" +
" </learning-equipment>\n" +
"</profile>";
}
/**
* @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(PersonalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PersonalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PersonalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PersonalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PersonalFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private java.awt.Button btnExport;
private java.awt.Button btnImport;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
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.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private java.awt.Label label1;
private java.awt.TextField txtBirthday;
private java.awt.TextField txtBook1;
private java.awt.TextField txtBook2;
private java.awt.TextField txtGame;
private java.awt.TextField txtGender;
private java.awt.TextField txtLanguage;
private java.awt.TextField txtLaptop;
private java.awt.TextField txtName;
private java.awt.TextField txtSport;
private java.awt.TextField txtStatus;
private java.awt.TextField txtUSUK;
private java.awt.TextField txtVPOP;
// End of variables declaration//GEN-END:variables
}
#PersonalParser.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2.lesson8.PersonalAnalyse;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author MyPC
*/
public class PersonalParser extends DefaultHandler{
Personal person = null;
Interest interest = new Interest();
LearningEquipment equipment = new LearningEquipment();
boolean isProfile = false;
boolean isName = false;
boolean isGender = false;
boolean isBirthday = false;
boolean isStatus = false;
boolean isInterest = false;
boolean isSport = false;
boolean isGame = false;
boolean isMusic = false;
boolean isUSUK = false;
boolean isVPOP = false;
boolean isLanguage = false;
boolean isEquipment = false;
boolean isLaptop = false;
boolean isName1 = false;
boolean isBookList = false;
boolean isBook = false;
int check = 0;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase("profile")){
person = new Personal();
isProfile = true;
}
if(isProfile){
if(qName.equalsIgnoreCase("name") && check == 0){
isName = true;
check = 1;
}else if(qName.equalsIgnoreCase("gender")){
isGender = true;
}else if(qName.equalsIgnoreCase("birthday")){
isBirthday = true;
}else if(qName.equalsIgnoreCase("status")){
isStatus = true;
}else if(qName.equalsIgnoreCase("interest")){
isInterest = true;
}else if(qName.equalsIgnoreCase("program-language")){
isLanguage = true;
}else if(qName.equalsIgnoreCase("learning-equipment")){
isEquipment = true;
}
if(isInterest){
if(qName.equalsIgnoreCase("sport")){
isSport = true;
}else if(qName.equalsIgnoreCase("game")){
isGame = true;
}else if(qName.equalsIgnoreCase("music")){
isMusic = true;
}
}
if(isMusic){
if(qName.equalsIgnoreCase("us-uk")){
isUSUK = true;
}else if(qName.equalsIgnoreCase("v-pop")){
isVPOP = true;
}
}
if(isEquipment){
if(qName.equalsIgnoreCase("laptop")){
isLaptop = true;
}else if(qName.equalsIgnoreCase("book-list")){
isBookList = true;
}
}
if(isLaptop){
if(qName.equalsIgnoreCase("name")){
isName1 = true;
}
}
if(isBookList){
if(qName.equalsIgnoreCase("book")){
isBook = true;
}
}
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equalsIgnoreCase("profile")){
isProfile = false;
person.interestList.add(interest);
person.equipmentList.add(equipment);
}
if(isProfile){
if(qName.equalsIgnoreCase("name")){
isName = false;
}else if(qName.equalsIgnoreCase("gender")){
isGender = false;
}else if(qName.equalsIgnoreCase("birthday")){
isBirthday = false;
}else if(qName.equalsIgnoreCase("status")){
isStatus = false;
}else if(qName.equalsIgnoreCase("interest")){
isInterest = false;
}else if(qName.equalsIgnoreCase("program-language")){
isLanguage = false;
}else if(qName.equalsIgnoreCase("learning-equipment")){
isEquipment = false;
}
if(isInterest){
if(qName.equalsIgnoreCase("sport")){
isSport = false;
}else if(qName.equalsIgnoreCase("game")){
isGame = false;
}else if(qName.equalsIgnoreCase("music")){
isMusic = false;
}
}
if(isMusic){
if(qName.equalsIgnoreCase("us-uk")){
isUSUK = false;
}else if(qName.equalsIgnoreCase("v-pop")){
isVPOP = false;
}
}
if(isEquipment){
if(qName.equalsIgnoreCase("laptop")){
isLaptop = false;
}else if(qName.equalsIgnoreCase("book-list")){
isBookList = false;
}
}
if(isLaptop){
if(qName.equalsIgnoreCase("name")){
isName1 = false;
}
}
if(isBookList){
if(qName.equalsIgnoreCase("book")){
isBook = false;
}
}
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String str = String.valueOf(ch, start, length);
if(isProfile){
if(isName){
person.setName(str);
}else if(isGender){
person.setGender(str);
}else if(isBirthday){
person.setBirthday(str);
}else if(isStatus){
person.setStatus(str);
}else if(isInterest){
if(isSport){
interest.setSport(str);
}else if(isGame){
interest.setGame(str);
}else if(isMusic){
if(isUSUK){
interest.setMusic(str);
}else if(isVPOP){
interest.setMusic(str);
}
}
}else if(isLanguage){
person.setProgramLanguage(str);
}else if(isEquipment){
if(isLaptop){
if(isName1){
equipment.setLaptop(str);
}
}else if(isBookList){
if(isBook){
equipment.setBookList(str);
}
}
}
}
}
}
|
NguyenHuuThanh [T1907A]
2020-04-20 06:00:04
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaimportexportxml02;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author abc
*/
public class StudentFrame extends javax.swing.JFrame {
DefaultTableModel tablemodel;
List<Student> studentlist = new ArrayList<>();
/**
* Creates new form StudentFrame
*/
public StudentFrame() {
initComponents();
tablemodel = (DefaultTableModel) tblStudent.getModel();
}
/**
* 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() {
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();
jLabel5 = new javax.swing.JLabel();
txtFullName = new javax.swing.JTextField();
txtRollNo = new javax.swing.JTextField();
txtHobby = new javax.swing.JTextField();
txtLanguage = new javax.swing.JTextField();
btnSave = new javax.swing.JButton();
btnImportXML = new javax.swing.JButton();
btnExportXML = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
txtGender = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
tblStudent = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("FullName");
jLabel2.setText("RollNo");
jLabel3.setText("Hobby");
jLabel5.setText("Language");
txtLanguage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtLanguageActionPerformed(evt);
}
});
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnImportXML.setText("ImportXML");
btnImportXML.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportXMLActionPerformed(evt);
}
});
btnExportXML.setText("ExportXML");
btnExportXML.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExportXMLActionPerformed(evt);
}
});
jLabel6.setText("Gender");
txtGender.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Male", "Female" }));
tblStudent.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"FullName", "RollNo", "Hobby", "Gender", "Language"
}
));
jScrollPane1.setViewportView(tblStudent);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(133, 133, 133)
.addComponent(btnSave)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jScrollPane1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(76, 76, 76)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel5)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(jLabel6)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 123, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtRollNo, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtHobby, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(btnImportXML)
.addGap(137, 137, 137)
.addComponent(btnExportXML))))
.addGap(101, 101, 101)))
.addGap(0, 0, 0))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(61, 61, 61)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(60, 60, 60)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtRollNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(58, 58, 58)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtHobby, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addGap(26, 26, 26)
.addComponent(jLabel4)
.addGap(6, 6, 6)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
.addComponent(jLabel5)
.addGap(18, 18, 18))
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSave)
.addComponent(btnImportXML)
.addComponent(btnExportXML))
.addGap(26, 26, 26)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(62, 62, 62))
);
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)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 238, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void txtLanguageActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String fullname = txtFullName.getText();
String rollno = txtRollNo.getText();
String hobby = txtHobby.getText();
String gender = txtGender.getSelectedItem().toString();
String language = txtLanguage.getText();
if (rollno.isEmpty() || fullname.isEmpty())
{
JOptionPane.showMessageDialog(rootPane, "Pls input");
}
for (Student student : studentlist)
{
if (student.getRollno().equalsIgnoreCase(rollno))
{
JOptionPane.showMessageDialog(rootPane, "Already has rollno");
}
}
Student std = new Student(fullname , rollno , gender , language , hobby);
studentlist.add(std);
tablemodel.addRow(new Object[] {fullname , rollno , hobby ,gender ,language});
}
private void btnExportXMLActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
StringBuilder builder = new StringBuilder();
for (Student student : studentlist) {
builder.append(student.getStringXML());
}
String body = builder.toString();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<studentList>\n" +
body+
"\n</studentList>";
//save into file student.xml
FileOutputStream fos = null;
try {
fos = new FileOutputStream("student.xml");
byte[] data = xml.getBytes();
fos.write(data);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(StudentFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
JOptionPane.showMessageDialog(rootPane, "Save success!!!");
}
private void btnImportXMLActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
FileInputStream fis = null;
try {
fis = new FileInputStream("student.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
StudentHandler handler = new StudentHandler();
try {
parser.parse(fis, handler);
} catch (IOException ex) {
Logger.getLogger(StudentFrame.class.getName()).log(Level.SEVERE, null, ex);
}
studentlist = handler.getStudentlist();
tablemodel.setRowCount(0);
for(Student student : studentlist)
{
tablemodel.addRow(new Object[]{
student.getFullname() , student.getRollno() , student.getHobby() , student.getGender() , student.getLanguage()
});
}
} catch (FileNotFoundException ex) {
Logger.getLogger(StudentFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(StudentFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(StudentFrame.class.getName()).log(Level.SEVERE, null, ex);
} finally
{
if(fis != null)
{
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(StudentFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
JOptionPane.showMessageDialog(rootPane, "Import thanh cong");
}
/**
* @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(StudentFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(StudentFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(StudentFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(StudentFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new StudentFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnExportXML;
private javax.swing.JButton btnImportXML;
private javax.swing.JButton btnSave;
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.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblStudent;
private javax.swing.JTextField txtFullName;
private javax.swing.JComboBox<String> txtGender;
private javax.swing.JTextField txtHobby;
private javax.swing.JTextField txtLanguage;
private javax.swing.JTextField txtRollNo;
// End of variables declaration
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaimportexportxml02;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author abc
*/
public class Student {
String fullname , rollno ,language ,gender , hobby ;
public Student(String fullname, String rollno, String language, String gender, String hobby) {
this.fullname = fullname;
this.rollno = rollno;
this.language = language;
this.gender = gender;
this.hobby = hobby;
}
public Student()
{
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Student{" + "fullname=" + fullname + ", rollno=" + rollno + ", language=" + language + ", gender=" + gender + ", hobby=" + hobby + '}';
}
public String getStringXML()
{
return "<student>\n" +
" <rollNo>"+rollno+"</rollNo>\n" +
" <fullname>"+fullname+"</fullname>\n" +
" <hobby>"+hobby+"</hobby>\n" +
" <gender>"+gender+"</gender>\n" +
"<language>" + language + "</language>" +
" </student>";
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaimportexportxml02;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author abc
*/
public class StudentHandler extends DefaultHandler {
List<Student> studentlist = new ArrayList<>();
Student currentStudent = null;
boolean isFullname = true , isRollNo = true , isGender = true , isHobby = true , isLanguage = true;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase("student"))
{
currentStudent = new Student();
}
else if (qName.equalsIgnoreCase("fullname"))
{
isFullname = true ;
}
else if (qName.equalsIgnoreCase("rollno"))
{
isRollNo = true ;
}
else if (qName.equalsIgnoreCase("hobby"))
{
isHobby = true ;
}
else if (qName.equalsIgnoreCase("gender"))
{
isGender = true ;
}
else if (qName.equalsIgnoreCase("language"))
{
isLanguage = true ;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equalsIgnoreCase("student"))
{
studentlist.add(currentStudent);
currentStudent = null;
}
else if (qName.equalsIgnoreCase("fullname"))
{
isFullname = false;
}
else if (qName.equalsIgnoreCase("rollno"))
{
isRollNo = false ;
}
else if (qName.equalsIgnoreCase("hobby"))
{
isHobby = false ;
}
else if (qName.equalsIgnoreCase("gender"))
{
isGender = false ;
}
else if (qName.equalsIgnoreCase("language"))
{
isLanguage = false ;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if (isFullname)
{
currentStudent.setFullname(value);
}
if (isRollNo)
{
currentStudent.setRollno(value);
}
if (isHobby)
{
currentStudent.setHobby(value);
}
if(isGender)
{
currentStudent.setGender(value);
}
if(isLanguage)
{
currentStudent.setLanguage(value);
}
}
public StudentHandler() {
}
public List<Student> getStudentlist() {
return studentlist;
}
}
|
Đường Thanh Bình [T1907A]
2020-04-20 04:31:38
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Profile;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author Administrator
*/
public class InformationJFrame extends javax.swing.JFrame {
DefaultTableModel tableModel;
List<Information> informationList = new ArrayList<>();
/**
* Creates new form ProfileJFrame
*/
public InformationJFrame() {
initComponents();
tableModel = (DefaultTableModel) tblInformation.getModel();
showInnformation();
}
private void showInformation(){
informationList = informationModify.fillAll();
tableModel.setRowCount(0);
List.forEach((information) -> {
tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
information.getFullname(), information.getAge(), information.getGender(),
information.getAddress(),information.getHobby(), information.getLeaningtools(), information.getProlanguage()
});
});
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtFullName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
tfAge = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
txtAddress = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
tfPhoneNumber = new javax.swing.JTextField();
txtEmail = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
btnImportXML = new javax.swing.JButton();
btnExportXML = new javax.swing.JButton();
btnSave = new javax.swing.JButton();
jLabel7 = new javax.swing.JLabel();
txtHobbies = new javax.swing.JTextField();
jLabel8 = new javax.swing.JLabel();
txtTool = new javax.swing.JTextField();
jLabel9 = new javax.swing.JLabel();
txtProgrammingLanguage = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
tblInformation = new javax.swing.JTable();
btnAdd = new javax.swing.JButton();
btnEdit = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Profile manager"));
jLabel1.setText("FullName");
txtFullName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtFullNameActionPerformed(evt);
}
});
jLabel2.setText("Age");
jLabel4.setText("PhoneNumber");
jLabel5.setText("Email");
jLabel6.setText("Address");
btnImportXML.setText("Import XML");
btnImportXML.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportXMLActionPerformed(evt);
}
});
btnExportXML.setText("Export XML");
btnExportXML.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExportXMLActionPerformed(evt);
}
});
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
jLabel7.setText("Hobbies");
jLabel8.setText("Tool");
jLabel9.setText("ProgrammingLanguage");
txtProgrammingLanguage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtProgrammingLanguageActionPerformed(evt);
}
});
tblInformation.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"FullName", "Age", "Address", "PhoneNumber", "Email", "Hobbies", "Tool", "ProgrammingLanguage"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(tblInformation);
btnAdd.setText("Add");
btnEdit.setText("Edit");
btnEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
btnDelete.setText("Delete");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(48, 48, 48)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel6)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGap(39, 39, 39)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtTool, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtProgrammingLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfAge, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfPhoneNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtHobbies, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(110, 110, 110)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnEdit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnImportXML, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnExportXML, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(0, 0, Short.MAX_VALUE))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
.addContainerGap(525, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSave))
.addGap(22, 22, 22)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(tfAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnImportXML))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnExportXML))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel6)))
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(tfPhoneNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAdd))
.addGap(32, 32, 32)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(btnEdit))
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(txtHobbies, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnDelete))
.addGap(43, 43, 43)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(txtTool, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtProgrammingLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(68, 68, 68)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(141, Short.MAX_VALUE))
);
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()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(68, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
Component jLabel1 = new javax.swing.JLabel();
Component txtFullName = new javax.swing.JTextField();
Component jLabel2 = new javax.swing.JLabel();
Component jLabel3 = new javax.swing.JLabel();
Component jLabel4 = new javax.swing.JLabel();
Component jLabel5 = new javax.swing.JLabel();
Component jLabel6 = new javax.swing.JLabel();
Component tfAge = new javax.swing.JTextField();
Component txtHobbies = new javax.swing.JTextField();
Component txtAddress = new javax.swing.JTextField();
Component txtTool = new javax.swing.JTextField();
Component txtProgrammingLanguage = new javax.swing.JTextField();
Component btnSave = new javax.swing.JButton();
Component btnAdd = new javax.swing.JButton();
Component btnDelete = new javax.swing.JButton();
Component btnEdit = new javax.swing.JButton();
Component btnImportXML = new javax.swing.JButton();
Component btnExportXML = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblInformation = new javax.swing.JTable();
Component jLabel7 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 255, 204));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Personal Information"));
jLabel1.setText("Full Name :");
jLabel2.setText("Age :");
jLabel3.setText("Address :");
jLabel4.setText("Hobby :");
jLabel5.setText("Leaning Tools :");
jLabel6.setText("Programming Language :");
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnAdd.setText("Update");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
private void btnAddActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
private void btnDeleteActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
btnDelete.setText("Reset");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
private void btnDeleteActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
btnImportXML.setText("InportXML");
btnImportXML.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportXMLActionPerformed(evt);
}
});
btnExportXML.setText("ExportXML");
btnExportXML.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExportXMLActionPerformed(evt);
}
});
tblInformation.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"No", "FullName ", "Age", "Gender", "Address", "Hobby", "LeaningTolls", "ProLanguage"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
tblInformation.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tblInformationMouseClicked(evt);
}
private void tblInformationMouseClicked(MouseEvent evt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
jScrollPane1.setViewportView(tblInformation);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 696, Short.MAX_VALUE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel7))
.addGap(30, 30, 30)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtFullName, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtProgrammingLanguage, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtTool, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtHobbies, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(tfAge, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnExportXML, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnImportXML, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnEdit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(39, 39, 39))))
};
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSave))
.addGap(25, 25, 25)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnUpdate))
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnDelete)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(cbGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnReset)
.addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnInportXML)
.addComponent(txtHobby, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnExportXML)
.addComponent(txtLeaningTools, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(txtProLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(38, 38, 38)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
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()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtFullNameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String FullName = txtFullName.getText();
String Age = tfAge.getText();
String Address = txtAddress.getText();
String Email = txtEmail.getText();
String PhoneNumber = tfPhoneNumber.getText();
String Hobbies = txtHobbies.getText();
String Tool = txtTool.getText();
String ProgrammingLanguage = txtProgrammingLanguage.getText();
if(RollNo,isEmpty() || FullName.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "RollNo is empty || FullName is empty?");
return;
}
Object RollNo = null;
for(Information information : informationList){
if(information.getRollNo().equalsIgnoreCase(RollNo)){
JOptionPane.showMessageDialog(rootPane, "RollNo existed!!!");
return;
}
}
Information infor = new Information(RollNo, FullName, Age, Address, Email, PhoneNumber, Hobbies, Tool, ProgrammingLanguage);
informationList.add(infor);
tableModel.addRow(new Object[] {RollNo, FullName, Age, Address, Email, PhoneNumber, Hobbies, Tool, ProgrammingLanguage});
}
private void txtProgrammingLanguageActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnImportXMLActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
FileInputStream fis = null;
try {
// TODO add your handling code here:
fis = new FileInputStream("student.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
InformationHandler handler = new InformationHandler();
parser.parse(fis, handler);
informationList = handler.getInformationList();
tableModel.setRowCount(0);
for (Information information : informationList) {
tableModel.addRow(new Object[] {
information.getRollNo(), information.getFullname(),
information.getAge(), information.getAddress()
});
}
} catch (FileNotFoundException ex) {
Logger.getLogger(InformationJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(InformationJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(InformationJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(InformationJFrame.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(InformationJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
JOptionPane.showMessageDialog(rootPane, "Import success!!!");
}
private void btnExportXMLActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
StringBuilder builder = new StringBuilder();
for (Information information : informationList) {
builder.append(information.getXMLString());
}
String body = builder.toString();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<information>\n" +
body+
"\n</information>";
//save into file student.xml
FileOutputStream fos = null;
try {
fos = new FileOutputStream("student.xml");
byte[] data = xml.getBytes();
fos.write(data);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(InformationJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
JOptionPane.showMessageDialog(rootPane, "Save success!!!");
}
/**
* @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(InformationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(InformationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(InformationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(InformationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
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(InformationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(InformationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(InformationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(InformationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InformationJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnEdit;
private javax.swing.JButton btnExportXML;
private javax.swing.JButton btnImportXML;
private javax.swing.JButton btnSave;
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.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblInformation;
private javax.swing.JTextField tfAge;
private javax.swing.JTextField tfPhoneNumber;
private javax.swing.JTextField txtAddress;
private javax.swing.JTextField txtEmail;
private javax.swing.JTextField txtFullName;
private javax.swing.JTextField txtHobbies;
private javax.swing.JTextField txtProgrammingLanguage;
private javax.swing.JTextField txtTool;
// End of variables declaration
private boolean isEmpty() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private void showInnformation() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Profile;
/**
*
* @author Administrator
*/
public class Information {
String RollNo,FullName,Address,Email,Hobbies,Tool,ProgrammingLanguage;
int Age,PhoneNumber;
public Information() {
}
public Information(String RollNo, String FullName, String Address, String Email, String Hobbies, String Tool, String ProgrammingLanguage, int Age, int PhoneNumber) {
this.RollNo = RollNo;
this.FullName = FullName;
this.Address = Address;
this.Email = Email;
this.Hobbies = Hobbies;
this.Tool = Tool;
this.ProgrammingLanguage = ProgrammingLanguage;
this.Age = Age;
this.PhoneNumber = PhoneNumber;
}
Information(String FullName, int parseInt, String Address, String Hobbies, String Email, String ProgrammingLanguage, int parseInt0, String Tool) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Information(Object RollNo, String FullName, String Age, String Address, String Email, String PhoneNumber, String Hobbies, String Tool, String ProgrammingLanguage) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public String getXMLString(){
return "<Information>\n"
+"<RollNo>" + RollNo + "</RollNO>\n"
+"<FullName>" + FullName + "</FullName>\n"
+"<Age>" + Age + "</Age>\n"
+"<Address>" + Address + "</Address>\n"
+"<Email>" + Email + "</Email>\n"
+"<PhoneNumber>" + PhoneNumber + "</PhoneNumber>\n"
+"<Hobbies>" + Hobbies + "</Hobbies>\n"
+"<Tool>" + Tool + "</Tool>\n"
+"<ProgrammingLanguage>" + ProgrammingLanguage + "</ProgrammingLanguage>\n"
+"</Information>";
}
void setFullName(String value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setAge(int start) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setPhoneNumber(int length) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setAddress(String value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setEmail(String value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setHobbies(String value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setTool(String value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setProgrammingLanguage(String value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setRollNo(String value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Object getRollNo() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Object getFullname() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Object getAge() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Object getAddress() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Profile;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author Administrator
*/
public class InformationHandler extends DefaultHandler {
List<Information> informaionList = new ArrayList<>();
Information currentInformation = null;
boolean isRollNo = false;
boolean isFullName = false;
boolean isAge = false;
boolean isAddress = false;
boolean isPhoneNumber = false;
boolean isEmail = false;
boolean isHobbies = false;
boolean isTool = false;
boolean isProgrammingLanguage = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase("information")){
currentInformation = new Information();
}else if(qName.equalsIgnoreCase("FullName")){
isFullName = true;
}else if(qName.equalsIgnoreCase("Age")){
isAge = true;
}else if(qName.equalsIgnoreCase("PhoneNumber")){
isPhoneNumber = true;
}else if(qName.equalsIgnoreCase("Email")){
isEmail = true;
}else if(qName.equalsIgnoreCase("Hobbies")){
isHobbies = true;
}else if(qName.equalsIgnoreCase("Tool")){
isTool = true;
}else if(qName.equalsIgnoreCase("ProgrammingLanguage")){
isProgrammingLanguage = true;
}else if(qName.equalsIgnoreCase("Address")){
isAddress = true;
}else if(qName.equalsIgnoreCase("RollNo")){
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equalsIgnoreCase("information")){
informationList.add(currentInformation);
currentInformation = null;
}else if(qName.equalsIgnoreCase("FullName")){
isFullName = false;
}else if(qName.equalsIgnoreCase("Age")){
isAge = false;
}else if(qName.equalsIgnoreCase("PhoneNumber")){
isPhoneNumber = false;
}else if(qName.equalsIgnoreCase("Email")){
isEmail = false;
}else if(qName.equalsIgnoreCase("Hobbies")){
isHobbies = false;
}else if(qName.equalsIgnoreCase("Tool")){
isTool = false;
}else if(qName.equalsIgnoreCase("ProgrammingLanguage")){
isProgrammingLanguage = false;
}else if(qName.equalsIgnoreCase("Address")){
isAddress = false;
}else if(qName.equalsIgnoreCase("RollNo")){
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if (isRollNo){
currentInformation.setRollNo(value);
}else if (isAge){
currentInformation.setAge(start);
}else if(isPhoneNumber){
currentInformation.setPhoneNumber(length);
}else if(isAddress){
currentInformation.setAddress(value);
}else if(isEmail){
currentInformation.setEmail(value);
}else if(isHobbies){
currentInformation.setHobbies(value);
}else if(isTool){
currentInformation.setTool(value);
}else if(isProgrammingLanguage){
currentInformation.setProgrammingLanguage(value);
}else if(isFullName){
currentInformation.setFullName(value);
}
}
public List<Information>getInformation(){
List<Information> InformationList = null;
return InformationList;
}
List<Information> getInformationList() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Profile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class InformationModify {
public static List<Information> fillAll(){
List<Information> informationList = new ArrayList<>();
Connection conn = null;
Statement statement = null;
try {
//lay tat ca danh sach
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
//query
String sql = "select * from personal_information";
statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
Information information = new Information(
resultSet.getInt("RollNo"),
resultSet.getString("fullname"),
resultSet.getString("gender"),
resultSet.getString("address"),
resultSet.getString("hobby"),
resultSet.getString("leaningtools"),
resultSet.getString("prolanguage"),
resultSet.getInt("age")
);
informationList.add(information);
}
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return informationList;
}
public static void save(Information infor){
Connection conn = null;
PreparedStatement statement = null;
try {
//lay tat ca danh sach
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
//query
String sql = "insert into personal_information(fullname, age, gender, address, hobby, leaningtools, prolanguage) value (?,?,?,?,?,?,?)";
statement = conn.prepareCall(sql);
statement.setString(1, (String) infor.getFullname());
statement.setInt(2, (int) infor.getAge());
statement.setString(4, (String) infor.getAddress());
statement.setString(5, infor.getHobbies());
statement.setString(6, infor.getTool());
statement.setString(7, infor.getProgramminglanguage());
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void update(Information infor){
Connection conn = null;
PreparedStatement statement = null;
try {
//lay tat ca danh sach
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
//query update
String sql = "update personal_information set fullname = ?, gender = ?, address = ?, hobby = ?, leaningtools= ?, prolanguage= ?, age= ? where rollno = ?";
statement = conn.prepareCall(sql);
statement.setInt(1, (int) infor.getRollNo());
statement.setString(2, (String) infor.getFullname());
statement.setInt(3, (int) infor.getAge());
statement.setString(5, (String) infor.getAddress());
statement.setString(6, infor.getHobbies());
statement.setString(7, infor.getTool());
statement.setString(8, infor.getProgramminglanguage());
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void delete(int rollno){
Connection conn = null;
PreparedStatement statement = null;
try {
//lay tat ca danh sach
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
//query delete
String sql = "delete from personal_information where rollno = ?";
statement = conn.prepareCall(sql);
statement.setInt(1, rollno);
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(InformationModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<infor>
<object>
<fullname>Duong Thanh Binh</fullname>
<age>18</age>
<address>Ha Noi</address>
<phonenumber>0522697743</phonenumber>
<email>binhdtth1907041@fpt.edu.vn</email>
<hobbies>
<hobbieList>
<hobby>Play game</hobby>
<hobby>Movie</hobby>
</hobbieList>
</hobbies>
<height>175cm</height>
<weight>48kg</weight>
<status>Alone</status>
<tools>
<toolList>
<tool>Laptop</toolList>
</toolList>
<programmingLanguage>
<languageList>
<language>Java</language>
<language>HTML</language>
<language>CSS,Bootstrap</language>
<language>XML</language>
</languageList>
</programmingLanguage>
</object>
</infor>
CREATE TABLE information(
RollNo int PRIMARY KEY AUTO_INCREMENT,
FullName varchar(50) not null,
Age int (11),
Address varchar(100),
Hobbies varchar(50),
Tool varchar(50),
ProgrammingLanguage varchar(100)
)
|
Minh Nghia [T1907A]
2020-04-19 18:17:22
|
<?xml version="1.0" encoding="UTF-8"?>
<profileList>
<profile>
<rollno>3</rollno>
<fullname>Nghia</fullname>
<age>34</age>
<address>Hn</address>
<hobby>game</hobby>
<leaningtools>PC</leaningtools>
<prolanguage>PHP</prolanguage>
</profile><profile>
<rollno>13</rollno>
<fullname>Hong</fullname>
<age>19</age>
<address>HD</address>
<hobby>Gym</hobby>
<leaningtools>Phone</leaningtools>
<prolanguage>Fb</prolanguage>
</profile><profile>
<rollno>15</rollno>
<fullname>Nam</fullname>
<age>12</age>
<address>hn</address>
<hobby>scoocer</hobby>
<leaningtools>pc</leaningtools>
<prolanguage>cute</prolanguage>
</profile>
</profileList>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Personal_Information;
/**
*
* @author Administrator
*/
public class Profile {
int rollno;
String fullname, gender, address, hobby, leaningtools, prolanguage;
int age;
public Profile() {
}
public Profile(String fullname, String gender, String address, String hobby, String leaningtools, String prolanguage, int age) {
this.fullname = fullname;
this.gender = gender;
this.address = address;
this.hobby = hobby;
this.leaningtools = leaningtools;
this.prolanguage = prolanguage;
this.age = age;
}
public Profile(int rollno, String fullname, String gender, String address, String hobby, String leaningtools, String prolanguage, int age) {
this.rollno = rollno;
this.fullname = fullname;
this.gender = gender;
this.address = address;
this.hobby = hobby;
this.leaningtools = leaningtools;
this.prolanguage = prolanguage;
this.age = age;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getLeaningtools() {
return leaningtools;
}
public void setLeaningtools(String leaningtools) {
this.leaningtools = leaningtools;
}
public String getProlanguage() {
return prolanguage;
}
public void setProlanguage(String prolanguage) {
this.prolanguage = prolanguage;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Profile{" + "rollno=" + rollno + ", fullname=" + fullname + ", gender=" + gender + ", address=" + address + ", hobby=" + hobby + ", leaningtools=" + leaningtools + ", prolanguage=" + prolanguage + ", age=" + age + '}';
}
public String getXMLString() {
return "<profile>\n"
+ "<rollno>" + rollno + "</rollno>\n"
+ "<fullname>" + fullname + "</fullname>\n"
+ "<age>" + age + "</age>\n"
+ "<address>" + address + "</address>\n"
+ "<hobby>" + hobby + "</hobby>\n"
+ "<leaningtools>" + leaningtools + "</leaningtools>\n"
+ "<prolanguage>" + prolanguage + "</prolanguage>\n"
+ "</profile>";
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Personal_Information;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author Administrator
*/
public class ProfileFrame extends javax.swing.JFrame {
DefaultTableModel tableModel;
List<Profile> profileList = new ArrayList<>();
/**
* Creates new form ProfileFrame
*/
public ProfileFrame() {
initComponents();
tableModel = (DefaultTableModel) tblProfile.getModel();
showProfile();
}
private void showProfile(){
profileList = ProfileModify.fillAll();
tableModel.setRowCount(0);
profileList.forEach((profile) -> {
tableModel.addRow(new Object[]{tableModel.getRowCount() + 1,
profile.getFullname(), profile.getAge(), profile.getGender(),
profile.getAddress(),profile.getHobby(), profile.getLeaningtools(), profile.getProlanguage()
});
});
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtFullName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
txtAge = new javax.swing.JTextField();
txtHobby = new javax.swing.JTextField();
txtAddress = new javax.swing.JTextField();
txtLeaningTools = new javax.swing.JTextField();
txtProLanguage = new javax.swing.JTextField();
btnSave = new javax.swing.JButton();
btnUpdate = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
btnReset = new javax.swing.JButton();
btnInportXML = new javax.swing.JButton();
btnExportXML = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblProfile = new javax.swing.JTable();
jLabel7 = new javax.swing.JLabel();
cbGender = new javax.swing.JComboBox<>();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 255, 204));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Personal Information"));
jLabel1.setText("Full Name :");
jLabel2.setText("Age :");
jLabel3.setText("Address :");
jLabel4.setText("Hobby :");
jLabel5.setText("Leaning Tools :");
jLabel6.setText("Programming Language :");
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnUpdate.setText("Update");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
btnReset.setText("Reset");
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnResetActionPerformed(evt);
}
});
btnInportXML.setText("InportXML");
btnInportXML.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnInportXMLActionPerformed(evt);
}
});
btnExportXML.setText("ExportXML");
btnExportXML.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExportXMLActionPerformed(evt);
}
});
tblProfile.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"No", "FullName ", "Age", "Gender", "Address", "Hobby", "LeaningTolls", "ProLanguage"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
tblProfile.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tblProfileMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tblProfile);
jLabel7.setText("Gender :");
cbGender.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Male", "Famale" }));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 696, Short.MAX_VALUE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel7))
.addGap(30, 30, 30)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtFullName, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtProLanguage, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtLeaningTools, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtHobby, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtAge, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE))
.addComponent(cbGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnExportXML, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnInportXML, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(39, 39, 39))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSave))
.addGap(25, 25, 25)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnUpdate))
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnDelete)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(cbGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnReset)
.addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnInportXML)
.addComponent(txtHobby, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnExportXML)
.addComponent(txtLeaningTools, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(txtProLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(38, 38, 38)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
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()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String fullname = txtFullName.getText();
int age = Integer.parseInt(txtAge.getText());
String gender = cbGender.getSelectedItem().toString();
String address = txtAddress.getText();
String hobby = txtHobby.getText();
String leaningtools = txtLeaningTools.getText();
String prolanguage = txtProLanguage.getText();
Profile pro = new Profile(fullname, gender, address, hobby, leaningtools, prolanguage, age);
ProfileModify.save(pro);
showProfile();
}
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
int selectIndex = tblProfile.getSelectedRow();
if(selectIndex >= 0){
Profile pro = profileList.get(selectIndex);
int option = JOptionPane.showConfirmDialog(rootPane, "Do you want to delete this iteam ?");
System.out.println("option" + option);
if(option == 0){
ProfileModify.delete(pro.getRollno());
showProfile();
}
}
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtFullName.setText("");
txtAge.setText("");
cbGender.setSelectedItem("");
txtAddress.setText("");
txtHobby.setText("");
txtLeaningTools.setText("");
txtProLanguage.setText("");
}
private void btnInportXMLActionPerformed(java.awt.event.ActionEvent evt) {
FileInputStream fis = null;
try {
// TODO add your handling code here:
fis = new FileInputStream("personal_information.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
ProfileHandler hander = new ProfileHandler();
parser.parse(fis, hander);
profileList = hander.getProfileList();
tableModel.setRowCount(0);
for (Profile profile : profileList) {
tableModel.addRow(new Object[] {
profile.getFullname(), profile.getAge(),
profile.getGender(), profile.getAddress(),
profile.getHobby(),profile.getLeaningtools(),
profile.getProlanguage()
});
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ProfileFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(ProfileFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(ProfileFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ProfileFrame.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(rootPane, "Inport success!!!");
}
private void btnExportXMLActionPerformed(java.awt.event.ActionEvent evt) {
StringBuilder builder = new StringBuilder();
for (Profile profile : profileList) {
builder.append(profile.getXMLString());
}
String body = builder.toString();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<profileList>\n" +
body+
"\n</profileList>";
FileOutputStream fos = null;
try {
fos = new FileOutputStream("personal_information.xml");
byte[] data = xml.getBytes();
fos.write(data);
} catch (FileNotFoundException ex) {
Logger.getLogger(ProfileFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ProfileFrame.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(ProfileFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
JOptionPane.showMessageDialog(rootPane, "Export success!!!");
}
private void tblProfileMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txtFullName.setText(tblProfile.getValueAt(tblProfile.getEditingRow(), 0).toString());
txtAge.setText(tblProfile.getValueAt(tblProfile.getEditingRow(), 1).toString());
cbGender.setSelectedItem(tblProfile.getValueAt(tblProfile.getEditingRow(), 2).toString());
txtAddress.setText(tblProfile.getValueAt(tblProfile.getEditingRow(), 3).toString());
txtHobby.setText(tblProfile.getValueAt(tblProfile.getEditingRow(), 4).toString());
txtLeaningTools.setText(tblProfile.getValueAt(tblProfile.getEditingRow(), 5).toString());
txtProLanguage.setText(tblProfile.getValueAt(tblProfile.getEditingRow(), 6).toString());
}
/**
* @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(ProfileFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProfileFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProfileFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProfileFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProfileFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnExportXML;
private javax.swing.JButton btnInportXML;
private javax.swing.JButton btnReset;
private javax.swing.JButton btnSave;
private javax.swing.JButton btnUpdate;
private javax.swing.JComboBox<String> cbGender;
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.JScrollPane jScrollPane1;
private javax.swing.JTable tblProfile;
private javax.swing.JTextField txtAddress;
private javax.swing.JTextField txtAge;
private javax.swing.JTextField txtFullName;
private javax.swing.JTextField txtHobby;
private javax.swing.JTextField txtLeaningTools;
private javax.swing.JTextField txtProLanguage;
// End of variables declaration
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Personal_Information;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class ProfileModify {
public static List<Profile> fillAll(){
List<Profile> profileList = new ArrayList<>();
Connection conn = null;
Statement statement = null;
try {
//lay tat ca danh sach
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
//query
String sql = "select * from personal_information";
statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
Profile profile = new Profile(
resultSet.getInt("rollno"),
resultSet.getString("fullname"),
resultSet.getString("gender"),
resultSet.getString("address"),
resultSet.getString("hobby"),
resultSet.getString("leaningtools"),
resultSet.getString("prolanguage"),
resultSet.getInt("age")
);
profileList.add(profile);
}
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return profileList;
}
public static void save(Profile pro){
Connection conn = null;
PreparedStatement statement = null;
try {
//lay tat ca danh sach
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
//query
String sql = "insert into personal_information(fullname, age, gender, address, hobby, leaningtools, prolanguage) value (?,?,?,?,?,?,?)";
statement = conn.prepareCall(sql);
statement.setString(1, pro.getFullname());
statement.setInt(2, pro.getAge());
statement.setString(3, pro.getGender());
statement.setString(4, pro.getAddress());
statement.setString(5, pro.getHobby());
statement.setString(6, pro.getLeaningtools());
statement.setString(7, pro.getProlanguage());
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void update(Profile pro){
Connection conn = null;
PreparedStatement statement = null;
try {
//lay tat ca danh sach
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
//query update
String sql = "update personal_information set fullname = ?, gender = ?, address = ?, hobby = ?, leaningtools= ?, prolanguage= ?, age= ? where rollno = ?";
statement = conn.prepareCall(sql);
statement.setInt(1, pro.getRollno());
statement.setString(2, pro.getFullname());
statement.setInt(3, pro.getAge());
statement.setString(4, pro.getGender());
statement.setString(5, pro.getAddress());
statement.setString(6, pro.getHobby());
statement.setString(7, pro.getLeaningtools());
statement.setString(8, pro.getProlanguage());
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void delete(int rollno){
Connection conn = null;
PreparedStatement statement = null;
try {
//lay tat ca danh sach
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/T1907A?serverTimezone=UTC", "root", "");
//query delete
String sql = "delete from personal_information where rollno = ?";
statement = conn.prepareCall(sql);
statement.setInt(1, rollno);
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(ProfileModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Personal_Information;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author Administrator
*/
public class ProfileHandler extends DefaultHandler{
List<Profile> profileList = new ArrayList<>();
Profile currentProfile = null;
boolean isFullname = false,
isAge = false,
isGender = false,
isAddress = false,
isHobby = false,
isLeaningtools = false,
isProlanguage = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase("profile")){
currentProfile = new Profile();
}else if(qName.equalsIgnoreCase("fullname")){
isFullname = true;
}else if(qName.equalsIgnoreCase("age")){
isAge = true;
}else if(qName.equalsIgnoreCase("gender")){
isGender = true;
}else if(qName.equalsIgnoreCase("address")){
isAddress = true;
}else if(qName.equalsIgnoreCase("hobby")){
isHobby = true;
}else if(qName.equalsIgnoreCase("leaningtools")){
isLeaningtools = true;
}else if(qName.equalsIgnoreCase("prolanguage")){
isProlanguage = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equalsIgnoreCase("profile")){
profileList.add(currentProfile);
currentProfile = null;
}else if(qName.equalsIgnoreCase("fullname")){
isFullname = false;
}else if(qName.equalsIgnoreCase("age")){
isAge = false;
}else if(qName.equalsIgnoreCase("gender")){
isGender = false;
}else if(qName.equalsIgnoreCase("address")){
isAddress = false;
}else if(qName.equalsIgnoreCase("hobby")){
isHobby = false;
}else if(qName.equalsIgnoreCase("leaningtools")){
isLeaningtools = false;
}else if(qName.equalsIgnoreCase("prolanguage")){
isProlanguage = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if(isFullname){
currentProfile.setFullname(value);
}else if(isAge){
currentProfile.setAge(Integer.parseInt(value));
}else if(isGender){
currentProfile.setGender(value);
}else if(isAddress){
currentProfile.setAddress(value);
}else if(isHobby){
currentProfile.setHobby(value);
}else if(isLeaningtools){
currentProfile.setLeaningtools(value);
}else if(isProlanguage){
currentProfile.setProlanguage(value);
}
}
public List<Profile> getProfileList(){
return profileList;
}
}
CREATE TABLE personal_information(
rollno int PRIMARY KEY AUTO_INCREMENT,
fullname varchar(50) not null,
age int (11),
gender varchar(20),
address varchar(100),
hobby varchar(50),
leaningtools varchar(50),
prolanguage varchar(100)
)
|
thienphu [T1907A]
2020-04-18 13:01:20
|
CREATE TABLE information(
rollNo VARCHAR(20) PRIMARY KEY NOT NULL,
fullname VARCHAR(50) NOT NULL,
age INT NOT NULL,
address VARCHAR(100) NOT NULL,
enjoy VARCHAR(100) NOT NULL,
programmingLanguage VARCHAR(100) NOT NULL )
<?xml version="1.0" encoding="UTF-8"?>
<information>
<Person>
<rollNo>pp01</rollNo>
<fullname>Do Thien Phu</fullname>
<age>18</age>
<address>Ha Noi</address>
<enjoy>play soccer </enjoy>
<programmingLanguage>Java</programmingLanguage>
</Person>
<Person>
<rollNo>pp02</rollNo>
<fullname>Nguyen Hong Ngoc</fullname>
<age>21</age>
<address>Ha Noi</address>
<enjoy>readbook </enjoy>
<programmingLanguage>Java</programmingLanguage>
</Person>
</information>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package xml.test04;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Thien Phu
*/
public class Information {
String rollNo, fullname, address;
int age;
String enjoy, programmingLanguage;
public Information() {
}
public Information(String rollNo, String fullname, int age, String address, String enjoy, String programmingLanguage) {
this.rollNo = rollNo;
this.fullname = fullname;
this.age = age;
this.address = address;
this.enjoy = enjoy;
this.programmingLanguage = programmingLanguage;
}
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEnjoy() {
return enjoy;
}
public void setEnjoy(String enjoy) {
this.enjoy = enjoy;
}
public String getProgrammingLanguage() {
return programmingLanguage;
}
public void setProgrammingLanguage(String programmingLanguage) {
this.programmingLanguage = programmingLanguage;
}
void display() {
System.out.println("\nInformation:");
System.out.println("rollNo = " + rollNo + " : fullname= " + fullname + " :Age= " + age + " :Address= " + address + " :Enjoy = " + enjoy
+ " :programmingLanguage = " + programmingLanguage);
}
public String getString() {
String data = "<Person>\n"
+ " <rollNo>" + this.rollNo + "</rollNo>\n"
+ " <fullname>" + this.fullname + "</fullname>\n"
+ " <age>" + this.age + "</age>\n"
+ " <address>" + this.address + "</address>\n"
+ " <enjoy>" + this.enjoy + "</enjoy>\n"
+ " <programmingLanguage>" + this.programmingLanguage + "</programmingLanguage>\n" + "</Person>";
return data;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package xml.test04;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author Thien Phu
*/
public class InformationHander extends DefaultHandler {
List<Information> informationList = new ArrayList<>();
Information info = null;
boolean isPerson = false;
boolean isRollNo = false;
boolean isFullName = false;
boolean isAge = false;
boolean isAddress = false;
boolean isEnjoy = false;
boolean isprogrammingLanguage = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("Person")) { // kiem tra xem đọc đến thẻ nào rồi
info = new Information();
} else if (qName.equalsIgnoreCase("rollNo")) {
isRollNo = true;
} else if (qName.equalsIgnoreCase("fullname")) {
isFullName = true;
} else if (qName.equalsIgnoreCase("age")) {
isAge = true;
} else if (qName.equalsIgnoreCase("address")) {
isAddress = true;
} else if (qName.equalsIgnoreCase("enjoy")) {
isEnjoy = true;
} else if (qName.equalsIgnoreCase("programmingLanguage")) {
isprogrammingLanguage = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("Person")) {
informationList.add(info);
info = null;
} else if (qName.equalsIgnoreCase("rollNo")) {
isRollNo = false;
} else if (qName.equalsIgnoreCase("fullname")) {
isFullName = false;
} else if (qName.equalsIgnoreCase("age")) {
isAge = false;
} else if (qName.equalsIgnoreCase("address")) {
isAddress = false;
} else if (qName.equalsIgnoreCase("enjoy")) {
isEnjoy = false;
} else if (qName.equalsIgnoreCase("programmingLanguage")) {
isprogrammingLanguage = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if (isRollNo) {
info.setRollNo(value);
} else if (isFullName) {
info.setFullname(value);
} else if (isAge) {
info.setAge(Integer.parseInt(value));
} else if (isAddress) {
info.setAddress(value);
} else if (isEnjoy) {
info.setEnjoy(value);
} else if (isprogrammingLanguage) {
info.setProgrammingLanguage(value);
}
}
void display() {
for (Information informationList1 : informationList) {
informationList1.display();
}
}
public List<Information> getInformationList() {
return informationList;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package xml.test04;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author Thien Phu
*/
public class InformationManage {
public InformationManage() {
}
public void insert(Information infor) {
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
String sql = "insert into information(rollNo,fullname,age,address,enjoy,programmingLanguage)values (?,?,?,?,?,?)";
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, infor.getRollNo());
preparedStatement.setString(2, infor.getFullname());
preparedStatement.setInt(3, infor.getAge());
preparedStatement.setString(4, infor.getAddress());
preparedStatement.setString(5, infor.getEnjoy());
preparedStatement.setString(6, infor.getProgrammingLanguage());
preparedStatement.execute();
} catch (ClassNotFoundException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public List<Information> readDataMySql(List<Information> informationList) {
Connection conn = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
String sql = "select * from information";
statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
Information infor = new Information(resultSet.getString("rollNo"),
resultSet.getString("fullname"),
Integer.parseInt(resultSet.getString("age")),
resultSet.getString("address"),
resultSet.getString("enjoy"),
resultSet.getString("programmingLanguage"));
informationList.add(infor);
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return informationList;
}
public List<Information> ImportXML() {
List<Information> inforList = new ArrayList<>();
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\baitapjava\\BaitapXML\\src\\xml\\test04\\info.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
InformationHander hander = new InformationHander();
parser.parse(fis, hander);
inforList = hander.getInformationList();
//System.out.println("Chay ben nay");
} catch (FileNotFoundException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return inforList;
}
//Export ra file XML
public void ExportXML(List<Information> inforList) {
StringBuilder builder = new StringBuilder();
for (Information infor : inforList) {
builder.append(infor.getString());
}
String body = builder.toString();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<information>\n"
+ body
+ "\n</information>";
FileOutputStream fos = null;
try {
fos = new FileOutputStream("D:\\baitapjava\\Bientrongjava\\src\\xml\\test04\\export.xml");
byte[] data = xml.getBytes();
fos.write(data);
} catch (FileNotFoundException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void delete(String data) {
Connection conn = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
String sql = "DELETE FROM information WHERE rollNo =" + "'" + data + "'";
statement = conn.createStatement();
statement.executeUpdate(sql);
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(InformationManage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package xml.test04;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author Thien Phu
*/
public class InformationFrame extends javax.swing.JFrame {
/**
* Creates new form InformationFrame
*/
List<Information> informationList = new ArrayList<>();
DefaultTableModel tableModel;
InformationManage manage = new InformationManage();
static int index = -1;
public InformationFrame() {
initComponents();
tableModel = (DefaultTableModel) tbInformation.getModel();
readData();
this.setLocationRelativeTo(this);
}
/**
* 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() {
jButton3 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
tfRollno = new javax.swing.JTextField();
tfFullname = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
tfAge = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
tfAddress = new javax.swing.JTextField();
btSave = new javax.swing.JButton();
btImport = new javax.swing.JButton();
btExport = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tbInformation = new javax.swing.JTable();
Enjoy = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
tfEnjoy = new javax.swing.JTextField();
tfLanguage = new javax.swing.JTextField();
btUpdate = new javax.swing.JButton();
btDeleted = new javax.swing.JButton();
jButton3.setText("Deleted");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N
jLabel1.setText("Roll No");
jLabel2.setText("Full Name");
tfRollno.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfRollnoActionPerformed(evt);
}
});
tfFullname.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfFullnameActionPerformed(evt);
}
});
jLabel3.setText("Age");
jLabel4.setText("Address");
btSave.setText("Save");
btSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSaveActionPerformed(evt);
}
});
btImport.setText("Import XML");
btImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btImportActionPerformed(evt);
}
});
btExport.setText("Export XML");
btExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btExportActionPerformed(evt);
}
});
tbInformation.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Roll No", "Full Name", "Age", "Address", "Enjoy", "programmingLanguage"
}
));
tbInformation.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tbInformationMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tbInformation);
Enjoy.setText("Enjoy");
jLabel5.setText("ProgrammingLanguage");
btUpdate.setText("Update");
btUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btUpdateActionPerformed(evt);
}
});
btDeleted.setText("Deleted");
btDeleted.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btDeletedActionPerformed(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()
.addComponent(jScrollPane1)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
.addComponent(tfLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(Enjoy, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(tfEnjoy, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tfFullname, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfRollno, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfAge, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfAddress, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(79, 79, 79)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btImport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btExport, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btDeleted, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(24, 24, 24))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(13, 13, 13)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btSave, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addComponent(tfRollno, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfFullname, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btImport, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btExport, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfAge, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Enjoy, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfEnjoy, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btDeleted, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(40, 40, 40)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
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(20, 20, 20)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void tfFullnameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void tfRollnoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {
String rollno = tfRollno.getText();
String fullname = tfFullname.getText();
String age = tfAge.getText();
String address = tfAddress.getText();
String enjoy = tfEnjoy.getText();
String proLanguage = tfLanguage.getText();
Information infor = new Information(rollno, fullname, Integer.parseInt(age), address, enjoy, proLanguage);
manage.insert(infor);
readData();
reset();
// informationList.add(infor);
// tableModel.addRow(new Object[]{rollno, fullname, age, address, enjoy, proLanguage});
}
public void readData() {
informationList.clear();
tableModel.setRowCount(0);
manage.readDataMySql(informationList);
for (Information infor : informationList) {
tableModel.addRow(new Object[]{infor.getRollNo(), infor.getFullname(),
infor.getAge(), infor.getAddress(), infor.getEnjoy(), infor.getProgrammingLanguage()});
}
}
public void reset() {
tfRollno.setText("");
tfFullname.setText("");
tfAge.setText("");
tfAddress.setText("");
tfEnjoy.setText("");
tfLanguage.setText("");
}
private void btImportActionPerformed(java.awt.event.ActionEvent evt) {
//import du lieu tu xml vao
informationList.clear();
informationList = manage.ImportXML();
for (Information infor : informationList) {
manage.insert(infor);
}
readData();
JOptionPane.showMessageDialog(btImport, "Import success");
}
private void btExportActionPerformed(java.awt.event.ActionEvent evt) {
manage.ExportXML(informationList);
JOptionPane.showMessageDialog(btExport, "Export Success");
}
private void tbInformationMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
index = tbInformation.getSelectedRow();
System.out.println("index: " + index);
tfRollno.setText(informationList.get(index).getRollNo());
tfFullname.setText(informationList.get(index).getFullname());
tfAge.setText(String.valueOf(informationList.get(index).getAge()));
tfAddress.setText(informationList.get(index).getAddress());
tfEnjoy.setText(informationList.get(index).getEnjoy());
tfLanguage.setText(informationList.get(index).getProgrammingLanguage());
}
private void btDeletedActionPerformed(java.awt.event.ActionEvent evt) {
String roll = tfRollno.getText();
if (roll.isEmpty()) {
JOptionPane.showMessageDialog(btDeleted, "Chọn rollNo cần xóa");
} else {
manage.delete(roll);
JOptionPane.showMessageDialog(btDeleted, "Xoa thanh cong rollNo = " + roll);
reset();
readData();
}
}
private void btUpdateActionPerformed(java.awt.event.ActionEvent evt) {
String roll = informationList.get(index).getRollNo();
if (tfRollno.getText().equalsIgnoreCase(roll)) {
String fullname = tfFullname.getText();
int age = Integer.parseInt(tfAge.getText());
String address = tfAddress.getText();
String enjoy = tfEnjoy.getText();
String language = tfLanguage.getText();
//ket noi sql
Connection conn = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t1907a", "root", "");
String sql = "UPDATE information SET fullname=" + "'" + fullname + "',age="
+ "'" + age + "',address= '" + address + "',enjoy= '" + enjoy + "',programmingLanguage= '"
+ language + "' where rollNo= '" + tfRollno.getText() + "'";
System.out.println("sql: " + sql);
statement = conn.createStatement();
statement.executeUpdate(sql);
} catch (ClassNotFoundException ex) {
Logger.getLogger(InformationFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(InformationFrame.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(InformationFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(InformationFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//
readData();
} else {
JOptionPane.showMessageDialog(btUpdate, "No edit RollNo");
}
reset();
}
/**
* @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(InformationFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(InformationFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(InformationFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(InformationFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InformationFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Enjoy;
private javax.swing.JButton btDeleted;
private javax.swing.JButton btExport;
private javax.swing.JButton btImport;
private javax.swing.JButton btSave;
private javax.swing.JButton btUpdate;
private javax.swing.JButton jButton3;
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.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tbInformation;
private javax.swing.JTextField tfAddress;
private javax.swing.JTextField tfAge;
private javax.swing.JTextField tfEnjoy;
private javax.swing.JTextField tfFullname;
private javax.swing.JTextField tfLanguage;
private javax.swing.JTextField tfRollno;
// End of variables declaration
}
|
Nguyễn Văn Quang [T1907A]
2020-04-17 11:38:13
|
package ProfileJavaswing;
public class Profile {
String name, age, address, like, learningtools, programminglanguage;
public Profile() {
}
public Profile(String name, String age, String address, String like, String learningtools, String programminglanguage) {
this.name = name;
this.age = age;
this.address = address;
this.like = like;
this.learningtools = learningtools;
this.programminglanguage = programminglanguage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public String getLearningtools() {
return learningtools;
}
public void setLearningtools(String learningtools) {
this.learningtools = learningtools;
}
public String getProgramminglanguage() {
return programminglanguage;
}
public void setProgramminglanguage(String programminglanguage) {
this.programminglanguage = programminglanguage;
}
@Override
public String toString() {
return "Profile{" + "name=" + name + ", age=" + age + ", address=" + address + ", like=" + like + ", learningtools=" + learningtools + ", programminglanguage=" + programminglanguage + '}';
}
public String getXMLString() {
return "<myprofile>\n"
+ "<name>" + name + "</name>\n"
+ " <age>" + age + "</age>\n"
+ " <address>" + address + "</address>\n"
+ " <like>" + like + "</like>\n"
+ " <learningtools>" + learningtools + "</learningtools>\n"
+ " <programminglanguage>"+programminglanguage+"</programminglanguage>\n"
+ "</myprofile>";
}
}
package ProfileJavaswing;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ProfileHandler extends DefaultHandler{
List<Profile> profileList = new ArrayList<>();
Profile currentProfile = null;
boolean isName = false;
boolean isAge = false;
boolean isAddress = false;
boolean isLike = false;
boolean isLearningtools = false;
boolean isProgramminglanguage = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase("myprofile")){
profileList.add(currentProfile);
currentProfile = new Profile();
} else if (qName.equalsIgnoreCase("name")) {
isName = true;
} else if (qName.equalsIgnoreCase("age")) {
isAge = true;
} else if (qName.equalsIgnoreCase("address")) {
isAddress = true;
} else if (qName.equalsIgnoreCase("like")) {
isLike = true;
} else if (qName.equalsIgnoreCase("learningtools")) {
isLearningtools = true;
} else if (qName.equalsIgnoreCase("programminglanguage")) {
isProgramminglanguage = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("myprofile")) {
profileList.add(currentProfile);
currentProfile = null;
} else if (qName.equalsIgnoreCase("name")) {
isName = false;
} else if (qName.equalsIgnoreCase("age")) {
isAge = false;
} else if (qName.equalsIgnoreCase("address")) {
isAddress = false;
} else if (qName.equalsIgnoreCase("like")) {
isLike = false;
} else if (qName.equalsIgnoreCase("learningtools")) {
isLearningtools = false;
} else if (qName.equalsIgnoreCase("programminglanguage")) {
isProgramminglanguage = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if (isName) {
currentProfile.setName(value);
} else if (isAge) {
currentProfile.setAge(value);
} else if (isAddress) {
currentProfile.setAddress(value);
} else if (isLike) {
currentProfile.setLike(value);
} else if (isLearningtools) {
currentProfile.setLearningtools(value);
} else if (isProgramminglanguage) {
currentProfile.setProgramminglanguage(value);
}
}
public List<Profile> getProfileList() {
return profileList;
}
}
package ProfileJavaswing;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class ProfileJFrame extends javax.swing.JFrame {
DefaultTableModel tableModel;
List<Profile> profileList = new ArrayList<>();
public ProfileJFrame() {
initComponents();
tableModel = (DefaultTableModel) tblProfile.getModel();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
txtAge = new javax.swing.JTextField();
txtAddress = new javax.swing.JTextField();
txtLike = new javax.swing.JTextField();
txtLtools = new javax.swing.JTextField();
txtProlanguage = new javax.swing.JTextField();
btnSave = new javax.swing.JButton();
btnImport = new javax.swing.JButton();
btnExport = new javax.swing.JButton();
btnEdit = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
btnReset = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblProfile = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Profile Detail Information"));
jLabel1.setText("Name:");
jLabel2.setText("Age:");
jLabel3.setText("Address:");
jLabel4.setText("Like:");
jLabel5.setText("Learning Tools:");
jLabel6.setText("Programming Language:");
txtAge.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtAgeActionPerformed(evt);
}
});
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnImport.setText("Import XML");
btnImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportActionPerformed(evt);
}
});
btnExport.setText("Export XML");
btnExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExportActionPerformed(evt);
}
});
btnEdit.setText("Edit");
btnEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
btnReset.setText("Reset");
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnResetActionPerformed(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()
.addGap(23, 23, 23)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtLtools)
.addComponent(txtProlanguage)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(99, 99, 99)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtLike, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addGap(69, 69, 69)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnImport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnExport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnEdit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(24, 24, 24))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSave))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnEdit))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnDelete))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtLike, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnReset))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(txtLtools, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnImport))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(txtProlanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnExport))
.addContainerGap(27, Short.MAX_VALUE))
);
tblProfile.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Name", "Age", "Address", "Like", "Learning Tools", "Programming Language"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, true, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(tblProfile);
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, false)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void txtAgeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String name = txtName.getText();
String age = txtAge.getText();
String address = txtAddress.getText();
String like = txtLike.getText();
String learningtools = txtLtools.getText();
String programminglanguage = txtProlanguage.getText();
if(name.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Name is empty");
return;
}else if(age.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Age is empty");
}else if(address.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Address is empty");
}else if(like.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Like is empty");
}else if(learningtools.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Learning Tools is empty");
}else if(programminglanguage.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Programming Language is empty");
}
Profile pro = new Profile(name, age, address, like, learningtools, programminglanguage);
profileList.add(pro);
tableModel.addRow(new Object[] {name, age, address, like, learningtools, programminglanguage});
}
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtName.setText("");
txtAge.setText("");
txtAddress.setText("");
txtLike.setText("");
txtLtools.setText("");
txtProlanguage.setText("");
}
private void btnImportActionPerformed(java.awt.event.ActionEvent evt) {
FileInputStream fis = null;
try {
// TODO add your handling code here:
fis = new FileInputStream("ProfileXML.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
ProfileHandler handler = new ProfileHandler();
parser.parse(fis, handler);
profileList = handler.getProfileList();
tableModel.setRowCount(0);
for (Profile profile : profileList) {
tableModel.addRow(new Object[]{
profile.getName(), profile.getAge(),
profile.getAddress(), profile.getLike(),
profile.getLearningtools(), profile.getProgramminglanguage()
});
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
JOptionPane.showMessageDialog(rootPane, "Import Success");
}
private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
StringBuilder builder = new StringBuilder();
for (Profile profile : profileList) {
builder.append(profile.getXMLString());
}
String body = builder.toString();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<profile_list>\n" +
body+
"\n</profile_list>";
FileOutputStream fos = null;
try {
fos = new FileOutputStream("ProfileXML.xml");
byte[] data = xml.getBytes();
fos.write(data);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
JOptionPane.showMessageDialog(rootPane, "Export Success");
}
/**
* @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(ProfileJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProfileJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProfileJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProfileJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProfileJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnEdit;
private javax.swing.JButton btnExport;
private javax.swing.JButton btnImport;
private javax.swing.JButton btnReset;
private javax.swing.JButton btnSave;
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.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblProfile;
private javax.swing.JTextField txtAddress;
private javax.swing.JTextField txtAge;
private javax.swing.JTextField txtLike;
private javax.swing.JTextField txtLtools;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtProlanguage;
// End of variables declaration
}
|
Trần Mạnh Dũng [T1907A]
2020-04-17 11:19:32
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ProfileJavaswing;
/**
*
* @author admin
*/
public class Profile {
String name, age, address, like, learningtools, programminglanguage;
public Profile() {
}
public Profile(String name, String age, String address, String like, String learningtools, String programminglanguage) {
this.name = name;
this.age = age;
this.address = address;
this.like = like;
this.learningtools = learningtools;
this.programminglanguage = programminglanguage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public String getLearningtools() {
return learningtools;
}
public void setLearningtools(String learningtools) {
this.learningtools = learningtools;
}
public String getProgramminglanguage() {
return programminglanguage;
}
public void setProgramminglanguage(String programminglanguage) {
this.programminglanguage = programminglanguage;
}
@Override
public String toString() {
return "Profile{" + "name=" + name + ", age=" + age + ", address=" + address + ", like=" + like + ", learningtools=" + learningtools + ", programminglanguage=" + programminglanguage + '}';
}
public String getXMLString() {
return "<myprofile>\n"
+ "<name>" + name + "</name>\n"
+ " <age>" + age + "</age>\n"
+ " <address>" + address + "</address>\n"
+ " <like>" + like + "</like>\n"
+ " <learningtools>" + learningtools + "</learningtools>\n"
+ " <programminglanguage>"+programminglanguage+"</programminglanguage>\n"
+ "</myprofile>";
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ProfileJavaswing;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author admin
*/
public class ProfileHandler extends DefaultHandler{
List<Profile> profileList = new ArrayList<>();
Profile currentProfile = null;
boolean isName = false;
boolean isAge = false;
boolean isAddress = false;
boolean isLike = false;
boolean isLearningtools = false;
boolean isProgramminglanguage = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase("myprofile")){
profileList.add(currentProfile);
currentProfile = new Profile();
} else if (qName.equalsIgnoreCase("name")) {
isName = true;
} else if (qName.equalsIgnoreCase("age")) {
isAge = true;
} else if (qName.equalsIgnoreCase("address")) {
isAddress = true;
} else if (qName.equalsIgnoreCase("like")) {
isLike = true;
} else if (qName.equalsIgnoreCase("learningtools")) {
isLearningtools = true;
} else if (qName.equalsIgnoreCase("programminglanguage")) {
isProgramminglanguage = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("myprofile")) {
profileList.add(currentProfile);
currentProfile = null;
} else if (qName.equalsIgnoreCase("name")) {
isName = false;
} else if (qName.equalsIgnoreCase("age")) {
isAge = false;
} else if (qName.equalsIgnoreCase("address")) {
isAddress = false;
} else if (qName.equalsIgnoreCase("like")) {
isLike = false;
} else if (qName.equalsIgnoreCase("learningtools")) {
isLearningtools = false;
} else if (qName.equalsIgnoreCase("programminglanguage")) {
isProgramminglanguage = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if (isName) {
currentProfile.setName(value);
} else if (isAge) {
currentProfile.setAge(value);
} else if (isAddress) {
currentProfile.setAddress(value);
} else if (isLike) {
currentProfile.setLike(value);
} else if (isLearningtools) {
currentProfile.setLearningtools(value);
} else if (isProgramminglanguage) {
currentProfile.setProgramminglanguage(value);
}
}
public List<Profile> getProfileList() {
return profileList;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ProfileJavaswing;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author admin
*/
public class ProfileJFrame extends javax.swing.JFrame {
DefaultTableModel tableModel;
List<Profile> profileList = new ArrayList<>();
public ProfileJFrame() {
initComponents();
tableModel = (DefaultTableModel) tblProfile.getModel();
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
txtAge = new javax.swing.JTextField();
txtAddress = new javax.swing.JTextField();
txtLike = new javax.swing.JTextField();
txtLtools = new javax.swing.JTextField();
txtProlanguage = new javax.swing.JTextField();
btnSave = new javax.swing.JButton();
btnImport = new javax.swing.JButton();
btnExport = new javax.swing.JButton();
btnEdit = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
btnReset = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblProfile = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Profile Detail Information"));
jLabel1.setText("Name:");
jLabel2.setText("Age:");
jLabel3.setText("Address:");
jLabel4.setText("Like:");
jLabel5.setText("Learning Tools:");
jLabel6.setText("Programming Language:");
txtAge.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtAgeActionPerformed(evt);
}
});
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnImport.setText("Import XML");
btnImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportActionPerformed(evt);
}
});
btnExport.setText("Export XML");
btnExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExportActionPerformed(evt);
}
});
btnEdit.setText("Edit");
btnEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
btnReset.setText("Reset");
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnResetActionPerformed(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()
.addGap(23, 23, 23)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtLtools)
.addComponent(txtProlanguage)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(99, 99, 99)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtLike, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addGap(69, 69, 69)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnImport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnExport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnEdit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(24, 24, 24))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSave))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnEdit))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnDelete))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtLike, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnReset))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(txtLtools, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnImport))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(txtProlanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnExport))
.addContainerGap(27, Short.MAX_VALUE))
);
tblProfile.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Name", "Age", "Address", "Like", "Learning Tools", "Programming Language"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, true, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(tblProfile);
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, false)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void txtAgeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String name = txtName.getText();
String age = txtAge.getText();
String address = txtAddress.getText();
String like = txtLike.getText();
String learningtools = txtLtools.getText();
String programminglanguage = txtProlanguage.getText();
if(name.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Name is empty");
return;
}else if(age.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Age is empty");
}else if(address.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Address is empty");
}else if(like.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Like is empty");
}else if(learningtools.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Learning Tools is empty");
}else if(programminglanguage.isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Programming Language is empty");
}
Profile pro = new Profile(name, age, address, like, learningtools, programminglanguage);
profileList.add(pro);
tableModel.addRow(new Object[] {name, age, address, like, learningtools, programminglanguage});
}
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtName.setText("");
txtAge.setText("");
txtAddress.setText("");
txtLike.setText("");
txtLtools.setText("");
txtProlanguage.setText("");
}
private void btnImportActionPerformed(java.awt.event.ActionEvent evt) {
FileInputStream fis = null;
try {
// TODO add your handling code here:
fis = new FileInputStream("ProfileXML.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
ProfileHandler handler = new ProfileHandler();
parser.parse(fis, handler);
profileList = handler.getProfileList();
tableModel.setRowCount(0);
for (Profile profile : profileList) {
tableModel.addRow(new Object[]{
profile.getName(), profile.getAge(),
profile.getAddress(), profile.getLike(),
profile.getLearningtools(), profile.getProgramminglanguage()
});
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
JOptionPane.showMessageDialog(rootPane, "Import Success");
}
private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
StringBuilder builder = new StringBuilder();
for (Profile profile : profileList) {
builder.append(profile.getXMLString());
}
String body = builder.toString();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<profile_list>\n" +
body+
"\n</profile_list>";
FileOutputStream fos = null;
try {
fos = new FileOutputStream("ProfileXML.xml");
byte[] data = xml.getBytes();
fos.write(data);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(ProfileJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
JOptionPane.showMessageDialog(rootPane, "Export Success");
}
/**
* @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(ProfileJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProfileJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProfileJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProfileJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProfileJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnEdit;
private javax.swing.JButton btnExport;
private javax.swing.JButton btnImport;
private javax.swing.JButton btnReset;
private javax.swing.JButton btnSave;
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.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblProfile;
private javax.swing.JTextField txtAddress;
private javax.swing.JTextField txtAge;
private javax.swing.JTextField txtLike;
private javax.swing.JTextField txtLtools;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtProlanguage;
// End of variables declaration
}