By GokiSoft.com| 10:08 20/09/2021|
Java Advanced

[Share Code] Quản lý thông tin cá nhân Profile bằng java - import + export XML File - C2010G

Quản lý thông tin cá nhân Profile bằng java - import + export XML File

# profile.xml

<?xml version="1.0" encoding="UTF-8"?>

<root>
    <info>
        <fullname>TRAN VAN DIEP</fullname>
        <birthday>08/02/1999</birthday>
        <gender>Nam</gender>
        <email>tranvandiep.it@gmail.com</email>
        <address>Ha Noi</address>
    </info>
    <hobits>
        <hobit>Da Bong</hobit>
        <hobit>Boi Loi</hobit>
        <hobit>Chay</hobit>
        <hobit>234</hobit>
    </hobits>
    <items>
        <item>May Tinh</item>
        <item>Keyboard</item>
        <item>Mouse</item>
        <item>Pen</item>
        <item>Book</item>
        <item>345</item>
    </items>
    <programing-language>
        <language>Lap Trinh C</language>
        <language>Lap Trinh HTML/CSS/JS</language>
        <language>Lap Trinh Bootstrap/jQuery</language>
        <language>4565</language>
    </programing-language>
</root>



#IHandler.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 xml.lesson04.bt1202;

/**
 *
 * @author Diep.Tran
 */
public interface IHandler {
    void startElement(String qName);
    void endElement(String qName);
    void characters(String value);
}


#Info.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 xml.lesson04.bt1202;

/**
 *
 * @author Diep.Tran
 */
public class Info implements IHandler {

    String fullname, email, gender, birthday, address;

    boolean isFullname = false;
    boolean isEmail = false;
    boolean isGender = false;
    boolean isBirthday = false;
    boolean isAddress = false;

    public Info() {
    }

    public Info(String fullname, String email, String gender, String birthday, String address) {
        this.fullname = fullname;
        this.email = email;
        this.gender = gender;
        this.birthday = birthday;
        this.address = address;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    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 getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Info{" + "fullname=" + fullname + ", email=" + email + ", gender=" + gender + ", birthday=" + birthday + ", address=" + address + '}';
    }

    @Override
    public void startElement(String qName) {
        switch (qName) {
            case "fullname":
                isFullname = true;
                break;
            case "email":
                isEmail = true;
                break;
            case "address":
                isAddress = true;
                break;
            case "gender":
                isGender = true;
                break;
            case "birthday":
                isBirthday = true;
                break;
        }
    }

    @Override
    public void endElement(String qName) {
        switch (qName) {
            case "fullname":
                isFullname = false;
                break;
            case "email":
                isEmail = false;
                break;
            case "address":
                isAddress = false;
                break;
            case "gender":
                isGender = false;
                break;
            case "birthday":
                isBirthday = false;
                break;
        }
    }

    @Override
    public void characters(String value) {
        if(isFullname) {
            fullname = value;
        } else if(isEmail) {
            email = value;
        } else if(isAddress) {
            address = value;
        } else if(isGender) {
            gender = value;
        } else if(isBirthday) {
            birthday = value;
        }
    }
    
    public String getXML() {
        return "    <info>\n" +
                "        <fullname>"+fullname+"</fullname>\n" +
                "        <birthday>"+birthday+"</birthday>\n" +
                "        <gender>"+gender+"</gender>\n" +
                "        <email>"+email+"</email>\n" +
                "        <address>"+address+"</address>\n" +
                "    </info>";
    }
}


#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 xml.lesson04.bt1202;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        Profile profile = Utility.parseProfileXML();
        
        profile.display();
        
        profile.hobitList.add("ABC");
        profile.itemList.add("2234234");
        profile.languageList.add("SQL Server");
        
        profile.saveXMLFile();
    }
}


#Profile.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 xml.lesson04.bt1202;

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;

/**
 *
 * @author Diep.Tran
 */
public class Profile implements IHandler{
    Info info;
    List<String> hobitList;
    List<String> itemList;
    List<String> languageList;
    
    boolean isInfo = false;
    boolean isHobit = false;
    boolean isItem = false;
    boolean isLanguage = false;

    public Profile() {
        hobitList = new ArrayList<>();
        itemList = new ArrayList<>();
        languageList = new ArrayList<>();
    }
    
