当前位置: 技术问答>java相关
jtable的问题,分析代码,现场给分
来源: 互联网 发布时间:2015-09-16
本文导语: 程序如下: 1 请问如何隐藏表头,(例中是姓名,年龄和已婚) 2 如何锁死列宽 3 如何使各条记录不能被选中,(不能高亮显示) import javax.swing.*; import javax.swing.table.*; public class JTableTest extends JFrame { public stati...
程序如下:
1 请问如何隐藏表头,(例中是姓名,年龄和已婚)
2 如何锁死列宽
3 如何使各条记录不能被选中,(不能高亮显示)
import javax.swing.*;
import javax.swing.table.*;
public class JTableTest extends JFrame {
public static void main(String args[]) {
JTableTest Test = new JTableTest();
Test.setSize(300,200);
Test.setVisible(true);
}
public JTableTest() {
JTable jt = new JTable(new MyTableModel());
jt.setRowHeight(20);
getContentPane().add(new JScrollPane(jt));
}
}
class MyTableModel extends AbstractTableModel {
private String[] columns = {"姓名","年龄","已婚"};
private Object[][] data =
{{"王晓明asfsdfsdfsdf",new Integer(28),new Boolean(true)},
{"程晓华",new Integer(26),new Boolean(false)},
{"张小李",new Integer(35),new Boolean(true)},
{"林晓力",new Integer(18),new Boolean(false)}
};
public int getColumnCount() {
return (columns.length);
}
public int getRowCount() {
return (data.length);
}
//public String getColumnName(int col) {
// return (columns[col]);
//}
public Object getValueAt(int row, int col) {
return (data[row][col]);
}
public Class getColumnClass(int col) {
return (getValueAt(0,col).getClass());
}
public boolean isCellEditable( int row,int col) {
return (col > 0 );
}
public void setValueAt(Object value, int row, int col) {
if(col == 1) {
try {
data[row][col] = new Integer((String)value);
}
catch (NumberFormatException e) {
}
}else
data[row][col] = value;
fireTableCellUpdated(row,col);
}
}
1 请问如何隐藏表头,(例中是姓名,年龄和已婚)
2 如何锁死列宽
3 如何使各条记录不能被选中,(不能高亮显示)
import javax.swing.*;
import javax.swing.table.*;
public class JTableTest extends JFrame {
public static void main(String args[]) {
JTableTest Test = new JTableTest();
Test.setSize(300,200);
Test.setVisible(true);
}
public JTableTest() {
JTable jt = new JTable(new MyTableModel());
jt.setRowHeight(20);
getContentPane().add(new JScrollPane(jt));
}
}
class MyTableModel extends AbstractTableModel {
private String[] columns = {"姓名","年龄","已婚"};
private Object[][] data =
{{"王晓明asfsdfsdfsdf",new Integer(28),new Boolean(true)},
{"程晓华",new Integer(26),new Boolean(false)},
{"张小李",new Integer(35),new Boolean(true)},
{"林晓力",new Integer(18),new Boolean(false)}
};
public int getColumnCount() {
return (columns.length);
}
public int getRowCount() {
return (data.length);
}
//public String getColumnName(int col) {
// return (columns[col]);
//}
public Object getValueAt(int row, int col) {
return (data[row][col]);
}
public Class getColumnClass(int col) {
return (getValueAt(0,col).getClass());
}
public boolean isCellEditable( int row,int col) {
return (col > 0 );
}
public void setValueAt(Object value, int row, int col) {
if(col == 1) {
try {
data[row][col] = new Integer((String)value);
}
catch (NumberFormatException e) {
}
}else
data[row][col] = value;
fireTableCellUpdated(row,col);
}
}
|
1: 将JTable用JScrollPane来容纳即可,比如:
JScrollPane tableJScrollPane;
JTable jTable1;
……
jTable1 = ……;//表初始化
tableJScrollPane = new JScrollPane(table);//这样应该可以隐藏表头
2: 设定列宽,具体显示还得看显示大小比例
tcm = JTablel.getColumnModel();
int[] widths1 = {95,105,,125,125,150};//各列列宽设定,假设5列
for(i=0;i
JScrollPane tableJScrollPane;
JTable jTable1;
……
jTable1 = ……;//表初始化
tableJScrollPane = new JScrollPane(table);//这样应该可以隐藏表头
2: 设定列宽,具体显示还得看显示大小比例
tcm = JTablel.getColumnModel();
int[] widths1 = {95,105,,125,125,150};//各列列宽设定,假设5列
for(i=0;i