代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
使用拖放对列表框进行重新排序
作者:
DDT
/ 发布于
2012/6/12
/
524
使用拖放对列表框进行重新排序
<div>public class ReorderableJList extends JList implements DragSourceListener, DropTargetListener, DragGestureListener { static DataFlavor localObjectFlavor; static { try { localObjectFlavor = new DataFlavor (DataFlavor.javaJVMLocalObjectMimeType); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); } } static DataFlavor[] supportedFlavors = { localObjectFlavor }; DragSource dragSource; DropTarget dropTarget; Object dropTargetCell; int draggedIndex = -1;</div> <div> public ReorderableJList () { super(); setCellRenderer (new ReorderableListCellRenderer()); setModel (new DefaultListModel()); dragSource = new DragSource(); DragGestureRecognizer dgr = dragSource.createDefaultDragGestureRecognizer (this, DnDConstants.ACTION_MOVE, this); dropTarget = new DropTarget (this, this); } <div> // DragGestureListener public void dragGestureRecognized (DragGestureEvent dge) { System.out.println ("dragGestureRecognized"); // find object at this x,y Point clickPoint = dge.getDragOrigin(); int index = locationToIndex(clickPoint); if (index == -1) return; Object target = getModel().getElementAt(index); Transferable trans = new RJLTransferable (target); draggedIndex = index; dragSource.startDrag (dge,Cursor.getDefaultCursor(), trans, this); } // DragSourceListener events public void dragDropEnd (DragSourceDropEvent dsde) { System.out.println ("dragDropEnd()"); dropTargetCell = null; draggedIndex = -1; repaint(); } public void dragEnter (DragSourceDragEvent dsde) {} public void dragExit (DragSourceEvent dse) {} public void dragOver (DragSourceDragEvent dsde) {} public void dropActionChanged (DragSourceDragEvent dsde) {} // DropTargetListener events public void dragEnter (DropTargetDragEvent dtde) { System.out.println ("dragEnter"); if (dtde.getSource() != dropTarget) dtde.rejectDrag(); else { dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); System.out.println ("accepted dragEnter"); } } public void dragExit (DropTargetEvent dte) {} public void dragOver (DropTargetDragEvent dtde) { // figure out which cell it's over, no drag to self if (dtde.getSource() != dropTarget) dtde.rejectDrag(); Point dragPoint = dtde.getLocation(); int index = locationToIndex (dragPoint); if (index == -1) dropTargetCell = null; else dropTargetCell = getModel().getElementAt(index); repaint(); } <div>public void drop (DropTargetDropEvent dtde) { System.out.println ("drop()!"); if (dtde.getSource() != dropTarget) { System.out.println ("rejecting for bad source (" + dtde.getSource().getClass().getName() + ")"); dtde.rejectDrop(); return; } Point dropPoint = dtde.getLocation(); int index = locationToIndex (dropPoint); System.out.println ("drop index is " + index); boolean dropped = false; try { if ((index == -1) || (index == draggedIndex)) { System.out.println ("dropped onto self"); dtde.rejectDrop(); return; } dtde.acceptDrop (DnDConstants.ACTION_MOVE); System.out.println ("accepted"); Object dragged = dtde.getTransferable().getTransferData(localObjectFlavor); // move items - note that indicies for insert will // change if [removed] source was before target System.out.println ("drop " + draggedIndex + " to " + index); boolean sourceBeforeTarget = (draggedIndex < index); System.out.println ("source is" + (sourceBeforeTarget ? "" : " not") + " before target"); System.out.println ("insert at " + (sourceBeforeTarget ? index-1 : index)); DefaultListModel mod = (DefaultListModel) getModel(); mod.remove (draggedIndex); mod.add ((sourceBeforeTarget ? index-1 : index), dragged); dropped = true; } catch (Exception e) { e.printStackTrace(); } dtde.dropComplete (dropped); } <div> public void dropActionChanged (DropTargetDragEvent dtde) {} public static void main (String[] args) { JList list = new ReorderableJList (); DefaultListModel defModel = new DefaultListModel(); list.setModel (defModel); String[] listItems = { "Chris", "Joshua", "Daniel", "Michael", "Don", "Kimi", "Kelly", "Keagan" }; Iterator it = Arrays.asList(listItems).iterator(); while (it.hasNext()) defModel.addElement (it.next()); // show list JScrollPane scroller = new JScrollPane (list, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); JFrame frame = new JFrame ("Checkbox JList"); frame.getContentPane().add (scroller); frame.pack(); frame.setVisible(true); } <div>class RJLTransferable implements Transferable { Object object; public RJLTransferable (Object o) { object = o; } public Object getTransferData(DataFlavor df) throws UnsupportedFlavorException, IOException { if (isDataFlavorSupported (df)) return object; else throw new UnsupportedFlavorException(df); } public boolean isDataFlavorSupported (DataFlavor df) { return (df.equals (localObjectFlavor)); } public DataFlavor[] getTransferDataFlavors () { return supportedFlavors; } } <div>class ReorderableListCellRenderer extends DefaultListCellRenderer { boolean isTargetCell; boolean isLastItem; public ReorderableListCellRenderer() { super(); } public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean hasFocus) { isTargetCell = (value == dropTargetCell); isLastItem = (index == list.getModel().getSize()-1); boolean showSelected = isSelected & (dropTargetCell == null); return super.getListCellRendererComponent (list, value, index, showSelected, hasFocus); } public void paintComponent (Graphics g) { super.paintComponent(g); if (isTargetCell) { g.setColor(Color.black); g.drawLine (0, 0, getSize().width, 0); } } } <div>} </div>
试试其它关键字
拖放对列表框
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3