[Java] Jtable - custome jtable

Public on October 19, 2014
Account.java
public class ACC {

    private double accNum, accMoney;
    private String accName;
    public ACC(double accNum, double accMoney, String accName) {
        super();
        this.accNum = accNum;
        this.accMoney = accMoney;
        this.accName = accName;
    }
    public ACC() {
        this(0, 0, "No Name");
    }
    public double getAccNum() {
        return accNum;
    }
    public void setAccNum(double accNum) {
        this.accNum = accNum;
    }
    public double getAccMoney() {
        return accMoney;
    }
    public void setAccMoney(double accMoney) {
        this.accMoney = accMoney;
    }
    public String getAccName() {
        return accName;
    }
    public void setAccName(String accName) {
        this.accName = accName;
    }
    @Override
    public String toString() {
        return "ACC [accNum=" + accNum + ", accMoney=" + accMoney
                + ", accName=" + accName + "]";
    }
}

AccModelTable.java

package thanhcs.bai20;

import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class AccModelTable extends AbstractTableModel{
        
    private Vector<ACC> vtacc;
    private String[] titleAcc = {"Acc Number", "Acc Name", "Acc Money"};
    Formatter x;
    String name;
    double money;
String txt;
    public AccModelTable(Vector<ACC> vtacc){
        super();
        this.vtacc = vtacc;
        
    }

    @Override
    public int getColumnCount() {
        return titleAcc.length;
    }
    

    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return vtacc.size();
    }


    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        ACC a = vtacc.get(rowIndex);
        switch (columnIndex) {
        case 0:
            a.setAccNum((Double)aValue);
            break;
        case 1:
            a.setAccName((String) aValue);
            break;
        case 2:
            a.setAccMoney((Double)aValue);
            break;

        default:
            break;
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ACC a = vtacc.get(rowIndex);
        switch (columnIndex) {
        case 0:
            
            return a.getAccNum();
        case 1:
            
        return a.getAccName();
        case 2:
            return a.getAccMoney();

        default:
            break;
        }


        return null;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return getValueAt(0, columnIndex).getClass();
    }

    @Override
    public String getColumnName(int column) {
        return titleAcc[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        // TODO Auto-generated method stub
        return super.isCellEditable(rowIndex, columnIndex);
    }

    public  void in()
    {
        int rows =  this.getRowCount();
        int cols = this.getColumnCount();
        
        for (int i = 0 ; i  < rows ; i++)
        {
            System.out.println(i+1+"-");
            {
                for(int j = 0 ; j <cols ; j++)
                {
                    String columname =  this.getColumnName(j);
                    Object mObject = this.getValueAt(i, j);
                    if(columname.equalsIgnoreCase("Acc Money"))
                    {
                        Double a = (Double)mObject;
                        money =  a.doubleValue();
                        System.out.print("\t"+money +" $");
                    }
                    else
                    {
                        System.out.print("\t"+mObject); 
                        
                        try {
                            x=  new Formatter("thanhcs.txt");
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            System.out.println("erro");
                        }
                    //     txt += (String) mObject;
                        x.format("%s" , mObject);
                    
                    }
                    
                    
                    
                    
                }
            }
        }
        dongfile();
    }

    private void dongfile() {
        x.close();
        
    }


}



Bai20_Jtable.java //main

package thanhcs.bai20;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.crypto.Mac;
import javax.swing.BorderFactory;
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.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

public class Bai20_Jtable extends JFrame implements ActionListener{

    
    Vector<ACC>vtacc;
    ACC mACC;
    JTable jtable;
    AccModelTable accModel;
    JButton add, clear, exit;
    JTextField txtnum, txtname, txtmoney;
    
    public Bai20_Jtable() {
        // TODO Auto-generated constructor stub
        setLayout(new BorderLayout());
        setGiaoDien();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);        
        setSize(400, 400);
        validate();
    }
    
    private void setGiaoDien() {
        
        vtacc = new Vector<>();
        for(int i = 0 ; i  < 3; i++)
        {
            mACC = new ACC();
            mACC.setAccNum(i);
            mACC.setAccName("Name "+i);
            mACC.setAccMoney(100*i+69);
            vtacc.addElement(mACC);
        }
        accModel = new  AccModelTable(vtacc);
        /*--------------------------------------------------
        *Suli giao dien
        *---------------------------------------------------*/
        JPanel jptop = new JPanel();
        JPanel jpcenter = new JPanel();
        JPanel jpbotton = new JPanel();
        
        /*
         * su li top
         */
        jptop.setLayout(new GridLayout(3, 2, 7, 7));
        jptop.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.RED), "Fill it"));
        jptop.add(new JLabel("Account Number : "));
        jptop.add(txtnum = new JTextField(10));
        jptop.add(new JLabel("Account Name : "));
        jptop.add(txtname = new JTextField(10));
        jptop.add(new JLabel("Account Money : "));
        jptop.add(txtmoney = new JTextField(10));
        add(jptop, BorderLayout.NORTH);
        
        /*
         * su li center
         */
        jtable = new JTable(accModel);
        add(new JScrollPane(jtable), BorderLayout.CENTER);
        //add(jpcenter, BorderLayout.CENTER);
        
        /*
         * su li botton
         */
        jpbotton.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.YELLOW), "Control"));
        jpbotton.add(add = new JButton("ADD"));
        jpbotton.add(clear = new JButton("CLEAR"));
        jpbotton.add(exit = new JButton("EXIT"));
        add(jpbotton, BorderLayout.SOUTH);
        add.addActionListener(this);
        exit.addActionListener(this);
        
    }

    public static void main(String[] args) {
        new Bai20_Jtable();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==add)
        {
            mACC = new ACC();
            mACC.setAccNum(Double.parseDouble(txtnum.getText().toString()));
            mACC.setAccName(txtname.getText().toString());
            mACC.setAccMoney(Double.parseDouble(txtmoney.getText().toString()));
            vtacc.addElement(mACC);
            jtable.revalidate();
        //    JOptionPane.showMessageDialog(null, mACC.toString());
        }
        
        if(e.getSource()==exit)
        {
            accModel.in();
            System.exit(0);
        }
    }
}




[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon