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相关推荐

  1. 20100919_Dawning_JTable测试

    package com.swing.demo; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSc ...

最新文章

  1. MixNet了解一下,来自Google Brain最新SOTA移动AI架构
  2. MaxCompute用户初体验
  3. ubuntu12.04下NFS链接开发板并测试交叉编译的第一个应用
  4. 每日一题(10)—— 数组与指针
  5. java责任链设计模式 订单_Java责任链设计模式实例分析
  6. 容器编排技术 -- 使用kubectl实现应用伸缩
  7. CTS(2)---CTS、CTS-V、GTS版本更新,及其注意事项
  8. 玩转VIM编辑器-vim的命令行
  9. PHP查看内存使用量
  10. bdd java_二元判断图BDD及其JAVA实现的应用与研究
  11. 20个PCB快捷键操作,提升绘图效率
  12. 软媒魔方 6.0.5 正式绿色版
  13. HTML CSS游戏官网网页模板——卡通的萌王游戏网页(13个页面)
  14. 2018的趋势与展望(上)——记罗振宇“时间的朋友2017”跨年演讲
  15. 从看脸到读心:深度理解人的视觉技术走到哪了?
  16. Elasticsearch系列—倒排索引原理
  17. 【企业微信实现免密登录以及发送消息(企业内部应用)】
  18. 各大主流招聘平台的优缺点和适合人群【总结】
  19. 人体工程学游戏椅行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  20. matlab怎么导入数据格式,matlab导入excel表格数据代码,如何将excel中数据导入到matlab中...

热门文章

  1. swing java awt_Java awt和Swing的区别
  2. Git与SVN的区别Git常用命令
  3. 那些人不可以喝茶叶茶
  4. IFIT2022-智能城市环境中的医疗物联网:基于量子同态加密的医疗成像架构
  5. Vue--引入字体图标的方式
  6. 车辆销售系统用例_销售模块用例图
  7. 8项物质解读:奥氏体、马氏体、渗碳体、珠光体、贝氏体、魏氏组织、铁素体、莱氏体.
  8. 体育品牌争夺体育明星前必读的12条法则
  9. 2022暑期实习笔试题总结(网易,华为,360,美团)
  10. Android软键盘挡住输入框,设置adjustResize还无效,解决方案