tableutils.java
来自「《j2ee开发全程实录》随书源码」· Java 代码 · 共 61 行
JAVA
61 行
package com.cownew.ctk.ui.swing;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class TableUtils
{
public static void hideColumn(JTable table, int index)
{
//得到列模型
TableColumnModel columnModel = table.getTableHeader().getColumnModel();
//从列模型中得到列顺序为index的列
TableColumn column = columnModel.getColumn(index);
column.setResizable(false);
column.setMinWidth(0);
column.setMaxWidth(0);
}
/**
* 得到表格选定行的制定列的值
* @param table 表格
* @param colId 列id
* @return
*/
public static Object getSelectedColValue(JTable table,String colId)
{
int rowIndex = table.getSelectedRow();
int columnIndex = table.getColumn(colId).getModelIndex();
return table.getModel().getValueAt(rowIndex, columnIndex );
}
/**
* 设定model的第rowindex行的colId列的值
* @param table
* @param rowIndex
* @param colId
* @return
*/
public static Object getValueAt(JTable table, int rowIndex,
String colId)
{
int colIndex = table.getColumn(colId).getModelIndex();
return table.getModel().getValueAt(rowIndex, colIndex);
}
/**
* 得到model的第rowindex行的colId列的值
* @param table
* @param value
* @param rowIndex
* @param colId
*/
public static void setValueAt(JTable table, Object value,
int rowIndex, String colId)
{
int colIndex = table.getColumn(colId).getModelIndex();
table.getModel().setValueAt(value, rowIndex, colIndex);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?