001 /* 002 * Copyright (c) 2005, romain guy (romain.guy@jext.org) and craig wickesser (craig@codecraig.com) 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 006 * 007 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 008 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 009 * * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 010 * 011 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 012 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 013 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 014 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 015 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 016 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 017 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 018 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 019 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 020 * POSSIBILITY OF SUCH DAMAGE. 021 */ 022 023 package net.java.swingfx.jdraggable.demo; 024 025 import java.awt.BorderLayout; 026 import java.awt.Dimension; 027 import java.awt.event.ActionEvent; 028 import java.awt.event.ActionListener; 029 030 import javax.swing.JButton; 031 import javax.swing.JFrame; 032 import javax.swing.JLabel; 033 import javax.swing.JPanel; 034 import javax.swing.JTextField; 035 036 import net.java.swingfx.jdraggable.DefaultDraggableManager; 037 import net.java.swingfx.jdraggable.DragPolicy; 038 import net.java.swingfx.jdraggable.DraggableManager; 039 040 041 /** 042 * A simple class which shows how JDraggable can be used. 043 * 044 * @author craig 045 * @since v0.1 046 * <br> 047 * $Header: /cvs/swingfx/docs/api/src-html/net/java/swingfx/jdraggable/demo/Main.html,v 1.1 2005/06/23 00:24:52 codecraig Exp $ 048 */ 049 public class Main { 050 public static void main(String[] args) { 051 // Create a Container, in this case a JPanel 052 JPanel draggableContainer = new JPanel(); 053 054 // Register the container as the "draggable container" with a DraggableManager 055 // in this example the DefaultDraggableManager is used. 056 // REMEMBER, you must register the container prior to adding any components to it!! 057 DraggableManager draggableManager = new DefaultDraggableManager(draggableContainer); 058 059 // set a STRICT Drag Policy, so that only components implementing 060 // the Draggable interface can be dragged. Try chaning this to DragPolicy.DEFAULT 061 // or DragPolicy.OPEN for a variation. 062 draggableManager.setDragPolicy(DragPolicy.STRICT); 063 064 // Create a panel which implements the Draggable interface 065 DraggablePanel dragPanel = new DraggablePanel(); 066 dragPanel.setPreferredSize(new Dimension(150, 100)); 067 068 // Create a label which implements the Draggable interface 069 DraggableLabel dragLabel = new DraggableLabel("Drag Me!"); 070 071 // Create a normal Swing JLabel 072 JLabel plainLabel = new JLabel("Plain Label"); 073 074 // Create a normal Swing JButton, try chaning the Drag Policy (above) 075 // to something like DragPolicy.OPEN...and watch the button become 076 // draggable!! 077 JButton b = new JButton("Click"); 078 079 // add an ActionListener to prove the button can be dragged and still 080 // receive it's events 081 b.addActionListener(new ActionListener() { 082 public void actionPerformed(ActionEvent e) { 083 System.out.println("Clicked.."); 084 } 085 }); 086 087 // A normal Swing JTextField 088 JTextField jtf = new JTextField(); 089 jtf.setPreferredSize(new Dimension(100, 20)); 090 091 // Add all the components to the "draggable container" 092 draggableContainer.add(dragPanel); 093 draggableContainer.add(dragLabel); 094 draggableContainer.add(plainLabel); 095 draggableContainer.add(b); 096 draggableContainer.add(jtf); 097 098 // create a frame to display our "draggable container" 099 JFrame f = new JFrame(); 100 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 101 f.getContentPane().add(draggableContainer, BorderLayout.CENTER); 102 f.setSize(800, 600); 103 f.setVisible(true); 104 } 105 }