class MyTable extends JTable { public MyTable() { super(); } public MyTable(Vector v1, Vector v2) { super(v1,v2); } public boolean isCellEditable(int row, int column) { return false;//重写这个方法是为了让表的单元格不能被用户编辑!!! } }写了这个类以后怎么样呢? MyTable t = new MyTalbe();//他妈的构造函数都没有?
顺便鄙视一下SUN 做个Jtable连个禁输入的方法都没有 他以为他是做Excel?
jTable1.setModel(new MyModel());class MyModel extends DefaultTableModel{ public MyModel () { } public boolean isCellEditable(int row, int column){ return false; } }
楼主不要毛躁import java.util.Vector;import javax.swing.table.AbstractTableModel;public class StandardTableModel extends AbstractTableModel { /** * each element of this vector represents a single row of the table. Internally, a row is represented * by a Object[]. */ protected Vector<Object[]> rows = new Vector<Object[]>();
/** * array that holds information on the names for the table columns */ protected String[] colNames = null; /** * 设置表格列是否允许编辑 */ protected boolean[] editables = null;
public StandardTableModel(String[] colNames, boolean[] editables) { super(); this.colNames = colNames; this.editables = editables; } public int getColumnCount() { return colNames.length; } public int getRowCount() { return rows.size(); } public String getColumnName(int column) { return this.colNames[column]; }
/** * deletes all data from the table (also imforms the GUI) */ public void clearTable(){ int numRows = this.getRowCount(); this.rows.clear(); if (numRows >= 1) this.fireTableRowsDeleted(0, numRows - 1); }
/** * @param columnName name of column to get the index for * @return the index of the column with the given name */ public int findColumn(String columnName) { for ( int i=0; i<this.colNames.length; i++ ){ if (this.colNames[i].toLowerCase().equals(columnName.toLowerCase())) return i; } return -1; }
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { (this.rows.get(rowIndex))[columnIndex] = aValue; this.fireTableCellUpdated(rowIndex,columnIndex); }
public Object getValueAt(int rowIndex, int columnIndex) { return (this.rows.get(rowIndex))[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return editables[columnIndex]; }
public Class<?> getColumnClass(int columnIndex) { return getValueAt(0, columnIndex).getClass(); }
/** * simple method to add a row to the table. * (It better matches the given column nummer and column types!!) *@param newRow */ public void addRow(Object[] newRow){ this.rows.add(newRow); this.fireTableRowsInserted(this.getRowCount()-1,this.getRowCount()-1); } /** *@return array containing the names of the columns */ public String[] getColNames() { return colNames; } /** * Setting new column names will flush the table, if the new array has not the same length as the old one! *@param colNames array containing new column names */ protected void setColNames(String[] colNames) { if (this.colNames.length != colNames.length) this.clearTable(); this.colNames = colNames; } }
class MyTable extends JTable
{
public MyTable() {
super();
} public MyTable(Vector v1, Vector v2)
{
super(v1,v2);
} public boolean isCellEditable(int row, int column) {
return false;//重写这个方法是为了让表的单元格不能被用户编辑!!!
}
}写了这个类以后怎么样呢?
MyTable t = new MyTalbe();//他妈的构造函数都没有?
做个Jtable连个禁输入的方法都没有
他以为他是做Excel?
public MyModel () {
}
public boolean isCellEditable(int row, int column){
return false;
}
}
给我弄个带main的例子进来
追加500分
你瞧你那样
就知道baidu一搜然后挂这里来套分是吧?
{
DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(new Object[][] { { "119", "foo", "bar", "ja", "ko", "zh", "zh", "zh" },
{ "911", "bar", "foo", "en", "fr", "pt", "en", "fr", "pt" } }, new Object[] { "SNo.", "1", "2",
"Native", "2", "3", "2", "3" });
JTable treeTable = new JTable(dm){
public boolean isCellEditable(int row, int column) {
return false;
}
};
JFrame frame = new JFrame("TreeTable");
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
frame.getContentPane().add(new JScrollPane(treeTable));
frame.pack();
frame.show();
}
import java.awt.Container;import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;public class Test2 extends JApplet {
private JTextArea txt = new JTextArea(4, 20);
private int editRow=-1;
private JTable table;
// The TableModel controls all the data:
class DataModel extends AbstractTableModel {
Object[][] data = { { "one", "two", "three", "four" },
{ "five", "six", "seven", "eight" },
{ "nine", "ten", "eleven", "twelve" }, }; // Prints data when table changes:
class TML implements TableModelListener {
public void tableChanged(TableModelEvent e) {
txt.setText(""); // Clear it
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++)
txt.append(data[i][j] + " ");
txt.append("\n");
}
}
} public DataModel() {
addTableModelListener(new TML());
} public int getColumnCount() {
return data[0].length;
} public int getRowCount() {
return data.length;
} public Object getValueAt(int row, int col) {
return data[row][col];
} public void setValueAt(Object val, int row, int col) {
data[row][col] = val;
fireTableDataChanged();
} public boolean isCellEditable(int row, int col) {
return true; //这里写成true就可以编辑了.
}
} public void init() {
Container cp = getContentPane();
table = new JTable(new DataModel());
cp.add(new JScrollPane(table));
cp.add(BorderLayout.SOUTH, txt);
} public static void main(String[] args) {
run(new Test2(), 350, 200);
} public static void run(JApplet applet, int width, int height) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
} ///:~
{
DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(new Object[][] { { "119", "foo", "bar", "ja", "ko", "zh", "zh", "zh" },
{ "911", "bar", "foo", "en", "fr", "pt", "en", "fr", "pt" } }, new Object[] { "SNo.", "1", "2",
"Native", "2", "3", "2", "3" });
JTable treeTable = new JTable(dm){
public boolean isCellEditable(int row, int column) {
return false;
}
};
JFrame frame = new JFrame("TreeTable");
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
frame.getContentPane().add(new JScrollPane(treeTable));
frame.pack();
frame.show();
}
楼主【wenglingyu000】截止到2008-07-14 09:17:27的历史汇总数据(不包括此帖):
发帖的总数量:8 发帖的总分数:320 每贴平均分数:40
回帖的总数量:26 得分贴总数量:5 回帖的得分率:19%
结贴的总数量:3 结贴的总分数:130
无满意结贴数:0 无满意结贴分:0
未结的帖子数:5 未结的总分数:190
结贴的百分比:37.50 % 结分的百分比:40.63 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
楼主该结一些帖子了
/**
* each element of this vector represents a single row of the table. Internally, a row is represented
* by a Object[].
*/
protected Vector<Object[]> rows = new Vector<Object[]>();
/**
* array that holds information on the names for the table columns
*/
protected String[] colNames = null; /**
* 设置表格列是否允许编辑
*/
protected boolean[] editables = null;
public StandardTableModel(String[] colNames, boolean[] editables) {
super();
this.colNames = colNames;
this.editables = editables;
} public int getColumnCount() {
return colNames.length;
} public int getRowCount() {
return rows.size();
} public String getColumnName(int column) {
return this.colNames[column];
}
/**
* deletes all data from the table (also imforms the GUI)
*/
public void clearTable(){
int numRows = this.getRowCount();
this.rows.clear();
if (numRows >= 1)
this.fireTableRowsDeleted(0, numRows - 1);
}
/**
* @param columnName name of column to get the index for
* @return the index of the column with the given name
*/
public int findColumn(String columnName) {
for ( int i=0; i<this.colNames.length; i++ ){
if (this.colNames[i].toLowerCase().equals(columnName.toLowerCase())) return i;
}
return -1;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
(this.rows.get(rowIndex))[columnIndex] = aValue;
this.fireTableCellUpdated(rowIndex,columnIndex);
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (this.rows.get(rowIndex))[columnIndex];
} public boolean isCellEditable(int rowIndex, int columnIndex) {
return editables[columnIndex];
}
public Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
/**
* simple method to add a row to the table.
* (It better matches the given column nummer and column types!!)
*@param newRow
*/
public void addRow(Object[] newRow){
this.rows.add(newRow);
this.fireTableRowsInserted(this.getRowCount()-1,this.getRowCount()-1);
}
/**
*@return array containing the names of the columns
*/
public String[] getColNames() {
return colNames;
}
/**
* Setting new column names will flush the table, if the new array has not the same length as the old one!
*@param colNames array containing new column names
*/
protected void setColNames(String[] colNames) {
if (this.colNames.length != colNames.length)
this.clearTable();
this.colNames = colNames;
}
}
private boolean[] editables = { false, false, false, true};
private StandardTableModel model;
private JTable table;...model = new StandardTableModel(columnNames, editables);
table = new JTable(model);model.addRow(...);
明天的太阳还会升起来吗??
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout()); jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jTable1.setEnabled(false);//让表不可被编辑
jScrollPane1.setViewportView(jTable1);
getContentPane().add(jScrollPane1);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
}例子给你了。大家都注意点素质啊。别都骂开了。