1、在修改单元格的内容的时候,如果不双击,那么修改的字段是加在原有字段后面的,这一点很不好,相信大家都有同感。
在论坛里搜到的如何让JTable的一个单元格进入编辑状态,代码如下,可是我有点疑问.
private static void setEditing(JTable tb, int row, int col) {
if (!tb.isCellEditable(row,col))
return;
//tb.editCellAt(row, col);
JTextField jText = (JTextField) ( (DefaultCellEditor) tb.getCellEditor(row,col)).getComponent();
jText.requestFocus();
jText.selectAll();
}
public boolean editCellAt(int row,int column)如果 row 和 column 位置的索引在有效范围内,并且这些索引处的单元格是可编辑的,则以编程方式启动该位置单元格的编辑。
setEditing这个方法应该是自定义的吧,这个方法应该在哪个类里面定义,还是直接在当前面板中定义并使用?
还有什么更好的方法吗实现这一功能吗?
2、数据库的表格很多情况下是连接生成的,列的字段由SQL语句指定,那JTable如何增加一行空白的行和指定列名的空白列呢?
在论坛里搜到的如何让JTable的一个单元格进入编辑状态,代码如下,可是我有点疑问.
private static void setEditing(JTable tb, int row, int col) {
if (!tb.isCellEditable(row,col))
return;
//tb.editCellAt(row, col);
JTextField jText = (JTextField) ( (DefaultCellEditor) tb.getCellEditor(row,col)).getComponent();
jText.requestFocus();
jText.selectAll();
}
public boolean editCellAt(int row,int column)如果 row 和 column 位置的索引在有效范围内,并且这些索引处的单元格是可编辑的,则以编程方式启动该位置单元格的编辑。
setEditing这个方法应该是自定义的吧,这个方法应该在哪个类里面定义,还是直接在当前面板中定义并使用?
还有什么更好的方法吗实现这一功能吗?
2、数据库的表格很多情况下是连接生成的,列的字段由SQL语句指定,那JTable如何增加一行空白的行和指定列名的空白列呢?
JTable table = new JTable(data, columnNames) {
// Select the text when the cell starts editing
// a) text will be replaced when you start typing in a cell
// b) text will be selected when you use F2 to start editing
// c) text will be selected when double clicking to start editing
public boolean editCellAt(int row, int column, EventObject e) {
boolean result = super.editCellAt(row, column, e);
final Component editor = getEditorComponent();
if (editor != null && editor instanceof JTextComponent) {
if (e == null) {
((JTextComponent) editor).selectAll();
} else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
((JTextComponent) editor).selectAll();
}
});
}
}
return result;
}
};第二个问题我没接触过,DefaultTableColumnModel有一个addColumn(TableColumn aColumn)方法可以增加一列,
不过不清楚是否好用.ps: 在CSDN问题是得给分的,呵呵. 另外你的问题描述的也不清楚.
我现在还没有多少分,因为来的少,等有了一些分数就给帖子加分,不好意思啊,感谢楼上的这么无私!
model是JTable存数据的地方,如果是自己写model,怎么添一行,加一列自己就可以定义了.
比如你用Vector-Vector存数据,要加一行就容易了,添一个存储一行的数据就行了;加一列
稍微复杂一点,可能要遍历每一行,再在每一行添一列数据.
这个我也没用过,就是给你提点议.
DefaultTableModel就是用的Vector存数据,也有添加行列的方式,可以借鉴一下.