java中表格加滚动条,java表格JTable实例(带滚动条,内嵌选
择框)
importjavax.swing.JTable;importjavax.swing.table.AbstractTableModel;importjavax.swing.JScrollPane;importjavax.swing.JFrame; java.awt.*;import java.awt.event.*;public class JTableDemo extendsJFrame {private boolean DEBUG = true;public
JTableDemo() { //实现构造⽅法
super("RecorderOfWorkers"); //⾸先调⽤⽗类JFrame的构造⽅法⽣成⼀个窗⼝
MyTableModel myModel = new MyTableModel();//myModel存放表格的数据
JTable table = new JTable(myModel);//表格对象table的数据来源是myModel对象
table.setPreferredScrollableViewportSize(new Dimension(500, 70));//表格的显⽰尺⼨//产⽣⼀个带滚动条的⾯板
JScrollPane scrollPane = newJScrollPane(table);//将带滚动条的⾯板添加⼊窗⼝中
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {//注册窗⼝
public voidwindowClosing(WindowEvent e) {
}
});
}//把要显⽰在表格中的数据存⼊字符串数组和Object数组中
class MyTableModel extendsAbstractTableModel {//表格中第⼀⾏所要显⽰的内容存放在字符串数组columnNames中
final String[] columnNames = { "First Name", "Position", "Telephone","MonthlyPay", "Married"};//表格中各⾏的内容保存在⼆维数
组data中
final Object[][] data ={
{"Wangdong", "Executive", "010********", new Integer(5000),new Boolean(false) },
{"LiHong", "Secretary", "010********", new Integer(3500),new Boolean(true) },
{"LiRui", "Manager", "010********", new Integer(4500),new Boolean(false) },
{"ZhaoXin", "Safeguard", "010********", new Integer(2000),new Boolean(true) },
{"ChenLei", "Salesman", "010********", new Integer(4000),new Boolean(false) } };//下述⽅法是重写AbstractTableModel中的
⽅法,其主要⽤途是被JTable对象调⽤,以便在表格中正确的显⽰出来。程序员必须根据采⽤的数据类型加以恰当实现。//获得列的数⽬public intgetColumnCount() {returncolumnNames.length;
}//获得⾏的数⽬
public intgetRowCount() {returndata.length;
}//获得某列的名字,⽽⽬前各列的名字保存在字符串数组columnNames中
public String getColumnName(intcol) {returncolumnNames[col];
}//获得某⾏某列的数据,⽽数据保存在对象数组data中
public Object getValueAt(int row, intcol) {returndata[row][col];
}//判断每个单元格的类型
public Class getColumnClass(intc) {return getValueAt(0, c).getClass();
}//将表格声明为可编辑的
public boolean isCellEditable(int row, intcol) {if (col < 2) {return false;
}else{return true;
}
}//改变某个数据的值
public void setValueAt(Object value, int row, intcol) {if(DEBUG) {
System.out.println("Setting value at " + row + "," +col+ " to " + value + " (an instance of "
+ Class() + ")");
}if (data[0][col] instanceof Integer && !(value instanceofInteger)) {try{
data[row][col]= String());
fireTableCellUpdated(row, col);
}catch(NumberFormatException e) {
JOptionPane.showMessageDialog(JTableDemo.this, "The \""
+getColumnName(col)+ "\" column accepts only integer values.");
}
}else{
data[row][col]=value;
fireTableCellUpdated(row, col);
}if(DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}private voidprintDebugData() {int numRows =getRowCount();int numCols =getColumnCount();for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");for (int j = 0; j < numCols; j++) {
System.out.print(" " +data[i][j]);
}
System.out.println();jfinal项目实例
}
System.out.println("--------------------------");
}
}public static voidmain(String[] args) {
JTableDemo frame= newJTableDemo(); frame.pack();
frame.setVisible(true);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。