问题描述
只允许表头第一列或者某一列不可拖动,其他列可以拖动,请问怎么实现?JTable的所有列是默认可以拖动的,JTableHeadertableHeader=JTable.getTableHeader();tableHeader.setReorderingAllowed(false);//表示所有的列都不可以拖动
解决方案
解决方案二:
一般是两个table拼接而成
解决方案三:
不动的这一列一般是ID这种字段,如果用两个table的话,数据模型实现的数据可能会不同步.
解决方案四:
你难道不会手动编码让它们数据同步吗?据我所知,所谓锁定列这种ui都是拼接的
解决方案五:
你说的编码我没有试过,应该可以吧,想问你一个问题,怎么在一个JScrollpane中添加两个JTable?
解决方案六:
该回复于2011-01-29 08:46:37被版主删除
解决方案七:
那像你所说的,用一个JScrollPane将两个JTable包含起来,然后还可以将第二个JTable可以拖动,这怎么实现呢?但是JScrollPane可以加两个JTable吗?不可以吧
解决方案八:
看看是不是你要的packagecom.test;importjava.awt.BorderLayout;importjava.awt.Dimension;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.Arrays;importjava.util.Vector;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTable;importjavax.swing.JViewport;importjavax.swing.table.DefaultTableColumnModel;importjavax.swing.table.DefaultTableModel;importjavax.swing.table.TableColumn;importjavax.swing.table.TableColumnModel;publicclassTestTableextendsJFrame{privatestaticfinallongserialVersionUID=-3606700961317828681L;privateJTablerowHeaderTable=null;privateDefaultTableModelrowHeaderTableModel=null;privateJTabletable=null;privateDefaultTableModeltableModel=null;publicTestTable(){super("Test");init();}privatevoidinit(){this.getContentPane().setLayout(newBorderLayout());this.getContentPane().add(getCenterPanel(),BorderLayout.CENTER);this.getContentPane().add(getSouthPanel(),BorderLayout.SOUTH);this.pack();this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}privateJPanelgetSouthPanel(){JPanelpanel=newJPanel();panel.setLayout(newFlowLayout(FlowLayout.CENTER));JButtonaddBtn=newJButton("Add");addBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){addCaseRow();}});JButtondelBtn=newJButton("Delete");delBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){removeCaseRows(table.getSelectedRows());}});panel.add(addBtn);panel.add(delBtn);returnpanel;}privateJScrollPanegetCenterPanel(){rowHeaderTable=newJTable(getRowHeaderTableModel(),getRowHeaderTableColumnModel());rowHeaderTable.getTableHeader().setReorderingAllowed(false);rowHeaderTable.setColumnSelectionAllowed(false);rowHeaderTable.getTableHeader().setReorderingAllowed(false);rowHeaderTable.getTableHeader().setResizingAllowed(false);rowHeaderTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);rowHeaderTable.setEnabled(false);JViewportjv=newJViewport();jv.setView(rowHeaderTable);intheight=newDouble(rowHeaderTable.getMaximumSize().getHeight()).intValue();jv.setPreferredSize(newDimension(50,height));JScrollPanescrollPanel_main=newJScrollPane();table=newJTable(getTableModel(),getTableColumnModel());table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);scrollPanel_main.getViewport().add(table);scrollPanel_main.setRowHeader(jv);scrollPanel_main.setCorner(JScrollPane.UPPER_LEFT_CORNER,rowHeaderTable.getTableHeader());returnscrollPanel_main;}privateDefaultTableModelgetRowHeaderTableModel(){rowHeaderTableModel=newDefaultTableModel(0,1){privatestaticfinallongserialVersionUID=1986093475481012619L;publicbooleanisCellEditable(introw,intcolumn){returnfalse;}};returnrowHeaderTableModel;}privateDefaultTableModelgetTableModel(){tableModel=newDefaultTableModel(0,2){privatestaticfinallongserialVersionUID=3325101167983738710L;publicbooleanisCellEditable(introw,intcolumn){returntrue;}};returntableModel;}privateTableColumnModelgetTableColumnModel(){TableColumnModelcolumnModel=newDefaultTableColumnModel();for(inti=0;i<2;i++){columnModel.addColumn(getTableColumns()[i]);}returncolumnModel;}privateTableColumn[]getTableColumns(){TableColumn[]tableColumns=newTableColumn[2];tableColumns[0]=newTableColumn(0,200,null,null);tableColumns[0].setHeaderValue("Test1");tableColumns[1]=newTableColumn(1,200,null,null);tableColumns[1].setHeaderValue("Test2");returntableColumns;}privateTableColumnModelgetRowHeaderTableColumnModel(){DefaultTableColumnModelrowHeaderColumnModel=newDefaultTableColumnModel();rowHeaderColumnModel.addColumn(getRowNumberColumn());returnrowHeaderColumnModel;}privateTableColumngetRowNumberColumn(){TableColumnrowNumberColumn=newTableColumn(0,50,null,null);rowNumberColumn.setHeaderValue("ID");returnrowNumberColumn;}privatevoidaddCaseRow(){introwId=tableModel.getRowCount()+1;Vector<String>headerVC=newVector<String>();headerVC.add(String.valueOf(rowId));Vector<String>dataVC=newVector<String>();dataVC.add("Test1:"+String.valueOf(rowId));dataVC.add("Test2:"+String.valueOf(rowId));rowHeaderTableModel.addRow(headerVC);tableModel.addRow(dataVC);}privatevoidremoveCaseRows(int[]rowIndex){if(rowIndex.length>0){Arrays.sort(rowIndex);for(inti=rowIndex.length-1;i>=0;i--){tableModel.removeRow(rowIndex[i]);rowHeaderTableModel.removeRow(rowIndex[i]);}}updateRowHeader();}privatevoidupdateRowHeader(){for(inti=0;i<rowHeaderTableModel.getRowCount();i++){rowHeaderTableModel.setValueAt(i+1,i,0);}}publicstaticvoidmain(String[]args){newTestTable();}}
解决方案九:
你说的是jtable的前面几列的冻结,具体的没有做过,不过你可以试试jctable,jctable这边设置挺简单的。