JTable不可编辑文字

wenglingyu000 2008-04-01 03:26:20
如何使JTable不可编辑文字(双击的那个)



我已经在baidu上搜了1天了什么DataTableModel 又什么重写Model类 重写JTable类
P都不管用

会的人进来写个简单的例子 不会的人 或者现在打开搜索页面正在找答案的人 你就别答了

见例子给分
...全文
1064 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
haPPPPy 2012-10-08
  • 打赏
  • 举报
回复
谢谢[Quote=引用 39 楼 的回复:]

Java code

public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {

……
[/Quote]
dunkin123 2010-04-24
  • 打赏
  • 举报
回复
楼主什么JB人啊,操,问问题还这么牛F,SB,你就是香港和澳门的合体----<肛门>
yfyht 2008-07-18
  • 打赏
  • 举报
回复 1

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;
}

例子给你了。大家都注意点素质啊。别都骂开了。
hjay0715 2008-07-18
  • 打赏
  • 举报
回复
table.setEnabled(false);
binxigogo 2008-07-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 aoyihuashao 的回复:]
Java codejTable1.setModel(newMyModel());classMyModelextendsDefaultTableModel{publicMyModel () {
}publicbooleanisCellEditable(introw,intcolumn){returnfalse;
}
}
[/Quote]

正解
妄旺 2008-07-17
  • 打赏
  • 举报
回复
BS下LZ这TD,居然还有人回
明天的太阳还会升起来吗??
Ji秋风 2008-07-17
  • 打赏
  • 举报
回复
private String[] columnNames = { "col1", "col2", "col3", "col4"};
private boolean[] editables = { false, false, false, true};

private StandardTableModel model;
private JTable table;

...

model = new StandardTableModel(columnNames, editables);
table = new JTable(model);

model.addRow(...);
Ji秋风 2008-07-17
  • 打赏
  • 举报
回复
楼主不要毛躁

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;
}
}



龙四 2008-07-17
  • 打赏
  • 举报
回复
table.setEnabled(false);
lsm0622 2008-07-14
  • 打赏
  • 举报
回复
啥都不会你还玩你妈逼啊 我操 就你这鸡巴态度 我连你妈都不想操了 傻逼似的
  • 打赏
  • 举报
回复
public static void main(String[] args)
{
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();
}
java1314521 2008-05-05
  • 打赏
  • 举报
回复
谢谢14楼的那位哥们
cpoysy 2008-04-27
  • 打赏
  • 举报
回复


import java.awt.BorderLayout;
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);
}
} ///:~

wenyuanyu145 2008-04-27
  • 打赏
  • 举报
回复
鄙视楼主
coolhty 2008-04-07
  • 打赏
  • 举报
回复
LZ哎,你这种性格,后果会很严重的哎,技术再好,公司不会要。。你当老板没人给你做事。。

尽量改改吧。。

做人低调点。
coolhty 2008-04-07
  • 打赏
  • 举报
回复
LZ哎,你这种性格,后果会很严重的哎,技术再好,公司不会要。。你当老板没人给你做事。。

尽量改改吧。。

做人低调点。
rypgood 2008-04-04
  • 打赏
  • 举报
回复
你丫什么态度
哥哥还就不在乎你那点分
就你那态度那叫学习?
别说500分,500w也没几个鸟你
死去吧
林g 2008-04-03
  • 打赏
  • 举报
回复


楼主,态度太差.

盒子danbo 2008-04-03
  • 打赏
  • 举报
回复
鄙视楼主
盒子danbo 2008-04-03
  • 打赏
  • 举报
回复
鄙视楼主
加载更多回复(18)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