科技时代新浪首页 > 科技时代 > 学园 > 正文

让界面更加绚丽 JavaSE6.0四种新功能(4)


http://www.sina.com.cn 2006年12月19日 09:07 天极yesky

  增强的拖放功能

  在Java SE 6中的拖放功能得到了增强,这主要表现在两个方面。

  ·可以定制拖放模式。

  可以在拖放的过程中加入其它的辅助信息。 首先需要通过JList、JTable等控件的setDropMode()方法来设置一个拖动模式。所有的控件都可以使用USER_SELECTION模式。这个模式在以前的Java SE版本中就有。这也是默认的拖放模式。

  JList、JTable和Jlist都支持ON模式,这个模式允许你将对象拖到其它项的上方。而INSERT模式允许将一个对象插入在其它项之间。而ON_OR_INSERT模式是前3种模式的组合。下面的代码将演示一个拖动的例子。

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TestDrapDrop
{
 public static void main(String args[])
 {
  JFrame f = new JFrame("拖放测试");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JPanel top = new JPanel(new BorderLayout());
  JLabel dragLabel = new JLabel("拖我:");
  JTextField text = new JTextField();
  text.setDragEnabled(true);
  top.add(dragLabel, BorderLayout.WEST);
  top.add(text, BorderLayout.CENTER);
  f.add(top, BorderLayout.NORTH);
  final JTree tree = new JTree();
  final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  tree.setTransferHandler(new TransferHandler()
  {
   public boolean canImport(TransferHandler.TransferSupport support)
   {
    if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop())
    {
     return false;
    }
    JTree.DropLocation dropLocation = (JTree.DropLocation) suppor.getDropLocation();
    return dropLocation.getPath() != null;
   }
   public boolean importData(TransferHandler.TransferSupport support)
   {
    if (!canImport(support))
    {
     return false;
    }
    JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
    TreePath path = dropLocation.getPath();
    Transferable transferable = support.getTransferable();
    String transferData;
    try
    {
     transferData = (String) transferable .getTransferData(DataFlavor.stringFlavor);
    }
    catch (IOException e)
    {
     return false;
    }
    catch (UnsupportedFlavorException e)
    {
     return false;
    }
    int childIndex = dropLocation.getChildIndex();
    if (childIndex == -1)
    {
     childIndex = model.getChildCount(path.getLastPathComponent());
    }
    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode( transferData);
    DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
    model.insertNodeInto(newNode, parentNode, childIndex);
    TreePath newPath = path.pathByAddingChild(newNode);
    tree.makeVisible(newPath);
    tree.scrollRectToVisible(tree.getPathBounds(newPath));
    return true;
   }
  });
  JScrollPane pane = new JScrollPane(tree);
  f.add(pane, BorderLayout.CENTER);
  JPanel bottom = new JPanel();
  JLabel comboLabel = new JLabel("DropMode");
  String options[] ={ "USE_SELECTION", "ON", "INSERT", "ON_OR_INSERT" };
  final DropMode mode[] =
   {DropMode.USE_SELECTION, DropMode.ON, DropMode.INSERT, DropMode.ON_OR_INSERT };
  final JComboBox combo = new JComboBox(options);
  combo.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    int selectedIndex = combo.getSelectedIndex();
    tree.setDropMode(mode[selectedIndex]);
   }
  });
  bottom.add(comboLabel);
  bottom.add(combo);
  f.add(bottom, BorderLayout.SOUTH);
  f.setSize(300, 400);
  f.setVisible(true);
 }

  图9为拖动程序的运行界面。在上面的文本框里输入相应的文本,然后将其选择再拖动到下方的树中。

让界面更加绚丽JavaSE6.0四种新功能(4)
图9 拖动界面

[上一页] [1] [2] [3] [4]

本文导航:
·带有排序和过滤功能的JTable
·增强的JTabbedPane组件
·增强的打印功能
·增强的拖放功能

发表评论 _COUNT_条

爱问(iAsk.com)



评论】【论坛】【收藏此页】【 】【多种方式看新闻】【下载点点通】【打印】【关闭




科技时代意见反馈留言板 电话:010-82628888-5595   欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

Copyright © 1996 - 2006 SINA Inc. All Rights Reserved

新浪公司 版权所有