    public void display() {
        System.out.println("===== Thong Tin Ca Nhan: ");
        System.out.println(info);
        
        System.out.println("===== So Thich:");
        for (String s : hobitList) {
            System.out.println(s);
        }
        
        System.out.println("===== Dung Cu Hoc Tap:");
        for (String s : itemList) {
            System.out.println(s);
        }
        
        System.out.println("===== Ngon Ngu Lap Trinh: ");
        for (String s : languageList) {
            System.out.println(s);
        }
    }

    @Override
    public void startElement(String qName) {
        switch(qName) {
            case "info":
                info = new Info();
                isInfo = true;
                break;
            case "hobit":
                isHobit = true;
                break;
            case "item":
                isItem = true;
                break;
            case "language":
                isLanguage = true;
                break;
            default:
                if(isInfo) {
                    info.startElement(qName);
                }
                break;
        }
    }

    @Override
    public void endElement(String qName) {
        switch(qName) {
            case "info":
                isInfo = false;
                break;
            case "hobit":
                isHobit = false;
                break;
            case "item":
                isItem = false;
                break;
            case "language":
                isLanguage = false;
                break;
            default:
                if(isInfo) {
                    info.endElement(qName);
                }
                break;
        }
    }

    @Override
    public void characters(String value) {
        if(isInfo) {
            info.characters(value);
        } else if(isHobit) {
            hobitList.add(value);
        } else if(isItem) {
            itemList.add(value);
        } else if(isLanguage) {
            languageList.add(value);
        }
    }
    
    public String getXML() {
        String infoXML = info.getXML();
        String hobitXML = "";
        for (String s : hobitList) {
            hobitXML += "        <hobit>"+s+"</hobit>\n";
        }
        hobitXML = "\n    <hobits>\n" +
            hobitXML +
            "    </hobits>";
        
        String itemXML = "";
        for (String s : itemList) {
            itemXML += "        <item>"+s+"</item>\n";
        }
        itemXML = "\n    <items>\n" +
            itemXML +
            "    </items>";
        String languageXML = "";
        for (String s : languageList) {
            languageXML += "        <language>"+s+"</language>\n";
        }
        languageXML = "\n    <programing-language>\n" +
            languageXML +
            "    </programing-language>";
        
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "\n" +
                "<root>\n" +
                infoXML +
                hobitXML +
                itemXML +
                languageXML +
                "\n</root>";
        
        return xml;
    }
    
    public void saveXMLFile() {
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream("profile.xml");
            
            String content = getXML();
            byte[] data = content.getBytes("utf8");
            fos.write(data);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Profile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Profile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Profile.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Profile.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}


#ProfileFrame.form


<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
  <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" attributes="0">
              <EmptySpace max="-2" attributes="0"/>
              <Group type="103" groupAlignment="0" attributes="0">
                  <Component id="jPanel1" alignment="1" max="32767" attributes="0"/>
                  <Component id="jTabbedPane2" alignment="0" max="32767" attributes="0"/>
              </Group>
              <EmptySpace max="-2" 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 type="unrelated" max="-2" attributes="0"/>
              <Component id="jTabbedPane2" max="32767" attributes="0"/>
              <EmptySpace min="-2" max="-2" 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="THONG TIN CA NHAN"/>
          </Border>
        </Property>
      </Properties>

