JAVA JTABLE 사용 예제

Programming/JAVA,JSP 2013. 3. 3. 11:20 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTable;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.DefaultTableCellRenderer;

import javax.swing.table.DefaultTableColumnModel;

import javax.swing.table.TableCellRenderer;

import javax.swing.table.TableColumn;

import javax.swing.table.TableColumnModel;

import javax.swing.table.TableModel;

 

class Info {

    public String name;

    public int money;

    public long contentLength = 0L;

    public Info(String name, int money)

    {

        this.name = name;

        this.money = money;

    }

}

 

class InfoTableModel extends AbstractTableModel{

    private static final long serialVersionUID = 7932826462497464190L;

    public ArrayList<Info> pages;

 

    public InfoTableModel(){

        pages = new ArrayList<Info>();

    }

    public int getColumnCount() {

        return 2;

    }

    public int getRowCount() {

        return pages.size();

    }

    public void addInfo(Info page){

        int idx = pages.size();

        pages.add(page);

        fireTableRowsInserted(idx, idx); // 반드시 호출해야한다.

    }

    public Object getValueAt(int rowIndex, int columnIndex) {

        Info info = pages.get(rowIndex);

        switch (columnIndex) {

        case 0 :

            return info.name;

        case 1 :

            return info.money;

        case 2 :

            return info.contentLength;

        default :

                return "invalid";

        }

    }

}

class myFrame extends JFrame {

    JTable table;

 

    public myFrame() {

        // TODO Auto-generated constructor stub

        setSize(600, 400);

        setVisible(true);

 

        InfoTableModel model = new InfoTableModel();

        TableColumnModel columnModel = new DefaultTableColumnModel();

        TableCellRenderer renderer = new DefaultTableCellRenderer() ; // 기본구현

        model.addInfo(new Info("김종명",50000));

        model.addInfo(new Info("김종명",40000));

        model.addInfo(new Info("김종명",60000));

       

 

        TableColumn column = new TableColumn(0);

        column.setCellRenderer(renderer); // 이렇게 하지 않아도 알아서 제공된다.

        column.setHeaderValue("name");

        columnModel.addColumn(column);

       

        column = new TableColumn(1);

        column.setHeaderValue("money");

        columnModel.addColumn(column);

            

        table = new JTable(model, columnModel);

        add(table);

    }

 

}

 

public class CarTest3 {

 

    public static void main(String args[]) {

        new myFrame();

    }

}

'Programming > JAVA,JSP' 카테고리의 다른 글

JAVA 선택정렬  (0) 2013.03.03
JAVA 삽입 정렬  (0) 2013.03.03
JAVA executeQuery INSERT DELETE  (0) 2013.03.02
JAVA 데이터베이스 실습 예제 UI  (0) 2013.02.24
JAVA JDBC 튜토리얼 예제 사이트  (0) 2013.02.24