How do I press, add, NOT RELEASE and drag?

When I press the mouse-button in my panel, my program will add a component under the mousepointer. I would like to start dragging this component, without first releasing the button and then grabbing the component by pressing a second time. Can it be done?
This code does not do it:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AddNDragTest extends JFrame {
    public AddNDragTest() {
        super("Click to add a box");
        setPreferredSize(new Dimension(700,500));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        pane = new JPanel();
        pane.setLayout(null);
        // Add a box under the mousepointer when mouse is pressed in 'pane'
        pane.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                Box box = new Box();
                box.addMouseListener(boxListener);
                box.addMouseMotionListener(boxListener);
                // Center the box on the mousepointer
                box.setLocation(me.getX() - box.getWidth() / 2,me.getY() - box.getHeight() / 2);
//                box.dragging = true;
                pane.add(box);
                repaint();
        add(pane);
    JPanel pane;
    MouseAdapter boxListener = new MouseAdapter() {
        public void mousePressed(MouseEvent me) {
            Box box = (Box) ( me.getSource() );
            box.mx0 = me.getX();
            box.my0 = me.getY();
//            box.dragging = true;
//        public void mouseReleased(MouseEvent me) {
//            Box box = (Box) ( me.getSource() );
//            box.dragging = false;
//        public void mouseMoved(MouseEvent me) {
//            Box box = (Box) ( me.getSource() );
//            if (box.dragging)
//                box.setLocation(box.getX() + me.getX() - box.mx0,box.getY() + me.getY() - box.my0);
        public void mouseDragged(MouseEvent me) {
            Box box = (Box) ( me.getSource() );
            box.setLocation(box.getX() + me.getX() - box.mx0,box.getY() + me.getY() - box.my0);
    class Box extends JPanel {
        public Box() {
            super();
            setSize(50,50);
            setBackground(Color.YELLOW);
            setBorder(BorderFactory.createLineBorder(Color.GREEN));
            setOpaque(true);
        // Store the location where the box was pressed upon to pretend
        // that it is beeing held at that point
        int mx0, my0;
        // Attempting to drag the box by listening for mouseMoved don't work
//        boolean dragging;
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Create and set up the window
                AddNDragTest test = new AddNDragTest();
                //Center and display the window..
                test.pack();
                Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
                Dimension siz = test.getSize();
                test.setLocation(( scr.width - siz.width ) / 2,( scr.height - siz.height ) / 2);
                test.setVisible(true);
}No hurry here, this is a hobby.

Well, in addition to adding the MouseListener to the "pane" you need to add a MouseMotionListener to handle the drag events as they will still be generated on the "pane", not the Box. The drag events would simply set the location of the newly added Box.
By the way don't use "Box" for the class name, there is already a Swing class using that name so it can be confusing.

Similar Messages

  • JButton pressed, but not released and actionperformed never called

    Hi,
    I have a simple JButton, with one action listener that does its thing (or at least should do because it is never called). I also added a mouse listener to trace the issue further.
    Here's what happen. I have another component (a JTextField) with a focus listener. If the compoent looses the focus, I want to have a chance to ask a quick confirmation question (JOptionPane) before continuing to do someting else (in that case, process the action on the button).
    So the focus is in the text field and I click on the button. Here's what happen:
    - The button gets a mouse pressed event
    - The text field looses the focus and the button gains it.
    - The text field's focus lost handler shows a JOptionPane => focus goes on the option pane
    - The option pane is confirmed, but the rest stops.
    I get NO mouse release event on my button, the action performed is also lost and my button remains "half pressed" (when i hover the mouse pointer over it, the button is rendered lowered ).
    My guess is that the option pane comes too quick and that the mouse release event is transfered to the option pane instead of the button I first clicked, causing the actionperformed to be ignored as a side effect.
    Any ideas or suggestions ?
    Thanks.

    I get NO mouse release event on my button, the action
    performed is also lost and my button remains "half
    pressed" (when i hover the mouse pointer over it, the
    button is rendered lowered ).
    basically that's a bug in the button's internal state handling.
    To get an idea about how to fix it, you might want to read my article "Make Buttons Respect InputVerifiers" at
    http://www.mycgiserver.com/~Kleopatra/swing/swingentry.html
    Though it's rather old the issue is not solved (until 1.5b2) It's only applicable if you have tight control over the L&F.
    Greetings
    Jeanette

  • How to keep pressing add row button to add new row

    Hello,
    I know that you can set the add row process to add many rows at once by pressing the button one time, but can anybody show me how to make it possible to keep hitting the add row button and it keep adding a new line?
    Right now it will only add one line and when I press the button again it does nothing.
    Thank you

    Hi, I have the same (or close) problem. I was trying to create a tubular form with ADDROW button and I did it. During creation wizard I did specify a table where added data should go. The solution I was looking for was to have only one empty row when I load my page and be able to add more empty rows one by one by clicking on ADD_ROW button. The problem is that when page loads it is already filled up with data from table (which I did specify during creation and where data should be writen to) and only when I press on ADD_ROW it will addd an empty one.
    Is there a way to do it the way I wanted?
    Thanks

  • IPhoto and Frontrow: can not click and drag after using frontrow slide show

    Recently I received my new MacBook Pro, I started using iPhoto and noticed the following problem:
    1. Open iPhoto and perform normal tasks, click and drag photos, etc.
    2. Close i-Photo
    3. Open Frontrow Slide Show(Pictures)- play for a few seconds
    4. Close Frontrow
    5. Reopen i-Photo and photos will not move(click/drag)
    6. Close i-Photo/reopen and all is fine
    Anyone else???
    Thanks!
    MacBook Pro   Mac OS X (10.4.9)  

    I discover that there is no parameter to set slide duration
    Using the Finder, drag all the images from the folder containing the images on to the Navigator pane ion Keynote, this will place the images centred on an individual slide
    with at least one image selected in the Navigator, select all, (command A)
    Inspector > Animate > Start transition: in the drop down menu, select:   Automatically
    seethe delay to the time you want the image to appear on screen, you can select individual times by selecting one slide at a time
    and no choice of slide transition and timing
    Inspector > Animate, click Add an Effect
    The Inspector is the side panel on the right hand side of the canvas
    All Apple applications have a very useful Help system located on the Menu bar > Help

  • Mouse not clicking and dragging

    My mouse pointer responds well to moving around the screen and double ckicking etc. However when I try to click and drag it 'loses' the object (file name or card in Freecell) or will not select all that I shift& drag. All the drivers are up to date. I have removed, waited and then replaced the Bluetooth USB fitting in the back of the computer and the batteries are fresh. Any Ideas?
    HP TouchSmart 520-1010ukH
    Microsoft mouse model MG-0982
    Windows 7
    This question was solved.
    View Solution.

    Hello again @itsmagicvi,
    If you have tried a different mouse on your computer and it is not happening to that mouse I suspect that your Microsoft Mouse is the problem and suggest you contact Microsoft on their forums at Microsoft Community as they may have addressed this issue with their hardware before.
    I hope I have answered your question to your satisfaction. Thank you for posting on the HP Forums. Have a great day!
    Please click the "Thumbs Up" on the bottom right of this post to say thank you if you appreciate the support I provide!
    Also be sure to mark my post as “Accept as Solution" if you feel my post solved your issue, it will help others who face the same challenge find the same solution.
    Dunidar
    I work on behalf of HP
    Find out a bit more about me by checking out my profile!
    "Customers don’t expect you to be perfect. They do expect you to fix things when they go wrong." ~ Donald Porter

  • How do you simply add (software) drums and bass to an acoustic track?!

    I am pulling my hair out just trying to find instructions on how to add drums and bass to an acoustic track.
    Keep in mind the track was laid down by me with a metronome so it's pretty spot-on; however a small adjustment with the timing needs to be made here and there.  So, how do you make these adjustments?
    Your help is very much appreciated!

    Thanks for your reply. If your meaning my user folder, it is also located on the SSD. But If I understand you correctly, I should put all data files, music, videos, recordings, pictures on the HD. I should use the SSD for installing all programs. For example, Lion OS, iLife, iWork, Recording software, Parallels etc.
    The only concern is some of these programs are large. Plus, with Parallels, I have some windows based software to install and this would occupy evenmore SSD space.
    Is it possible to copy the Lion OS from the SSD to a partitioned HD? This way I can install Parallels and a Windows OS on one of the 1TB HD partitions. For example, create 3 partitions on the 1TB HD. Use Partition 1 for MacOS parallels and windows OS, Partition 2 for  Mac and windows data and Partition 3 for Archivable data.
    Does this seem like a logical process? I appreciate your input.

  • Why have apple not released and update to fix the wifi problems

    I have a ipad mini all was working fine untill apple released the update to OS6 now we are in OS7 and still no fix?
    now i have tried ALL off the work rounds that have been postedand they do NOT work.
    in order to get the mini to conect to any network it needs to be sat on top of the router this isnt just my router this is all routers im on virgin tryed with BT and Sky and mcdonalds,costa,starbucks ect ect...
    I feel ripped off a wifi tablet with no working wifi is crap. A very expensive glass coaster
    WHY HAVE YOU NOT FIXED THIS ???????

    This is a user-to-user forum. Apple doesn't live here (except checking every so often to enforce the TOU). You can rant, rave and vent all you want, won't change a thing and may get you canned.
    This is your specific Wi-Fi problem. Most users have had no issues. My two iGadgets have gone from 6.0.1 to 7.0.4 with all the intermediates with no problem. Same for all my colleagues Minis, Maxis and other gadgets. Which leads to...
    If you care to provide more specific technical details, we can strive to help you the best we can given our experiences.

  • How to get Display to not "stretch" and therefore distort photos

    My wife has 20" Cinema Display hooked up to an IBM Thinkpad running Windows XP. Because the Display is "stretched" (extra wide), it distorts photos, making everything appear wider than it really is.
    Is there any way to get the display to not be stretched? This can be done on my Macbook in System Preferences, but the display settings in the PC do not have that option.

    You need to update the drivers on the Thinkpad to a version that supports widescreen resolutions. If you post the exact model of Thinkpad, someone will be able to point you in the right direction.

  • I think it's time to add note taking and study cards features to all the books in ibooks. (Not just textbook)

    i can't see any reason, why not?

    We are fellow users here on these forums, if you want to leave feedback for Apple then you can do so via this page : http://www.apple.com/feedback/ibooks_ios.html

  • Why can i not click and drag firefox to a new moniter without minimizing first?

    says it in question

    hello, this should not be happening - can you replicate this problem when you lauch firefox in safemode once?
    [[Troubleshoot extensions, themes and hardware acceleration issues to solve common Firefox problems]]

  • How can I code for a click and drag to progress forward/backward in timeline

    I'm working on a project, the goal is to have a .swf animation like this.
    The functionality I'm looking for is:
    1. I need my animation to make one full rotation and stop,
    and then
    2. I need to fix my AS3 so you have to CLICK/HOLD/DRAG to progress forward and backward through the timeline, but only when your holding the mouse button down.
    Currently the code progresses the frame forward and backward in relation to the mouseX position.
    Here is my current code:
    import flash.events.MouseEvent;
    var startPosition:Number=mouseX;
    var delayTime=10;
    gotoAndStop(1);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouse);
    function onMouse(e:MouseEvent):void{
        stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouse);
        var currentPosition:Number=mouseX;
        if(mouseX-startPosition<=0){
            nextFrame();
        }else{
            prevFrame();
        setTimeout(setListener, delayTime);
    function setListener(){
        startPosition=mouseX;
    stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouse);
    any help would be appreciated,
    Thanks.

    Found a thread much like this one that solved all my problems
    Here the link to anyone who might be pulling their hair out trying to figure this out in the future
    http://forums.adobe.com/thread/570903?tstart=0
    Thanks Adobe Forums

  • How to block maintenance order from releasing if cost is high

    Hi Experts
    If cost of maintenance order is more than Rs.100000(e.g); it should not release and if less than this value then it can be released.
    Plz help me can i do this in SAP PM and how ?
    Regards
    Pooja

    Hi ,
    In SAP PM there is a functionality known as Permits.You can create a Functional Permit and assign it your order type to acheive the requirement.Create a characteristics with currency format(data type) in the additional field give table name CSEVPERMIT and field name  PKOSTEN.Create a class type 049 and assign the characteristics created to the class.Give this class in the permit create (Tcode IPMD).Assign the range of the values by clicking on the classification sequence like > 50000Rs etc.In the order release option choose 2Permit must be issued else error message.
    assign this permit to the order type or the Technical object.
    Check it out and let know.
    Manisha

  • BP Status 'Not Released'

    Hi,
      when creating a business partner, there is a check box 'not released' in Status tab in sap gui. If I check the check box and save without error, will this BP be replicated to ECC?
      As I have a business scenario that BP will be created in CRM and replicated to ECC. But it cannot be used for any business transaction until the BP is approved.
      Therefore, I need to know if BP flagged with that status, will it be replicated to ECC?
      secondly, in ECC, how do i uncheck the check box if the BP is approved and ready to be used. (Once it is uncheck from Not Release, this should replicate to CRM and update the status, so that all business transaction are ready to be used with this BP).
      Is this workable?
    cheers,
    ginnie

    Hi Ginnie  ,
    To answer your First Question ,
    Yes, Eventhough you set the Not Released under status tab of general data, Still that BP will flow to ECC. It will give you a popup saying *Business partner  has status "Not Released" * and a Yellow Exclamation mark will come next to BP number and it will flow to ECC.
    At ECC side , you can use this in transactions too .
    Instead of this, I will suggest you to use Central Lock which is just above the Not released check box, to achieve your functionality. If you use this, BP will be transfered to ECC but system will not allow you to use it in any transaction.
    So you can achieve your functionality as follows :
    1. Create a BP in CRM with Central Lock Tick on.
    2. BP will flow to ECC with this tick on.(As a result Central lock, No transactions are allowed for this BP)
    3. Once BP is been approved by an approver in CRM, Reset this tick in CRM , which automatically will flow in ECC
    4 And now you are able to use this BP in transactions at ECC side
    Hope I am able to clear all your doubts. Please revert to me if you need more help in this.
    Regards,
    Nikhil

  • In ME2N  report for PO- How to get or add Vendor name in ALV grid output

    Hl Everyone
    How to get  or add Vendor Name and payment terms in the ALV grid output for the follwing reports like ME2N and ME2V.
    cuurently i am in 4.7 E version.
    Kindly suggest..........
    thanks in advance
    Regards
    Prashanth

    Hi Pankaj
    I knew that vendor name field is avaiable in ECC versions, but how to get the same field(vendor name) in 4.7 E vesion.
    Kindly suggest
    Regards
    Prashanth

  • How do I move iCloud notes to ON MY MAC?

    Short story:
    I had to move my notes from ON MY MAC to iCloud in order to move them from a broken system to a new system. Now, I am on the new system and am trying to firgure out how to move my notes out of iCloud and keep them ON MY MAC. If I disable iCLoud syncing I can start adding notes to ON MY MAC. However, when I resync to iCloud it merges my notes. This is really weird, because I know that with my earlier system I had the two coexist.
    How do a move my notes? -- and I have more than 3,000, so it is not copy and paste.
    Long-story:
    The first time I upgraded from Snow Leopard to Mountain Lion none of my notes came through. There seems to have been some kind of big in the upgrade process. So, I went back to my Snow Leopard system (yes, I made a copy before upgrading) and this time upgraded to Lion before going to Mountain Lion. When I did that I was able to get my notes into ON MY MAC in Mountain Lion.
    So, on Monday I had my disk die. This was a bummer, but fortunately I had a Time Machine backup from earlier in the day. So, I boot into the recovery partition and restore from TIme Machine. Only problem is that this time Mail was unable to restore any of my local mail boxes (I have about 300,000 messages and got a message about "running out of space" before stopiing -- even though I had 125GB available. In addition to the Mail problem, there were problems with missing fonts. Apparently, there are still quite a few bugs in Time Machine for Mountain Lion and who knows what else I discover as time progressed.
    Fortunately, I still had my trusty old Snow Leopard image on a separate partition. I decided to do another Mountain Lion Install and migrate from that partition. However, I knew that I was going to lose a bunch of recent notes that I created in Mountain Lion, so I copied all of my ON MY MAC notes to iCloud. I even tested to ensure that I could move them back. I also copied a bunch of recent couments to a thumb drive to restore later as well. So, I spend forever doing the install and migrate and in the end everything was working -- except I can't get my notes back to ON MY MAC.

    I finally solved the problem. I upgraded all of my stuff  to Snow Leopard. Now, I can have some notes local and others on me.com. It really wasn't worth it "living on the edge" with Mountain Lion -- especially with a work machine.
    For anybody else thinking of going bacl to SnapLeoppard from Mountain Lion it was fairly painless. Here are a few pointers:
    I was able to archive email boxes in Mountain Lion and import them into Snow Leopard with no problem.
    If you keep around a Moutain Lion partition you can easily move documents back to your SL disk using the SL finder.
    Moving notes was a not as bad as I though. I simply shared  all the new notes in ML and emailed them to myself. You have to do one Note as a time which wa a bit of a drag though. However, from SL you just drag the email messages into the Notes and they are fully usable. I did go an copy them one by one into real notes, biut I'm not sure that that is necessary.

Maybe you are looking for

  • HT3819 I have a new HP computer and would like to transfer my iTunes library from my old PC

    I have been trying to transfer my iTunes library from my old PC to our new HP PC (Windows 8.1). We did back up our old PC files to an external harddrive but the iTunes files are 'jumbled'when I open them on the new PC and not in any format - only str

  • Skip inspection at the time of Goods Receipt

    I want to skip inspection since it has been done at source. How can I do it at the time of GR ? I know about activating indicator 'Source Inspection - No GR', but I don't find it in MIGO screen.

  • XY graph cursor legend in another VI

    Hi, I have a XY graph on my main VI. On the main VI there is little space for the cursor legend so I would like to move it into a separate frontpanel that can be popped up on demand. I managed to use the graphs reference to get all information about

  • Opening HCM PDF forms from UWL

    Dear Friends, I am working in an assignment which involves the following Portal version 7.01 SP3 HCM Process and Forms ESS and MSS UWL I am doing the following steps 1. logged in as a manager 2.Go to Team 3. Initiated the 'start process' to change th

  • How to define the size of each column of a GridLayout in HTMLB?

    Hi there, I think everything is in the subject. It seems it is not possible. The GridLayout.setWidth() refers to the grid in general and it is not possible to select a GridLayout column (in order to apply to it a setWidth()). When I do a GridLayoutCe