      <Layout>
        <DimensionLayout dim="0">
          <Group type="103" groupAlignment="0" attributes="0">
              <Group type="102" alignment="0" attributes="0">
                  <EmptySpace min="-2" pref="25" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="0" attributes="0">
                      <Component id="jLabel1" alignment="0" min="-2" max="-2" attributes="0"/>
                      <Component id="jLabel2" alignment="0" min="-2" max="-2" attributes="0"/>
                      <Component id="jLabel3" alignment="0" min="-2" max="-2" attributes="0"/>
                      <Component id="jLabel4" alignment="0" min="-2" max="-2" attributes="0"/>
                      <Component id="jLabel5" alignment="0" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace min="-2" pref="35" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="0" max="-2" attributes="0">
                      <Component id="addressTxt" min="-2" pref="291" max="-2" attributes="0"/>
                      <Component id="emailTxt" min="-2" pref="291" max="-2" attributes="0"/>
                      <Component id="birthdayTxt" min="-2" pref="291" max="-2" attributes="0"/>
                      <Component id="genderTxt" min="-2" pref="291" max="-2" attributes="0"/>
                      <Component id="fullnameTxt" min="-2" pref="291" max="-2" attributes="0"/>
                      <Group type="102" alignment="1" attributes="0">
                          <Group type="103" groupAlignment="1" attributes="0">
                              <Component id="itemBtn" max="32767" attributes="0"/>
                              <Component id="saveBtn" max="32767" attributes="0"/>
                          </Group>
                          <EmptySpace type="separate" max="-2" attributes="0"/>
                          <Group type="103" groupAlignment="0" max="-2" attributes="0">
                              <Component id="hobitBtn" max="32767" attributes="0"/>
                              <Component id="languageBtn" pref="137" max="32767" attributes="0"/>
                          </Group>
                      </Group>
                  </Group>
                  <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"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="fullnameTxt" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace type="separate" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="genderTxt" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace type="separate" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="jLabel3" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="birthdayTxt" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace type="separate" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="jLabel4" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="emailTxt" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace type="separate" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="jLabel5" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="addressTxt" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace type="separate" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="saveBtn" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="hobitBtn" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace type="separate" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="itemBtn" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="languageBtn" alignment="3" min="-2" 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="text" type="java.lang.String" value="Ho &amp; Ten: "/>
          </Properties>
        </Component>
        <Component class="javax.swing.JTextField" name="fullnameTxt">
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel2">
          <Properties>
            <Property name="text" type="java.lang.String" value="Gioi Tinh:"/>
          </Properties>
        </Component>
        <Component class="javax.swing.JTextField" name="genderTxt">
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel3">
          <Properties>
            <Property name="text" type="java.lang.String" value="Ngay Sinh:"/>
          </Properties>
        </Component>
        <Component class="javax.swing.JTextField" name="birthdayTxt">
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel4">
          <Properties>
            <Property name="text" type="java.lang.String" value="Email:"/>
          </Properties>
        </Component>
        <Component class="javax.swing.JTextField" name="emailTxt">
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel5">
          <Properties>
            <Property name="text" type="java.lang.String" value="Dia Chi:"/>
          </Properties>
        </Component>
        <Component class="javax.swing.JTextField" name="addressTxt">
        </Component>
        <Component class="javax.swing.JButton" name="saveBtn">
          <Properties>
            <Property name="text" type="java.lang.String" value="L&#x1b0;u Th&#xf4;ng Tin"/>
          </Properties>
          <Events>
            <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="saveBtnActionPerformed"/>
          </Events>
        </Component>
        <Component class="javax.swing.JButton" name="hobitBtn">
          <Properties>
            <Property name="text" type="java.lang.String" value="Them So Thich"/>
          </Properties>
          <Events>
            <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="hobitBtnActionPerformed"/>
          </Events>
        </Component>
        <Component class="javax.swing.JButton" name="itemBtn">
          <Properties>
            <Property name="text" type="java.lang.String" value="Them Dung Cu"/>
          </Properties>
          <Events>
            <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="itemBtnActionPerformed"/>
          </Events>
        </Component>
        <Component class="javax.swing.JButton" name="languageBtn">
          <Properties>
            <Property name="text" type="java.lang.String" value="Them Ngon Ngu"/>
          </Properties>
          <Events>
            <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="languageBtnActionPerformed"/>
          </Events>
        </Component>
      </SubComponents>
    </Container>
    <Container class="javax.swing.JTabbedPane" name="jTabbedPane2">
      <Properties>
        <Property name="toolTipText" type="java.lang.String" value=""/>
        <Property name="name" type="java.lang.String" value="S&#x1edf; Th&#xed;ch" noResource="true"/>
      </Properties>
      <AccessibilityProperties>
        <Property name="AccessibleContext.accessibleName" type="java.lang.String" value="S&#x1edf; Th&#xed;ch"/>
      </AccessibilityProperties>

      <Layout class="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout"/>
      <SubComponents>
        <Container class="javax.swing.JPanel" name="jPanel2">
          <Properties>
            <Property name="name" type="java.lang.String" value="S&#x1edf; Th&#xed;ch" noResource="true"/>
          </Properties>
          <Constraints>
            <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout" value="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout$JTabbedPaneConstraintsDescription">
              <JTabbedPaneConstraints tabName="S&#x1edf; Th&#xed;ch">
                <Property name="tabTitle" type="java.lang.String" value="S&#x1edf; Th&#xed;ch"/>
              </JTabbedPaneConstraints>
            </Constraint>
          </Constraints>

