现在的位置: 首页 > 综合 > 正文

Swing中经常会遇到的若干问题——JTable(持续更新)

2016年06月07日 ⁄ 综合 ⁄ 共 4919字 ⁄ 字号 评论关闭

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/40955213

(1)让组件在屏幕中央显示

	public static void setContainerCenter(Container container) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int screenWidth = screenSize.width;
		int screenHight = screenSize.height;
		container.setLocation((screenWidth - container.getWidth()) / 2, (screenHight - container.getHeight()) / 2);
	}

(2)隐藏Jtable中的某一列

	public static void hideColByLocation(int col, JTable table){
		DefaultTableColumnModel dcm = (DefaultTableColumnModel) table.getColumnModel();
		dcm.getColumn(col).setPreferredWidth(0);
		dcm.getColumn(col).setMaxWidth(0);
		dcm.getColumn(col).setMinWidth(0);
	}

(3)Jtable中定位到当前新增加的行

	public static void focusCurrentRow(JTable table, List<Object> objs) {
		int last = objs.size() - 1;
		table.setRowSelectionInterval(last, last);
		table.changeSelection(last, 0, false, false);
	}

4)Jtable中行的上移、下移操作

	public static void eventOnMoveUpOrDowmBySelectedRow(JTable table, String str, List<String> nameList) {
		int[] selectedRows = table.getSelectedRows();
		for (int i = 0; i < selectedRows.length - 1; i++) {
			if(selectedRows[i + 1] - selectedRows[i] != 1){
				return;
			}
		}
		
		List<String> swapSelectedList = null;
		if ("上移".equals(str)) {
			if(selectedRows.length == 1){
				swapSelectedList = swap(nameList, selectedRows[0], selectedRows[0] - 1);
				setNameListAndModel(swapSelectedList, table);
				table.getSelectionModel().setSelectionInterval(selectedRows[0] - 1, selectedRows[0] - 1);	
			}else{
				swapSelectedList = swapUp(nameList, selectedRows);
				setNameListAndModel(swapSelectedList, table);
				table.getSelectionModel().setSelectionInterval(selectedRows[0]-1, selectedRows[selectedRows.length-1]-1);
			}
		} else if ("下移".equals(str)) {
			if(selectedRows.length == 1){
				swapSelectedList = swap(nameList, selectedRows[0], selectedRows[0] + 1);
				setNameListAndModel(swapSelectedList, table);
				table.getSelectionModel().setSelectionInterval(selectedRows[0] + 1, selectedRows[0] + 1);
			}else{
				swapSelectedList = swapDown(nameList, selectedRows);
				setNameListAndModel(swapSelectedList, table);
				table.getSelectionModel().setSelectionInterval(selectedRows[0]+1, selectedRows[selectedRows.length-1]+1);
			}
		}
	}

	/**
	 * {方法功能中文描述}上移多行
	 */
	private static  List<String> swapUp(List<String> list, int[] selectedRows) {
		if (selectedRows.length >= list.size() || selectedRows.length == 0 || selectedRows[0] == 0) {
			return list;
		} else {
			String obj = list.get(selectedRows[0] - 1);
			for (int pos : selectedRows) {
				list.set(pos - 1, list.get(pos));
			}
			list.set(selectedRows[0] + selectedRows.length - 1 , obj);
			return list;
		}
	}
	
		
	/**
	 * {方法功能中文描述}下移多行
	 */
	private static List<String> swapDown(List<String> list, int[] selectedRows) {
		if (selectedRows.length >= list.size() || selectedRows.length == 0 || selectedRows[selectedRows.length-1] == list.size()-1) {
			return list;
		} else {
			String obj = list.get(selectedRows[selectedRows.length-1] + 1);
			for (int i = selectedRows.length-1; i >= 0; i--) {
				list.set(selectedRows[i] + 1, list.get(selectedRows[i]));
			}
			list.set(selectedRows[0] , obj);
			return list;
		}
	}
	
	
	/**
	 * {方法功能中文描述} 交换两行的位置 
	 */
	private static List<String> swap(List<String> list, int fromRow, int toRow) {
		if (toRow >= list.size() || toRow < 0) {
			return list;
		} else {
			String obj = list.get(fromRow);
			list.set(fromRow, list.get(toRow));
			list.set(toRow, obj);
			return list;
		}
	}
	
	private static void setNameListAndModel(List<String> NameList, JTable table) {
		/**重新设置model中数据**/
		table.getModel().setNames(NameList);
		/**数据变化**/
		table.getModel().fireTableDataChanged(); 
		table.setModel(uiModel);
	}

(5)打开面板选中Jtree第一个节点并展开

	public static void setFirstNode2Selected(DefaultTreeModel model,JPanel panel, JTree currTree){ 
		/**通过树根获取其第一个孩子**/ 
		TreeNode firstChild = (TbTreeNode) ((DefaultMutableTreeNode)panel.getLeftContentPanel().getTreeModel().getRoot()).getFirstChild(); 
		Object userObject = firstChild.getUserObject(); 
		/**通过VO获取Node对象**/
		TreeNode childnode = panel.getTreeModel().getNodeByVo(userObject); 
		if(childnode!=null){ 
			childnode.setCanBeSelected(true); /**设置可被选中**/
			childnode.setSelectState(TreeNode.SELECTED); /**设置被选中**/
		} 
		/**设置选中的路径状态**/ 
		currTree.getSelectionModel().setSelectionPath(new TreePath(childnode.getPath())); 
	}

(6)JTable行的多种选择模式

	setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); //一次选择一个或多个连续的索引范围。
	setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);  //一次选择一个连续的索引范围。
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);   //一次选择一个列表索引。

(7)JTable不可编辑

             调用AbstractTableModel中的setWriteable()方法,true为可以编辑,false为不可编辑

(8)JTable中数据发生变化时

             调用AbstractTableModel中 fireTableDataChanged()方法进行处理

(9)JTable显示设置

     //设置列名称
     private static final String[] colNames = new String[] { "A", "B", "C", "D"};

     //设置列名称显示
     public String getColumnName(int column) {
	  return colNames[column];
      }
		   	
     //设置列数目
     public int getColumnCount() {
	  return colNames.length;
     }
		   	
     //是否可编辑
     public boolean isCellEditable(int rowIndex, int columnIndex) {
          if(!isWriteable){
		return false; 
	  }
	  if(isWriteable){
	    //设置第二列可以编辑
	     if(columnIndex == 1){
		return true;
	     }
	  }
	   return false;
      }
		   	
		   	
      //填充数据模型
      public Object getValueAt(int rowIndex, int columnIndex) {
	  //假设从某一list中获取数据对象,遍历该list
	  if (list != null && list.size() > 0) {
	      for (int i = 0;i< list.size(); i++) {
		 switch (columnIndex) {
		     case 0:
	        		return ""; //返回值自行设定
	 	     case 1:
	              		return "";
		     case 2:
				return "";
		     case 3:
				return "";
		     case 4:
				return "";
		}
	     }
	  }
	     return null;
	}
	
	
	//修改表中数据时
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
	     if(columnIndex == 1){
		  //操作...
	     }
	}

抱歉!评论已关闭.