JTable如何实现添加、删除、修改表中的数据?
解决方案 »
- Java一个线程中传递数组时抛出空指针异常,疑似数组没传进来?求助,谢谢!
- 数据类型问题求解~
- 简单加密问题。
- FCKeditor在weblogic7配置问题?弄了两天还不行!!!555555555555555
- (新手)谁能给些java小项目实践的题目我啊?
- 请教:如何用wsad实现一个caidan
- 有关Java证书的一个问题
- 有什么工具能够分析程序中用到的类和方法?
- jPanel键盘监听会卡
- String forwardPage=new ForumAction().excute(request,response);这句怎么理解?
- java中如何判断一个线程是否存在?
- 使JMF支持RM,实现rm/rmvb文件的网络传输和播放?
DefaultTableModel model = new DefaultTableModel(cells, columnNames);
随便挑个例子你看下吧
/**
@version 1.01 2004-08-22
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;/**
This program demonstrates how to sort a table column.
Double-click on a table columm's header to sort it.
*/
public class TableSortTest
{
public static void main(String[] args)
{
JFrame frame = new TableSortFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
This frame contains a table of planet data.
*/
class TableSortFrame extends JFrame
{
public TableSortFrame()
{
setTitle("TableSortTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // set up table model and interpose sorter DefaultTableModel model = new DefaultTableModel(cells, columnNames);
final SortFilterModel sorter = new SortFilterModel(model); // show table final JTable table = new JTable(sorter);
add(new JScrollPane(table), BorderLayout.CENTER); // set up double click handler for column headers table.getTableHeader().addMouseListener(new
MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
// check for double click
if (event.getClickCount() < 2) return; // find column of click and
int tableColumn = table.columnAtPoint(event.getPoint()); // translate to table model index and sort
int modelColumn = table.convertColumnIndexToModel(tableColumn);
sorter.sort(modelColumn);
}
});
} private Object[][] cells =
{
{ "Mercury", 2440.0, 0, false, Color.yellow },
{ "Venus", 6052.0, 0, false, Color.yellow },
{ "Earth", 6378.0, 1, false, Color.blue },
{ "Mars", 3397.0, 2, false, Color.red },
{ "Jupiter", 71492.0, 16, true, Color.orange },
{ "Saturn", 60268.0, 18, true, Color.orange },
{ "Uranus", 25559.0, 17, true, Color.blue },
{ "Neptune", 24766.0, 8, true, Color.blue },
{ "Pluto", 1137.0, 1, false, Color.black }
}; private String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous", "Color" }; private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 200;
}/**
This table model takes an existing table model and produces a new model that sorts the rows
so that the entries in a given column are sorted.
*/
class SortFilterModel extends AbstractTableModel
{
/**
Constructs a sort filter model.
@param m the table model whose rows should be sorted
*/
public SortFilterModel(TableModel m)
{
model = m;
rows = new Row[model.getRowCount()];
for (int i = 0; i < rows.length; i++)
{
rows[i] = new Row();
rows[i].index = i;
}
} /**
Sorts the rows.
@param c the column that should become sorted
*/
public void sort(int c)
{
sortColumn = c;
Arrays.sort(rows);
fireTableDataChanged();
} // Compute the moved row for the three methods that access model elements public Object getValueAt(int r, int c) { return model.getValueAt(rows[r].index, c); } public boolean isCellEditable(int r, int c) { return model.isCellEditable(rows[r].index, c); } public void setValueAt(Object aValue, int r, int c)
{
model.setValueAt(aValue, rows[r].index, c);
} // delegate all remaining methods to the model public int getRowCount() { return model.getRowCount(); }
public int getColumnCount() { return model.getColumnCount(); }
public String getColumnName(int c) { return model.getColumnName(c); }
public Class getColumnClass(int c) { return model.getColumnClass(c); } /**
This inner class holds the index of the model row
Rows are compared by looking at the model row entries
in the sort column.
*/
private class Row implements Comparable<Row>
{
public int index;
public int compareTo(Row other)
{
Object a = model.getValueAt(index, sortColumn);
Object b = model.getValueAt(other.index, sortColumn);
if (a instanceof Comparable)
return ((Comparable) a).compareTo(b);
else
return a.toString().compareTo(b.toString());
}
} private TableModel model;
private int sortColumn;
private Row[] rows;
}
http://blog.csdn.net/mq612/archive/2006/08/26/1123819.aspx