          <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="jScrollPane1" pref="412" max="32767" attributes="0"/>
                      <EmptySpace max="-2" 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="jScrollPane1" pref="246" max="32767" attributes="0"/>
                      <EmptySpace max="-2" attributes="0"/>
                  </Group>
              </Group>
            </DimensionLayout>
          </Layout>
          <SubComponents>
            <Container class="javax.swing.JScrollPane" name="jScrollPane1">
              <AuxValues>
                <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
              </AuxValues>

              <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
              <SubComponents>
                <Component class="javax.swing.JTable" name="hobitTable">
                  <Properties>
                    <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor">
                      <Table columnCount="2" rowCount="0">
                        <Column editable="false" title="STT" type="java.lang.Object"/>
                        <Column editable="false" title="S&#x1edf; Th&#xed;ch" type="java.lang.Object"/>
                      </Table>
                    </Property>
                    <Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor">
                      <TableColumnModel selectionModel="0">
                        <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="false">
                          <Title/>
                          <Editor/>
                          <Renderer/>
                        </Column>
                        <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="false">
                          <Title/>
                          <Editor/>
                          <Renderer/>
                        </Column>
                      </TableColumnModel>
                    </Property>
                    <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor">
                      <TableHeader reorderingAllowed="true" resizingAllowed="true"/>
                    </Property>
                  </Properties>
                </Component>
              </SubComponents>
            </Container>
          </SubComponents>
        </Container>
        <Container class="javax.swing.JPanel" name="jPanel3">
          <Constraints>
            <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout" value="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout$JTabbedPaneConstraintsDescription">
              <JTabbedPaneConstraints tabName="D&#x1ee5;ng C&#x1ee5; H&#x1ecd;c T&#x1ead;p">
                <Property name="tabTitle" type="java.lang.String" value="D&#x1ee5;ng C&#x1ee5; H&#x1ecd;c T&#x1ead;p"/>
              </JTabbedPaneConstraints>
            </Constraint>
          </Constraints>

          <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="jScrollPane2" pref="427" max="32767" attributes="0"/>
                      <EmptySpace max="-2" 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="jScrollPane2" pref="246" max="32767" attributes="0"/>
                      <EmptySpace max="-2" attributes="0"/>
                  </Group>
              </Group>
            </DimensionLayout>
          </Layout>
          <SubComponents>
            <Container class="javax.swing.JScrollPane" name="jScrollPane2">
              <AuxValues>
                <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
              </AuxValues>

              <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
              <SubComponents>
                <Component class="javax.swing.JTable" name="itemTable">
                  <Properties>
                    <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor">
                      <Table columnCount="2" rowCount="0">
                        <Column editable="false" title="STT" type="java.lang.Object"/>
                        <Column editable="false" title="D&#x1ee5;ng C&#x1ee5;" type="java.lang.Object"/>
                      </Table>
                    </Property>
                    <Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor">
                      <TableColumnModel selectionModel="0">
                        <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="false">
                          <Title/>
                          <Editor/>
                          <Renderer/>
                        </Column>
                        <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="false">
                          <Title/>
                          <Editor/>
                          <Renderer/>
                        </Column>
                      </TableColumnModel>
                    </Property>
                    <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor">
                      <TableHeader reorderingAllowed="true" resizingAllowed="true"/>
                    </Property>
                  </Properties>
                </Component>
              </SubComponents>
            </Container>
          </SubComponents>
        </Container>
        <Container class="javax.swing.JPanel" name="jPanel4">
          <Constraints>
            <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout" value="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout$JTabbedPaneConstraintsDescription">
              <JTabbedPaneConstraints tabName="Ng&#xf4;n Ng&#x1eef; L&#x1ead;p Tr&#xec;nh">
                <Property name="tabTitle" type="java.lang.String" value="Ng&#xf4;n Ng&#x1eef; L&#x1ead;p Tr&#xec;nh"/>
              </JTabbedPaneConstraints>
            </Constraint>
          </Constraints>

          <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="jScrollPane3" pref="427" max="32767" attributes="0"/>
                      <EmptySpace max="-2" 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="jScrollPane3" pref="246" max="32767" attributes="0"/>
                      <EmptySpace max="-2" attributes="0"/>
                  </Group>
              </Group>
            </DimensionLayout>
          </Layout>
          <SubComponents>
            <Container class="javax.swing.JScrollPane" name="jScrollPane3">
              <AuxValues>
                <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
              </AuxValues>

