1: Create new Project !
Student.java
-----------------------------
StudentCollection.java
FILE_Process.java
MAINCLASS.java
Student.java
-----------------------------
ackage thanhcs.javademo;
import java.io.Serializable;
public class Student implements Serializable{
private int id;
private String name;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
StudentCollection.java
package thanhcs.javademo;
import java.io.Serializable;
import java.util.Vector;
import javax.swing.plaf.basic.BasicScrollPaneUI.VSBChangeListener;
public class StudentCollection implements Serializable {
Vector<Student> vtStudent;
public StudentCollection(Vector<Student>vtStudent) {
// TODO Auto-generated constructor stub
this.vtStudent = vtStudent;
}
public void addStudent(Student a)
{
vtStudent.add(a);
}
public Student getStudent(int i)
{
return vtStudent.get(i);
}
public int getNumOfSrudent()
{
return vtStudent.size();
}
}
StudentTableMode.java
package thanhcs.javademo;
import javax.swing.table.AbstractTableModel;
public class StudentTableModel extends AbstractTableModel{
StudentCollection stdCollections;
String title[] = {"id", "name"};
public StudentTableModel(StudentCollection stdCollections) {
// TODO Auto-generated constructor stub
this.stdCollections = stdCollections;
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return title.length;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return stdCollections.getNumOfSrudent();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Student a = stdCollections.getStudent(rowIndex);
switch (columnIndex) {
case 0:
return a.getId();
case 1:
return a.getName();
default:
break;
}
return null;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Student a = stdCollections.getStudent(rowIndex);
switch (columnIndex) {
case 0:
a.setId((Integer)aValue);
break;
case 1:
a.setName((String) aValue);
break;
default:
break;
}
}
@Override
public Class<?> getColumnClass(int columnIndex) {
// TODO Auto-generated method stub
return super.getColumnClass(columnIndex);
}
@Override
public String getColumnName(int column) {
// TODO Auto-generated method stub
return title[column];
}
}
FILE_Process.java
package thanhcs.javademo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class FILE_Process {
public void WRITE(Object obj , String filePath) throws Exception
{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filePath));
out.writeObject(obj);
out.close();
}
public Object READ(String filePath) throws Exception
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(filePath));
Object a = in.readObject();
in.close();
return a;
}
}
MAINCLASS.java
package thanhcs.javademo;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class MAINCLASS extends JFrame implements ActionListener, ListSelectionListener{
JTextField txtID, txtName;
JButton btnADD, btnEDIT, btnREMOVE , btnEXIT;
StudentCollection stdCollection;
JTable jtable;
Vector<Student> vtStudent;
Student studentTemp;
StudentTableModel studentModel;
FILE_Process fileProcess;
String NAMEFILE = "thanhcs94";
public MAINCLASS() {
setGiaoDien();
setSuKien();
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void setSuKien() {
}
private void setGiaoDien() {
JPanel jp1 = new JPanel();
jp1.setLayout(new BorderLayout());
add(jp1, BorderLayout.NORTH);
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
jp2.setLayout(new GridLayout(2, 2));
jp2.add(new JLabel("ID"));
jp2.add(txtID = new JTextField(20));
jp2.add(new JLabel("NAME"));
jp2.add(txtName = new JTextField(20));
jp1.add(jp2, BorderLayout.CENTER);
jp3.add(btnADD = new JButton("ADD"));
jp3.add(btnREMOVE = new JButton("REMOVE"));
jp3.add(btnEDIT = new JButton("EDIT"));
jp3.add(btnEXIT = new JButton("EXIT"));
btnADD.addActionListener(this);
btnREMOVE.addActionListener(this);
btnEDIT.addActionListener(this);
btnEXIT.addActionListener(this);
jp1.add(jp3, BorderLayout.SOUTH);
fileProcess = new FILE_Process();
vtStudent = new Vector<>();
/****** RUN IT IN THE FIRST TIME YOU BUIL .WRITE DATA TO FILE ******/
/**
for(int i = 0 ; i < 1 ; i++)
{
vtStudent.add(new Student(i, "Maria Ozawa"));
}
try {
fileProcess.WRITE(vtStudent, NAMEFILE);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
//doc file
try {
vtStudent = (Vector<Student>) fileProcess.READ(NAMEFILE);
stdCollection = new StudentCollection(vtStudent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
studentModel = new StudentTableModel(stdCollection);
jtable = new JTable(studentModel);
add(new JScrollPane(jtable), BorderLayout.CENTER);
}
public static void main(String[] args) {
new MAINCLASS().setVisible(true);;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnADD)
{
try {
int id = Integer.parseInt(txtID.getText().toString());
String name = txtName.getText();
stdCollection.addStudent(new Student(id,name));
jtable.revalidate();
} catch (Exception e2) {
JOptionPane.showMessageDialog(this, e2);
}
}
if(e.getSource()==btnREMOVE)
{
int i = jtable.getSelectedRow();
if(i==-1)
{
JOptionPane.showMessageDialog(null, "unselected");
}
else
{
int result = JOptionPane.showConfirmDialog(this, "Are you sure ??", "DELETE", JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.YES_OPTION)
{
vtStudent.removeElement(stdCollection.getStudent(i));
jtable.revalidate();
}
}
}
if(e.getSource()==btnEDIT)
{
String id=null, name =null;
int i = jtable.getSelectedRow();
System.out.println(i+"");
Student a = vtStudent.get(i);
try {
id = JOptionPane.showInputDialog("ID");
name = JOptionPane.showInputDialog("Name");
a.setId(Integer.parseInt(id));
a.setName(name);
vtStudent.remove(i);
vtStudent.add(i, a);
jtable.revalidate();
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "null value");
}
}
if(e.getSource()==btnEXIT)
{
try {
fileProcess.WRITE(vtStudent, NAMEFILE);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("DA LUU DU LIEU");
System.exit(0);
}
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
}
}
No comments:
Post a Comment