日期:2014-05-20 浏览次数:20772 次
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class JTableDemo {
static Random ram = new Random(37);
public static void main(String[] args) {
final JTableDemo demo = new JTableDemo();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame = new JFrame();
frame.setSize(300, 200);
final JTable table = new JTable(demo.new MyTableModel(demo.getDataList(), demo.getColName()));
frame.getContentPane().add(table, BorderLayout.CENTER);
JPanel panel = new JPanel();
JButton subButton = new JButton("提交");
subButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MyTableModel tm = (MyTableModel)table.getModel();
List<Book> list = tm.getRowList();
list.add(demo.new Book("Book-" + ram.nextInt(10000), "123-"+ram.nextInt(10000), 123.00));
tm.setRowList(list);
tm.fireTableDataChanged();
}
});
JButton disButton;
panel.add(disButton = new JButton("显示"));
disButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "自己实现!!");
}
});
panel.add(subButton);
frame.getContentPane().add(panel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
public List<Book> getDataList(){
List<Book> list = new ArrayList<Book>();
for(int i = 0; i < 5; i++){
Book book = new Book("Book"+i, "201404180"+i, 10.0*i);
list.add(book);
}
return list;
}
public List<String> getColName(){
List<String> list = new ArrayList<String>();
list.add("Book Name");
list.add("ISIN");
list.add("Price");
return list;
}
@SuppressWarnings("serial")
class MyTableModel extends DefaultTableModel{
List<Book> rowList;
List<String> colList;
public void setRowList(List<Book> rowList){
this.rowList = rowList;
setDataVector(getRowDataFromList(), getColDataFromList());
}
MyTableModel(List<Book> rowList, List<String> colList){
this.rowList = rowList;
this.colList = colList;
setDataVector(getRowDataFromList(), getColDataFromList());
}
public List<Book> getRowList(){
return rowList ;
}
private Object[][] getRowDataFromList() {
Object[][] dataObj = null;
if(rowList != null && rowList.size() > 0){
dataObj = new Object[rowList.size()][colList.size()];
for (int row = 0; row < rowList.size(); row++) {
dataObj[row][0] = rowList.get(row).getBookName();
dataObj[row][1] = rowList.get(row).getISIN();
dataObj[row][2] = rowList.get(row).getPrice();
}
}
return dataObj;
}
private Object[] getColDataFromList() {
Object[] colObj = null;
if(colList !=null && colList.size() > 0){
colObj = new Object[colList.size()];
for (int col = 0; col < colList.size(); col++) {
colObj[col] = colList.get(col);
}
}
return colObj;
}
}
class Book{
String bookName;
String ISIN;
Double price;
Book(String bookName, String ISIN, Double price){
this.bookName = bookName;
this.ISIN = ISIN;
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;