              <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
              <SubComponents>
                <Component class="javax.swing.JTable" name="langugageTable">
                  <Properties>
                    <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor">
                      <Table columnCount="2" rowCount="0">
                        <Column editable="false" title="STT" type="java.lang.Object"/>
                        <Column editable="false" title="Ng&#xf4;n Ng&#x1eef;" type="java.lang.Object"/>
                      </Table>
                    </Property>
                    <Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor">
                      <TableColumnModel selectionModel="0">
                        <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="false">
                          <Title/>
                          <Editor/>
                          <Renderer/>
                        </Column>
                        <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="false">
                          <Title/>
                          <Editor/>
                          <Renderer/>
                        </Column>
                      </TableColumnModel>
                    </Property>
                    <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor">
                      <TableHeader reorderingAllowed="true" resizingAllowed="true"/>
                    </Property>
                  </Properties>
                </Component>
              </SubComponents>
            </Container>
          </SubComponents>
        </Container>
      </SubComponents>
    </Container>
  </SubComponents>
</Form>


#ProfileFrame.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 xml.lesson04.bt1202;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Diep.Tran
 */
public class ProfileFrame extends javax.swing.JFrame {
    Profile profile;
    DefaultTableModel hobitModel, itemModel, languageModel;

    /**
     * Creates new form ProfileFrame
     */
    public ProfileFrame() {
        initComponents();
        
        profile = Utility.parseProfileXML();
        
        hobitModel = (DefaultTableModel) hobitTable.getModel();
        itemModel = (DefaultTableModel) itemTable.getModel();
        languageModel = (DefaultTableModel) langugageTable.getModel();
        
        fullnameTxt.setText(profile.info.getFullname());
        genderTxt.setText(profile.info.getGender());
        addressTxt.setText(profile.info.getAddress());
        emailTxt.setText(profile.info.getEmail());
        birthdayTxt.setText(profile.info.getBirthday());
        
        updateTab();
        
        hobitTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int index = hobitTable.getSelectedRow();
                String s = profile.hobitList.get(index);
                
                s = JOptionPane.showInputDialog("Sua Thoi Quen", s);
                
                profile.hobitList.set(index, s);
                updateTab();
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        
        itemTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int index = itemTable.getSelectedRow();
                String s = profile.itemList.get(index);
                
                s = JOptionPane.showInputDialog("Sua Dung Cu", s);
                
                profile.itemList.set(index, s);
                updateTab();
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        
        langugageTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int index = langugageTable.getSelectedRow();
                String s = profile.languageList.get(index);
                
                s = JOptionPane.showInputDialog("Sua Ngon Ngu", s);
                
                profile.languageList.set(index, s);
                updateTab();
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }
    
    private void updateTab() {
        hobitModel.setRowCount(0);
        itemModel.setRowCount(0);
        languageModel.setRowCount(0);
        
        profile.hobitList.forEach(s -> {
            hobitModel.addRow(new Object[] {hobitModel.getRowCount() + 1, s});
        });
        profile.itemList.forEach(s -> {
            itemModel.addRow(new Object[] {itemModel.getRowCount() + 1, s});
        });
        profile.languageList.forEach(s -> {
            languageModel.addRow(new Object[] {languageModel.getRowCount() + 1, s});
        });
    }

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

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        fullnameTxt = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        genderTxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        birthdayTxt = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        emailTxt = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        addressTxt = new javax.swing.JTextField();
        saveBtn = new javax.swing.JButton();
        hobitBtn = new javax.swing.JButton();
        itemBtn = new javax.swing.JButton();
        languageBtn = new javax.swing.JButton();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        hobitTable = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        itemTable = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        langugageTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("THONG TIN CA NHAN"));

        jLabel1.setText("Ho & Ten: ");

        jLabel2.setText("Gioi Tinh:");

        jLabel3.setText("Ngay Sinh:");

        jLabel4.setText("Email:");

        jLabel5.setText("Dia Chi:");

