如题
sun的java tutorial有相关的例子,去下载来看吧!
简单讲一下吧。
使用 DefaultCellEditor。
package pced.edit;
import java.awt.*;
import javax.swing.*;
import pced.common.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.io.*;
import com.borland.dbswing.JdbTable;
import java.util.List;
/**
* [機 能] order scenario output table model class<p>
*/
class OutFileTableModel extends javax.swing.table.DefaultTableModel
{
/**
* table header name
*/
private static String[] strName = null;
/**
* table field content
*/
private static String[][] strField = null;
/**
* [機 能] table cell can or not edit <p>
*/
public boolean isCellEditable(int nRowIndex, int nColIndex)
{
if(nColIndex == 2)
{
return true;
}
else
{
return false;
}
}
/**
* [機 能] set table content <p>
*/
public static void setTabContent(String[][] strFieldF, String[] strNameF)
{
strName = strNameF;
strField = strFieldF;
}
/**
* [機 能] get Column Count<p>
*/
public int getColumnCount()
{
return strName.length;
}
/**
* [機 能] get row count<p>
*/
public int getRowCount()
{
return strField.length;
}
/**
* [機 能] get table value at row and colunm<p>
*/
public Object getValueAt(int nRow, int nCol)
{
return strField[nRow][nCol];
}
/**
* [機 能] get table colunm name<p>
*/
public String getColumnName(int nCol)
{
return strName[nCol];
}
/**
* [機 能] get table colunm name<p>
*/
public Class getColumnClass(int nC)
{
return getValueAt(0, nC).getClass();
}
/**
* [機 能] set table colunm and row value<p>
*/
public void setValueAt(Object oValue, int nRow, int nCol)
{
if(nCol == 2)
{
for(int i = 0; i < strField.length; i++)
{
if(i != nRow)
{
if(oValue.equals(strField[i][2]))
{
return;
}
}
}
}
if(((String)oValue).length() <= 0)
{
return;
}
strField[nRow][nCol] = (String)oValue;
fireTableCellUpdated(nRow, nCol);
}
}
public class OrderOutputDlg extends JDialog
{
public void initTable(String[] strName, PcedOutScenarioList list)
{
listInput = list;
int nSize = list.size();
String[][] strField = new String[nSize][4];
for(int i = 0; i < nSize; i++)
{
strField[i][0] = list.getItem(i).getID();
strField[i][1] = list.getItem(i).getType();
strField[i][2] = list.getItem(i).getFileName();
strField[i][3] = String.valueOf(i);
}
OutFileTableModel.setTabContent(strField, strName);
OutFileTableModel tableModel = new OutFileTableModel();
jTable1 = new JdbTable(tableModel);
jTableBak = new JdbTable(strField, strName);
jTable1.setAutoscrolls(false);
jTable1.setRequestFocusEnabled(false);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
jTable1.setRowHeaderVisible(false);
jTable1.setSmartColumnWidths(false);
jTable1.setAutoSelection(false);
jTable1.setShowVerticalLines(true);
jTable1.setRowSelectionAllowed(true);
jTable1.setShowHorizontalLines(true);
int[] nCol = {3, 4};
jTable1.setHiddenColumns(nCol);
jTable1.setSelectionMode(jTable1.getSelectionModel().MULTIPLE_INTERVAL_SELECTION);
JTableHeader hdr = jTable1.getTableHeader();
hdr.setReorderingAllowed(false);
jScrollPane1.getViewport().add(jTable1, null);
this.jButtonOk.setEnabled(false);
}
}
public class NumberCellEditor
extends JSpinner implements TableCellEditor
{
......
SpinnerNumberModel pageModel = new SpinnerNumberModel(0, 0, 10000, 1);
this.setModel(pageModel);
......
}
table.setDefaultEditor(Integer.class,new NumberCellEditor());