20100919_Dawning_JTable
package com.swing.demo; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import java.awt.Dimension; import java.awt.GridLayout; /*** * 基于Java图形界面JTable测试案例 * * @author Administrator * */ public class TableSortDemo extends JPanel { private boolean DEBUG = false; public TableSortDemo() { super(new GridLayout(1,0)); JTable table = new JTable(new MyTableModel()); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); /** * */ table.setAutoCreateRowSorter(true);//这句话是重点 //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this panel. add(scrollPane); } class MyTableModel extends AbstractTableModel { //设置每一列的列名 private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian" }; //设置列的值 private Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) }, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)}, }; public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } /* * * * JTable uses this method to determine the default renderer/ * * * editor for each cell. If we didn't implement this method, * * then the last column would contain text ("true"/"false"), * rather than a check box. */ public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } /* * Don't need to implement this method unless your table's * editable. * * */ public boolean isCellEditable(int row, int col) { if (col < 2) { return false; } else { return true; } } /* * Don't need to implement this method unless your table's * data can change. */ public void setValueAt(Object value, int row, int col) { if (DEBUG) { System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")"); } data[row][col] = value; if (DEBUG) { System.out.println("New value of data:"); printDebugData(); } } private void printDebugData() { 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(); } System.out.println("--------------------------"); } } private static void createAndShowGUI() { JFrame frame = new JFrame("TableSortDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TableSortDemo newContentPane = new TableSortDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.setSize(500,500); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
20100919_Dawning_JTable相关推荐
- 20100919_Dawning_JTable测试
package com.swing.demo; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSc ...
最新文章
- MixNet了解一下,来自Google Brain最新SOTA移动AI架构
- MaxCompute用户初体验
- ubuntu12.04下NFS链接开发板并测试交叉编译的第一个应用
- 每日一题(10)—— 数组与指针
- java责任链设计模式 订单_Java责任链设计模式实例分析
- 容器编排技术 -- 使用kubectl实现应用伸缩
- CTS(2)---CTS、CTS-V、GTS版本更新,及其注意事项
- 玩转VIM编辑器-vim的命令行
- PHP查看内存使用量
- bdd java_二元判断图BDD及其JAVA实现的应用与研究
- 20个PCB快捷键操作,提升绘图效率
- 软媒魔方 6.0.5 正式绿色版
- HTML CSS游戏官网网页模板——卡通的萌王游戏网页(13个页面)
- 2018的趋势与展望(上)——记罗振宇“时间的朋友2017”跨年演讲
- 从看脸到读心:深度理解人的视觉技术走到哪了?
- Elasticsearch系列—倒排索引原理
- 【企业微信实现免密登录以及发送消息(企业内部应用)】
- 各大主流招聘平台的优缺点和适合人群【总结】
- 人体工程学游戏椅行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
- matlab怎么导入数据格式,matlab导入excel表格数据代码,如何将excel中数据导入到matlab中...