        saveBtn.setText("Lưu Thông Tin");
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        hobitBtn.setText("Them So Thich");
        hobitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hobitBtnActionPerformed(evt);
            }
        });

        itemBtn.setText("Them Dung Cu");
        itemBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemBtnActionPerformed(evt);
            }
        });

        languageBtn.setText("Them Ngon Ngu");
        languageBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                languageBtnActionPerformed(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(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(addressTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(emailTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(birthdayTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(genderTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fullnameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(itemBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(saveBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(hobitBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(languageBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fullnameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(genderTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(birthdayTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(emailTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(addressTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveBtn)
                    .addComponent(hobitBtn))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itemBtn)
                    .addComponent(languageBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane2.setToolTipText("");
        jTabbedPane2.setName("Sở Thích"); // NOI18N

        jPanel2.setName("Sở Thích"); // NOI18N

        hobitTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Sở Thích"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(hobitTable);
        if (hobitTable.getColumnModel().getColumnCount() > 0) {
            hobitTable.getColumnModel().getColumn(0).setResizable(false);
            hobitTable.getColumnModel().getColumn(1).setResizable(false);
        }

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane2.addTab("Sở Thích", jPanel2);

        itemTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Dụng Cụ"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(itemTable);
        if (itemTable.getColumnModel().getColumnCount() > 0) {
            itemTable.getColumnModel().getColumn(0).setResizable(false);
            itemTable.getColumnModel().getColumn(1).setResizable(false);
        }

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane2.addTab("Dụng Cụ Học Tập", jPanel3);

        langugageTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Ngôn Ngữ"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(langugageTable);
        if (langugageTable.getColumnModel().getColumnCount() > 0) {
            langugageTable.getColumnModel().getColumn(0).setResizable(false);
            langugageTable.getColumnModel().getColumn(1).setResizable(false);
        }

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane2.addTab("Ngôn Ngữ Lập Trình", jPanel4);

        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)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTabbedPane2))
                .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)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane2)
                .addContainerGap())
        );

        jTabbedPane2.getAccessibleContext().setAccessibleName("Sở Thích");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void hobitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hobitBtnActionPerformed
        // TODO add your handling code here:
        String s = JOptionPane.showInputDialog("Them so thich");
        profile.hobitList.add(s);
        updateTab();
    }//GEN-LAST:event_hobitBtnActionPerformed

    private void itemBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemBtnActionPerformed
        // TODO add your handling code here:
        String s = JOptionPane.showInputDialog("Them dung cu");
        profile.itemList.add(s);
        updateTab();
    }//GEN-LAST:event_itemBtnActionPerformed

    private void languageBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_languageBtnActionPerformed
        // TODO add your handling code here:
        String s = JOptionPane.showInputDialog("Them ngon ngu");
        profile.languageList.add(s);
        updateTab();
    }//GEN-LAST:event_languageBtnActionPerformed

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        // TODO add your handling code here:
        profile.info.setFullname(fullnameTxt.getText());
        profile.info.setGender(genderTxt.getText());
        profile.info.setEmail(emailTxt.getText());
        profile.info.setAddress(addressTxt.getText());
        profile.info.setBirthday(birthdayTxt.getText());
        
        profile.saveXMLFile();
        JOptionPane.showMessageDialog(rootPane, "Luu Thanh Cong!!!");
    }//GEN-LAST:event_saveBtnActionPerformed

    /**
     * @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//GEN-BEGIN:variables
    private javax.swing.JTextField addressTxt;
    private javax.swing.JTextField birthdayTxt;
    private javax.swing.JTextField emailTxt;
    private javax.swing.JTextField fullnameTxt;
    private javax.swing.JTextField genderTxt;
    private javax.swing.JButton hobitBtn;
    private javax.swing.JTable hobitTable;
    private javax.swing.JButton itemBtn;
    private javax.swing.JTable itemTable;
    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.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JButton languageBtn;
    private javax.swing.JTable langugageTable;
    private javax.swing.JButton saveBtn;
    // End of variables declaration//GEN-END:variables
}


#ProfileHandler.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 xml.lesson04.bt1202;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Diep.Tran
 */
public class ProfileHandler extends DefaultHandler{
    Profile profile;

    public ProfileHandler() {
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        qName = qName.toLowerCase();
        
        switch(qName) {
            case "root":
                profile = new Profile();
                break;
            default:
                if(profile != null) {
                    profile.startElement(qName);
                }
                break;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        qName = qName.toLowerCase();
        switch(qName) {
            case "root":
                break;
            default:
                if(profile != null) {
                    profile.endElement(qName);
                }
                break;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if(profile != null) {
            profile.characters(value);
        }
    }

    public Profile getProfile() {
        return profile;
    }
}


#Utility.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 xml.lesson04.bt1202;

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 Diep.Tran
 */
public class Utility {
    public static Profile parseProfileXML() {
        try {
            File file = new File("profile.xml");
            
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            ProfileHandler handler = new ProfileHandler();
            
            parser.parse(file, handler);
            
            return handler.getProfile();
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }
}


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó