Mình tạo nút add để thêm dòng vào Bill Table ở tab Bill Manage nhưng khi nhấn nào table mất luôn.Nhờ mấy bạn giúp với . Cảm ơn nhiều .
package bill;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.ImageIcon;
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.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class SalesManageGUI extends JFrame implements ActionListener{
private JLabel lbTitle1,lbID,lbName,lbDis,lbNOI,lbNOP,lbPrice;
private JTextField txtID,txtName,txtDis,txtNOI,txtNOP,txtPrice;
private JTable tMain ;
private JButton btnAdd3,btnEdit3,btnDelete3,btnInsert2,btnSave3 ;
private String[] column = {"ID Item","Name Item","Distributor","Price","Number Of Inventory","Note"} ;
private Object[][] data ;
private Item[] a = new Item[100] ;
Vector vTittle = new Vector() ;
Vector vTittle2 = new Vector() ;
Vector vTittle3 = new Vector() ;
Vector vMain = new Vector();
Vector vMain2 = new Vector();
Vector vAdd,vAdd2,vAdd3 ;
private DefaultTableModel dtm = new DefaultTableModel() ;
private DefaultTableModel dtm2 = new DefaultTableModel() ;
private int n ;
private int nCus = 0 ;
private int nItem = 0 ;
private ArrayOfBill b = new ArrayOfBill() ;
private JLabel lbSB1,lbSB2, lbCus, lbDate, lbPay1,lbPay2,lbNote1,lbNote2,lbItem,lbNumber,lbTitle,lbTb1,lbTb2,lbFind,lbPnb,lbAdd;
private JTextField txtSB1,txtSB2,txtCus,txtDate,txtPay1,txtPay2,txtItem,txtNumber,txtSum,txtNote1,txtNote2,txtFind,txtPnb,txtAdd;
private JButton btnAdd1,btnEdit1,btnReset1,btnRemove1, btnSort,btnSum,btnSave,btnFind,btnAdd2,btnEdit2,btnReset2,btnRemove2;
private JPanel pButton1,pButton2,JTable1,JTable2;
JTable tMain1,tMain2;
public SalesManageGUI() throws FileNotFoundException {
super("Sale Manage") ;
super.setSize(1400,800);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setVisible(true);
JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);
super.getContentPane().add(tab,BorderLayout.CENTER) ;
//table Bill Manage
JPanel panel_2 = new JPanel() ;
Font fontLb=new Font("Time new Roman",Font.PLAIN,16);
Font fontTb=new Font("Time new Roman",Font.BOLD, 22);
lbTb1=new JLabel("Bill Table");
lbTb1.setFont(fontTb);
lbTb1.setBounds(870,130,100,50);
panel_2.add(lbTb1) ;
lbTb2=new JLabel("Detail Bill Table");
lbTb2.setFont(fontTb);
lbTb2.setBounds(840,450,300,30);
panel_2.add(lbTb2) ;
lbTitle=new JLabel("Sale Manage");
Font font=new Font("Time new Roman",Font.BOLD,30);
lbTitle.setFont(font);
lbTitle.setBounds(650,20,400,35);
panel_2.add(lbTitle) ;
lbSB1=new JLabel("Bill Serial");
lbSB1.setFont(fontLb);
lbSB1.setBounds(50,200,100,20);
panel_2.add(lbSB1) ;
txtSB1=new JTextField();
txtSB1.setBounds(180,200,150,25);
panel_2.add(txtSB1) ;
lbCus=new JLabel("Customer");
lbCus.setFont(fontLb);
lbCus.setBounds(50,240,100,20);
panel_2.add(lbCus) ;
txtCus=new JTextField();
txtCus.setBounds(180,240,150,25);
panel_2.add(txtCus) ;
lbPnb=new JLabel("Phone Number");
lbPnb.setFont(fontLb);
lbPnb.setBounds(50,280,130,20);
panel_2.add(lbPnb) ;
txtPnb=new JTextField();
panel_2.add(txtPnb) ;
txtPnb.setBounds(180,280,150,25);
lbAdd=new JLabel("Address");
lbAdd.setFont(fontLb);
lbAdd.setBounds(50,320,100,20);
panel_2.add(lbAdd) ;
txtAdd=new JTextField();
txtAdd.setBounds(180,320,150,25);
panel_2.add(txtAdd) ;
lbDate=new JLabel("Date");
lbDate.setFont(fontLb);
lbDate.setBounds(380,200,50,20);
panel_2.add(lbDate) ;
txtDate=new JTextField();
txtDate.setBounds(480,200,150,25);
panel_2.add(txtDate) ;
lbPay1=new JLabel("Payment");
lbPay1.setFont(fontLb);
lbPay1.setBounds(380, 240, 100, 20);
panel_2.add(lbPay1) ;
txtPay1=new JTextField();
txtPay1.setBounds(480, 240, 150, 25);
panel_2.add(txtPay1) ;
lbNote1=new JLabel("Note");
lbNote1.setFont(fontLb);
lbNote1.setBounds(380, 280, 100, 20);
panel_2.add(lbNote1) ;
txtNote1=new JTextField();
txtNote1.setBounds(480,280,150,65);
panel_2.add(txtNote1) ;
pButton1 = new JPanel();
btnAdd1=new JButton("Add");
btnEdit1=new JButton("Edit");
btnReset1=new JButton("Reset");
btnRemove1=new JButton("Remove");
pButton1.add(btnAdd1);
pButton1.add(btnEdit1);
pButton1.add(btnReset1);
pButton1.add(btnRemove1);
pButton1.setBounds(200, 398, 400, 40);
panel_2.add(pButton1) ;
String[] column1 = {"Ordinal Number","Bill Serial","Customer","Phone Number","Address","Date","Payment","Note"} ;
Object[][] data1 = new Object[50][50] ;
tMain1 = new JTable(data1,column1) ;
JScrollPane table1 = new JScrollPane(tMain1) ;
table1.setBounds(700,200,800,200);
panel_2.add(table1) ;
btnSort=new JButton("Sort");
btnSort.setBounds(1199,176, 100, 25);
panel_2.add(btnSort) ;
btnSum=new JButton("Sum");
btnSum.setBounds(1197,398,100,25);
panel_2.add(btnSum) ;
txtSum=new JTextField();
txtSum.setBounds(1296,398,100,26);
panel_2.add(txtSum) ;
lbFind=new JLabel("Enter Bill Serial");
lbFind.setBounds(705,400,100,25);
panel_2.add(lbFind) ;
txtFind=new JTextField();
txtFind.setBounds(800,398,102,25);
panel_2.add(txtFind) ;
btnFind=new JButton("Find");
btnFind.setBounds(900,398,101,24);
panel_2.add(btnFind) ;
//txtPay,txtIdI,txtItem,txtNumber
lbSB2=new JLabel("Bill Serial");
lbSB2.setFont(fontLb);
lbSB2.setBounds(50,500,100,20);
panel_2.add(lbSB2) ;
//
txtSB2=new JTextField();
txtSB2.setBounds(150,500,150,25);
panel_2.add(txtSB2) ;
lbItem=new JLabel("Item Name");
lbItem.setFont(fontLb);
lbItem.setBounds(50,540,100,20);
panel_2.add(lbItem) ;
txtItem=new JTextField();
txtItem.setBounds(150,540, 150, 25);
panel_2.add(txtItem) ;
lbNumber=new JLabel("Number");
lbNumber.setFont(fontLb);
lbNumber.setBounds(50,580,100,20);
panel_2.add(lbNumber) ;
txtNumber=new JTextField();
txtNumber.setBounds(150,580,150,25);
panel_2.add(txtNumber) ;
//
lbPay2=new JLabel("Payment");
lbPay2.setFont(fontLb);
lbPay2.setBounds(50,620,100,20);
panel_2.add(lbPay2) ;
txtPay2=new JTextField();
txtPay2.setBounds(150,620,150,25);
panel_2.add(txtPay2) ;
lbNote2=new JLabel("Note");
lbNote2.setFont(fontLb);
lbNote2.setBounds(380,500,50,20);
panel_2.add(lbNote2) ;
txtNote2=new JTextField();
txtNote2.setBounds(480,500,170,105);
panel_2.add(txtNote2) ;
//
//
pButton2 = new JPanel();
btnAdd2 = new JButton("Add") ;
btnEdit2=new JButton("Edit");
btnReset2=new JButton("Reset");
btnRemove2=new JButton("Remove");
pButton2.add(btnAdd2);
pButton2.add(btnEdit2);
pButton2.add(btnReset2);
pButton2.add(btnRemove2);
pButton2.setBounds(200, 680, 400, 40);
panel_2.add(pButton2) ;
String[] column2 = {"Ordinal Numbers","Bill Serial","Item","Number","Payment","Note"} ;
Object[][] data2 = new Object[50][50] ;
tMain2 = new JTable(data2,column2) ;
JScrollPane table2 = new JScrollPane(tMain2) ;
table2.setBounds(700,500,800,200);
panel_2.add(table2) ;
btnAdd1.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
DateOfPurchase d = new DateOfPurchase(9,8,2000) ;
Customer c = new Customer(txtCus.getText(),true,txtPnb.getText(),txtAdd.getText()) ;
b.getA()[nCus] = new Bill(txtSB1.getText(),nItem,c,d) ;
addCus(b.getA()[nCus]);
dtm2.setDataVector(vMain2,vTittle2);
tMain1.setModel(dtm2);
}
});
panel_2.setLayout(null);
tab.addTab("Bill Manage", null, panel_2, null);
//table Item Manage
JPanel panel_1 = new JPanel();
Font f=new Font("Times New Roman",Font.BOLD,30);
lbTitle=new JLabel("ITEM");
lbTitle.setBounds(650,10,100,30);//x,y,chieu rong,chieu cao
lbTitle.setFont(f);
panel_1.add(lbTitle) ;
lbID=new JLabel("ID Item ");
lbID.setBounds(120,500,80,30);
panel_1.add(lbID) ;
txtID = new JTextField(5);
txtID.setBounds(200,500,150,30);
panel_1.add(txtID) ;
lbName = new JLabel("Name Item ") ;
lbName.setBounds(120,550,80,30);
panel_1.add(lbName) ;
txtName = new JTextField(5);
txtName.setBounds(200,550,150,30);
panel_1.add(txtName) ;
lbPrice = new JLabel("Price ") ;
lbPrice.setBounds(120,600,80,30);
panel_1.add(lbPrice) ;
txtPrice = new JTextField(5);
txtPrice.setBounds(200,600,150,30);
panel_1.add(txtPrice) ;
lbDis = new JLabel("Distributor") ;
lbDis.setBounds(400,500,80,30);
panel_1.add(lbDis) ;
txtDis = new JTextField(5);
txtDis.setBounds(530,500,150,30);
panel_1.add(txtDis) ;
lbNOI = new JLabel("Number of inventory") ;
lbNOI.setBounds(400,600,120,30);
panel_1.add(lbNOI) ;
txtNOI = new JTextField(5);
txtNOI.setBounds(530,600,150,30);
panel_1.add(txtNOI) ;
btnAdd3 = new JButton("Add") ;
btnAdd3.setBounds(120,700,100,40);
panel_1.add(btnAdd3) ;
btnEdit3 = new JButton("Edit");
btnEdit3.setBounds(270,700,100,40);
panel_1.add(btnEdit3) ;
btnDelete3 = new JButton("Delete");
btnDelete3.setBounds(420,700,100,40);
panel_1.add(btnDelete3) ;
btnSave3 = new JButton("Save");
btnSave3.setBounds(570,700,100,40);
panel_1.add(btnSave3) ;
//setText for Table
tMain = new JTable(data,column) ;
JScrollPane table = new JScrollPane(tMain) ;
table.setBounds(100,60,1200,400);
panel_1.add(table) ;
title();
btnAdd3.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
a[n] = new Item(txtID.getText(),txtName.getText(),txtDis.getText(),Integer.parseInt(txtPrice.getText()),0,Integer.parseInt(txtNOI.getText())) ;
addItem(a[n]);
dtm.setDataVector(vMain,vTittle);
tMain.setModel(dtm);
JOptionPane.showMessageDialog(null,"dfdf");
}
});
super.validate();
panel_1.setLayout(null);
tab.addTab("Item Manage", null, panel_1,null);
readFile("src/bill/WareHouse");
for(int i = 0 ; i < this.n ; i++) {
addItem(a[i]);
dtm.setDataVector(vMain,vTittle);
tMain.setModel(dtm);
}
super.validate();
}
public void title() {
vTittle.add("Serial Item") ;
vTittle.add("Name Item") ;
vTittle.add("Price") ;
vTittle.add("Distributor") ;
vTittle.add("Number Of Inventory") ;
vTittle.add("Note") ;
}
public void title2() {
vTittle2.add("Ordirial Number") ;
vTittle2.add("Bill Serial") ;
vTittle2.add("Customer") ;
vTittle2.add("Phone Number") ;
vTittle2.add("Address") ;
vTittle2.add("Date") ;
vTittle2.add("Payment") ;
}
public void addItem(Item i) {
vAdd = new Vector() ;
vAdd.add(i.getId()) ;
vAdd.add(i.getNameOfItem()) ;
vAdd.add(i.getPrice()) ;
vAdd.add(i.getDistribor()) ;
vAdd.add(i.getNumberOfInventory()) ;
n++ ;
vMain.add(vAdd) ;
}
public void addCus(Bill i) {
vAdd2 = new Vector() ;
vAdd2.add(nCus) ;
vAdd2.add(i.getSerialBill()) ;
vAdd2.add(i.getC().getFullname()) ;
vAdd2.add(i.getC().getPhonenumber()) ;
vAdd2.add(txtAdd.getText()) ;
vAdd2.add(txtDate.getText()) ;
vAdd2.add(txtNote1.getText()) ;
nCus++ ;
vMain2.add(vAdd2) ;
}
public void readFile(String s) throws FileNotFoundException{
Scanner kb = new Scanner(new File(s)) ;
while(kb.hasNextLine() == true) {
a[n] = new Item(kb.next(),kb.next(),kb.next(),kb.nextInt(),0,kb.nextInt()) ;
n++ ;
}
}
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
SalesManageGUI s = new SalesManageGUI() ;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
package bill;
import java.util.Scanner;
public class ArrayOfBill {
private int n ;
private Bill[] a = new Bill[100] ;
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public Bill[] getA() {
return a;
}
public void setA(Bill[] a) {
this.a = a;
}
public void input() {
Scanner kb = new Scanner(System.in) ;
System.out.println("Enter n : ");
this.setN(kb.nextInt());
for(int i = 0 ; i < this.n ; i++) {
System.out.println("Enter Bill "+(i+1)+" : ");
a[i].input() ;
}
}
public void output() {
for(int i = 0 ; i < this.n ; i++)
a[i].output();
System.out.println();
}
public void remove(int t) {
for(int i = t ; i < this.n ; i++)
a[i] = a[i+1] ;
n-- ;
}
public void add() {
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
package bill;
import java.util.Scanner;
public class Bill {
private Item[] a = new Item[100] ;
private Customer c = new Customer();
private String serialBill ;
private DateOfPurchase dop = new DateOfPurchase();
private int n ;
public Item[] getA() {
return a;
}
public void setA(Item[] a) {
this.a = a;
}
public String getSerialBill() {
return serialBill;
}
public void setSerialBill(String serialBill) {
this.serialBill = serialBill;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public Item[] getI() {
return a;
}
public void setI(Item[] i) {
this.a = a;
}
public Customer getC()
{
return c;
}
public void setC(Customer c)
{
this.c=c;
}
public DateOfPurchase getDop()
{
return dop;
}
public void setDop(DateOfPurchase dop)
{
this.dop=dop;
}
public Bill() //khoi tao ham ko doi so
{
}
public Bill(String s,int n,Customer c,DateOfPurchase d) //khoi tao ham co doi so
{
setSerialBill(s);
setN(n);
setC(c);
setDop(dop);
}
public double total()
{
double sum = 0 ;
for(int j = 0 ; j < this.n ; j++)
sum += a[j].total() ;
return sum ;
}
public void input()
{
Scanner kb=new Scanner(System.in);
System.out.println("Enter Profile of Customer :");
c.input();
System.out.println("Date of purchase");
dop.input();
System.out.println("Enter n : ");
this.setN(kb.nextInt());
for(int j = 0 ; j < this.n ; j++) {
System.out.println("Enter Item 1 : ");
Item i=new Item();
i.input();
a[j]=i;
}
}
public void output()
{
c.output();
dop.output();
for(int j = 0 ; j < this.n ; j++)
a[j].output();
System.out.println("Total : "+total());
}
public static void main(String[] args) {
Bill b=new Bill();
b.input();
b.output();
}
}
package bill;
import java.util.Scanner;
public class Item {
protected String nameOfItem,Id,distribor ;
protected int price ;
protected int numberOfPurchase,numberOfInventory ;
public Item() {
}
public Item(String id,String name,String distributor,int price,int purchase,int inventory) {
setId(id);
setNameOfItem(name);
setDistribor(distributor);
setPrice(price);
setNumberOfInventory(inventory);
setNumberOfPurchase(purchase);
}
public String getNameOfItem() {
return nameOfItem;
}
public void setNameOfItem(String nameOfItem) {
this.nameOfItem = nameOfItem;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getDistribor() {
return distribor;
}
public void setDistribor(String distribor) {
this.distribor = distribor;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNumberOfPurchase() {
return numberOfPurchase;
}
public void setNumberOfPurchase(int numberOfPurchase) {
this.numberOfPurchase = numberOfPurchase;
}
public int getNumberOfInventory() {
return numberOfInventory;
}
public void setNumberOfInventory(int numberOfInventory) {
this.numberOfInventory = numberOfInventory;
}
public void input() {
Scanner kb = new Scanner(System.in) ;
System.out.print("Enter Id : ");
this.setId(kb.next());
System.out.print("Enter name of item : ");
this.setNameOfItem(kb.nextLine());
kb.nextLine() ;
System.out.print("Enter Distributor : ");
this.setDistribor(kb.nextLine());
System.out.println("Enter Price : ");
this.setPrice(kb.nextInt());
System.out.print("Enter number of inventory : ");
this.setNumberOfInventory(kb.nextInt());
System.out.print("Enter number of purchase : ");
this.setNumberOfPurchase(kb.nextInt());
}
public void output() {
System.out.println("ID : "+ this.Id);
System.out.println("Name of Item : "+this.nameOfItem);
System.out.println("Distributor : "+this.distribor);
System.out.println("Number Of Inventory : "+this.numberOfInventory);
System.out.println("Number Of Purchase : "+this.numberOfPurchase);
}
public double total() {
return this.price*this.numberOfPurchase ;
}
}