Jtable冻结

網上看到的凍結Table列的代碼,是通過兩個Table來實現。現有一個第三方控件包jsuit,有jpvTableView控件,可以實現凍結的功能,但是沒有凍結表頭。

 

  1. importjava.awt.BorderLayout;
  2. importjava.awt.event.WindowAdapter;
  3. importjava.awt.event.WindowEvent;
  4. importjavax.swing.*;
  5. importjavax.swing.event.ListSelectionEvent;
  6. importjavax.swing.event.ListSelectionListener;
  7. importjavax.swing.table.AbstractTableModel;
  8. publicclassJtableTestextendsJFrame{
  9. privatestaticfinallongserialVersionUID=1L;
  10. Object[][]data;
  11. Object[]column;
  12. JTablefixedTable,table;
  13. publicJtableTest(){
  14. super("FixedColumnExample");
  15. setSize(400,150);
  16. data=newObject[][]{{"1","11","A","","","","",""},
  17. {"2","22","","B","","","",""},
  18. {"3","33","","","C","","",""},
  19. {"4","44","","","","D","",""},
  20. {"5","55","","","","","E",""},
  21. {"6","66","","","","","","F"}};
  22. column=newObject[]{"fixed1","o","a","b","c","d","e","f"};
  23. AbstractTableModelfixedModel=newAbstractTableModel(){
  24. privatestaticfinallongserialVersionUID=1L;
  25. publicintgetColumnCount(){
  26. return1;
  27. }
  28. publicintgetRowCount(){
  29. returndata.length;
  30. }
  31. publicStringgetColumnName(intcol){
  32. return(String)column[col];
  33. }
  34. publicObjectgetValueAt(introw,intcol){
  35. returndata[row][col];
  36. }
  37. };
  38. AbstractTableModelmodel=newAbstractTableModel(){
  39. privatestaticfinallongserialVersionUID=1L;
  40. publicintgetColumnCount(){
  41. returncolumn.length-2;
  42. }
  43. publicintgetRowCount(){
  44. returndata.length;
  45. }
  46. publicStringgetColumnName(intcol){
  47. return(String)column[col+1];
  48. }
  49. publicObjectgetValueAt(introw,intcol){
  50. returndata[row][col+1];
  51. }
  52. publicvoidsetValueAt(Objectobj,introw,intcol){
  53. data[row][col+1]=obj;
  54. }
  55. publicbooleanCellEditable(introw,intcol){
  56. returntrue;
  57. }
  58. };
  59. fixedTable=newJTable(fixedModel);
  60. fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  61. fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  62. fixedTable.getSelectionModel().addListSelectionListener(
  63. newListSelectionListener(){
  64. publicvoidvalueChanged(ListSelectionEventarg0){
  65. checkSelection(true);
  66. }
  67. });
  68. table=newJTable(model);
  69. table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  70. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  71. table.getSelectionModel().addListSelectionListener(
  72. newListSelectionListener(){
  73. publicvoidvalueChanged(ListSelectionEventarg0){
  74. checkSelection(false);
  75. }
  76. });
  77. JViewportviewport=newJViewport();
  78. viewport.setView(fixedTable);
  79. viewport.setPreferredSize(fixedTable.getPreferredSize());
  80. JScrollPanescroll=newJScrollPane(table);
  81. scroll.setRowHeaderView(viewport);
  82. scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER,fixedTable
  83. .getTableHeader());
  84. getContentPane().add(scroll,BorderLayout.CENTER);
  85. }
  86. privatevoidcheckSelection(booleanisFixedTable){
  87. intfixedSelectedIndex=fixedTable.getSelectedRow();
  88. intselectedIndex=table.getSelectedRow();
  89. if(fixedSelectedIndex!=selectedIndex){
  90. if(isFixedTable){
  91. table.setRowSelectionInterval(fixedSelectedIndex,
  92. fixedSelectedIndex);
  93. }else{
  94. fixedTable
  95. .setRowSelectionInterval(selectedIndex,selectedIndex);
  96. }
  97. }
  98. }
  99. publicstaticvoidmain(String[]args){
  100. JtableTestframe=newJtableTest();
  101. frame.addWindowListener(newWindowAdapter(){
  102. publicvoidwindowClosing(WindowEvente){
  103. System.exit(0);
  104. }
  105. });
  106. frame.setVisible(true);
  107. }
  108. }

你可能感兴趣的:(C++,c,swing,F#,C#)