Doubt in swing-dragging

How to a drag a component build in to the container. Suppose if I constructed a button within the panel it should be moved along with the mouse dragged event and on mouse released it should be placed in that area.In short how should I drag and drop the component?
Thanks in advance
javasans

Try this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class TestTextArea extends JFrame
     JScrollPane scrollPane;
     JTextArea textArea;
     ImageIcon image;
     public TestTextArea()
          JPanel panel = new JPanel();
          setContentPane( panel );
          panel.setLayout( new BorderLayout() );
          image = new ImageIcon("????.jpg");
          textArea = new JTextArea( "one two", 10, 30 )
               public void paintComponent(Graphics g)
                    Point p = scrollPane.getViewport().getViewPosition();
                    g.drawImage(image.getImage(), p.x, p.y, null);
                    super.paintComponent(g);
          textArea.setOpaque(false);
          textArea.setFont( new Font("monospaced", Font.PLAIN, 12) );
          scrollPane = new JScrollPane( textArea );
          panel.add( scrollPane );
     public static void main(String[] args)
          TestTextArea frame = new TestTextArea();
          frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
          frame.pack();
          frame.setVisible(true);
}

Similar Messages

  • A doubt in SWING event handling

    Hi all,
    I'm a beginner in JAVA studying SWING. I learned that if we want to handle an event for a event source(say JButton) we should implement corresponding Event Listener(say ActionListener) .
    I also understood that we should register the Listener with that event source.
    With that knowledge I tried this program
    import javax.swing.*;
    import java.awt.event.*;
    public class SimpleGui2 implements MouseListener,ActionListener {
       SimpleGui2 insGui;
       JButton button= new JButton("Click me");
         public static void main(String[] args) {
              SimpleGui2 gui = new SimpleGui2();
              gui.start();
       public void start() {
            JFrame frame = new JFrame();
            //button.addMouseListener(this);   
            //button.addActionListener(this);
            button.addActionListener(insGui);
            frame.getContentPane().add(button);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300,300);
            frame.setVisible(true);
       public void actionPerformed(ActionEvent ae) {
            button.setText("Triggered");
       public void mouseClicked(MouseEvent me) {
          button.setText("I have been clicked");
       public void mouseEntered(MouseEvent me) { }
       public void mouseExited(MouseEvent me) { }
       public void mousePressed(MouseEvent me) { }
       public void mouseReleased(MouseEvent me) { }  
    }but It's is not working as I expected.
    or precisely why when we clicked the actionPerformed/mouseClicked method is not invoked ?
    if i change the same with argument "this" it's working .
    I guess "this" refers to current object (which is also of type SimpleGui2 which implemented ActionListener)
    Can any one please clarify me what happens behind the scenes ?
    Thanks in advance

    first of all, you never initialize insGui. YOu just declare it, and never use it.
    SimpleGui2 insGui;However, you dont need it anyways. As you suspected, using 'this' when you add your actionListener is what you need.
    button.addActionListener(insGui);
    Can any one please clarify me what happens behind the scenes ?Well my professor always said it was 'black magic'. All I know is that you click a button with an event listener, and an event is fired doing whatever you want it to do. For example, if I want my event to print "Hey" then when I click the button, "Hey" is printed. Simple enough eh? You will probably find a better answer using Google though. It's amazing.

  • Doubt in swing

    Hai ,
    Can I create a installer using swing?. If it is how to do that?
    Refer me any link for the same.

    DHURAI wrote:
    Hai ,That's not an English word.
    Can I create a installer using swing?. I'm not sure. What did you try? Where are you stuck?
    If it is how to do that?Same way you create any other program.
    Refer me any link for the same.
    link

  • Drag and Drop between two JTables

    I tried to implement drag and drop between two JTables using data transfer, but i did not get it to work, that dnd works in both directions. i have my own transferhandler, but if is use setTransferHandler(handler), i'm no longer able to start a drag from that table. i also tried to use a mouse listener to call exportAsDrag, but i only works in one direction. any ideas or examples?

    That is a rather large request, and indicates that you have likely not spent much time researching the subject before posting. Read up on Swing drag & drop here (http://java.sun.com/docs/books/tutorial/dnd/), then ask specific questions about something you don't understand, problems with your implementation, etc.
    Mike

  • How to populate my input data into java bean through

    Hi,
    I have one doubt in swing how many ways we can populate the input properties(data fron gui screen) into my javabean object(Simple POJO)
    Thanks,
    Tuku

    Hi,
    I have one doubt in swing how many ways we can populate the input properties(data fron gui screen) into my javabean object(Simple POJO)
    Thanks,
    Tuku

  • Writing a delegating CharsetDecoder?

    I want to write a CharsetDecoder that wraps an arbitrary existing decoder. Basically I want to run a standard decoder, but do special things for all the malformed input, not just the first bad sequence.
    The CharsetDecoder interface requires that I do this by implementing decodeLoop(). Unfortunately I cannot just wrap the delegate's decodeLoop(), because it's a protected method. That only leaves the two decode() methods. The problem with both is that I (inside my decodeLoop) don't know if endOfInput should be set. The single-parameter decode() method assumes it, and for the other decode() method I have to supply it. If I set it true when I'm invoked from a StreamDecoder there's a risk of spurious failures because the input buffer might only have part of a multi-byte sequence at any time, but the rest will show up later. If I use the second method and set it false there's a risk that I'll never get to process trailing malformed input, as the decode() method that invoked me will generate its own malformed result at the end of the buffer.
    The crucial information is stored as the ST_END state, but my own state is hidden from me in decodeLoop().
    Does anyone know how this is supposed to work? It seems like the CharsetDecoder interface got a bit carried away with OO.

    I had a similar problem when I wanted to delegate to a Swing drag-and-drop object, which had a protected method.
    You can use Reflection to get the appropriate Method from the Class of the decoder you're delegating to. Then call setAccessible(true) on the Method; after that you can call invoke() on the Method even though it's protected.

  • Light weight && heavy weight

    hi
    i have on doubt in swing... awt are heavy weight && swing are light weight .awt are system dependedent ....iam right..
    if right System dependent in this sence java is plaftform independent
    so how is it possible..
    pls give me details
    regards
    kedar

    Light weight components: To draw the components the jvm takes the os routine.
    Heavy weight components: To draw the components the JVM takes its own routines.
    You can check this. If you draw the awt component in one os it gives the different layout in other os. But this is not in the case of swing.This is the main difference . That's why the main components in big project are done in swing.
    if in doubt please ask?
    javasans

  • Enable Dropping into Empty JTables

    Hi there!
    I've just posted another blog on Swing Drag and Drop. Learn how to enable dropping into empty JTables with a single method call in Mustang, or a simple override in earlier versions of J2SE.
    http://weblogs.java.net/blog/shan_man/archive/2006/01/enable_dropping.html
    All the best,
    Shannon Hickey (Swing Team)

    Hi,
    It looks like below bug in SQLDeveloper.
    Bug 6326721: Updatable views - could not insert, update and delete as for tables.
    Please close the thread, if I answered your question.
    Regards,
    Satishbabu Gunukula
    http://oracleracexpert.blogspot.com

  • Hello  Simple problem - don,t know how to solve it.  With Premiere CC when I try to do a selection (click... drag... release the click) very often it stop way before the end of the move I'm swinging the Magic Mouse. I taught that the mouse clicking was de

    Hello
    Simple problem - don,t know how to solve it.
    With Premiere CC when I try to do a selection (click... drag... release the click) very often it stop way before the end of the move I'm swinging the Magic Mouse. I taught that the mouse clicking was defective and went to get a new Magic Mouse after lots of frustration. Today, I have an edit to do it it does the SAME thing !!
    I was like ????#$%?&*(???
    Opened all the lights and taught I've trow the new mouse to the garbage and was using the defective mouse again... no !! - ??
    Actually, the bran new mouse is doing the same thing. What I understand after investigating on the motion and watching carefully my fingers !! -  is that when I click I have to keep my finger at the EXACT same place on the mouse... drag and release and it's fine. If I click by pushing on the mouse and my finder is moving of a 1/32th of a millimeter, it will release and my selection will be to redo. You can understand my frustration ! - 75$ later... same problem, but I know that if I click with about 5 pounds of pressure and trying to pass my finger through the plastic of the mouse, it you stay steady and make it !
    The problem is that scrolling is enable while clicking and it bugs.
    How to disable it ??
    Simple question - can't find the answer !

    Helllooo !?
    sorry but the Magic Mouse is just useless with the new Adobe Premiere CC and since I'm not the only one but can't find answer this is really disappointing. This mouse is just fantastic and now I have to swap from a USB mouse to the Magic Mouse every times I do some editing. My USB mouse if hurting my hand somehow and I want to got back to the Magic Mouse asap. Please - for sure there is a simple solution !
    Thanks !!

  • Drag and Drop Swing Components between two different Application Windows

    Hi!!!
    I tried the Dnd feature using the DnD api. It was a simple test, where I ahd two different app windows each made up of a JLabel. One I registered as drag component and the other as a drop component. the drop componenet(JLabel) does not have any text making it invisble to the eyes. When I drag the other JLabel to the drop window, the drop JLAbel takes the text value. I was successful so far.
    But can I do the same for any other Swing component in a scenario where when I drag the component, the component should have physically moved into the other window.
    any help on this with sample code will be greatly apprciated.
    Thanks in advance
    Suja

    Ok I found the answer to your problem. If you download the tutorial they have the code there it's in one folder. I hope this helps.
    http://java.sun.com/docs/books/tutorial/

  • Drag and Drop files from website to local swing application

    I want to drag some file from the web browser and drop that to local swing list. Please suggest something on that?
    Regards,
    Kanni

    Kanni wrote:
    Can we do this using dnd, from a website.Yes.
    Can you pass me some sample code.No.

  • I want to make a VBIDE-Like drag and drop programm using Swing

    I am using swing am able to display no.of JInternalFrames on JDesktopPane to achieve an senario as in the VB-IDE we can easy resize and move the VB-components like Label ,Button ...But i want to drag a JButton or JLabel from any JToolBar and when i draw a rectangle,with dotted lines , on some container (may be JDeskTopane) and when i realese the Mouse then that JButton or JLabel will be displayed on the container.Please give me any idea,code,links or hints so that i can complete it.i.e How i can develope a VB-Ide like drag and ide in Java..
    Thanks in advance....

    Hey there buddy!
    You may like to check out the code for some projects on sourceforge if you wish to build something like this. Firstly, it is no easy task you are undertaking, but a good one none the less!
    The projects I have in mind are the following ones:
    Abacus GUI builder:
    http://sourceforge.net/projects/abaguibuilder
    JUIE
    http://sourceforge.net/projects/juie
    Have a look at the code to see how it is done. JUIE uses AWT but is in the process of being upgraded to swing. Abacus is currently Swing based.
    As far as I know, you are going to have to learn about Mouse handlers, in particular public void mousePressed();mouseDragged() etc... You will also need to learn about the layered pane architechture of Swing. When you click on your button it would (presumably) create a new button, add it to one of the thousands of possible layers you can have, and when you drag that component, the mouseDragged event will constantly send updates to the component to re-position it in the GUI...
    Good luck with it and I would be very interested to see how you went about it in the end.
    Take care
    Jason Barraclough.

  • How can I learn "Drag and Drop" with Swing components?

    Hello,
    Im using swing components, so using paint() to draw a "drag icon" doesnt seem acceptable. Im basically trying to move a JPanel image to be dragged/dropped over another JPanel in the same program/applet.
    I looked at Sun's Java Tutorial for information on doing drag and drop, but there was no trail completed yet.
    Does anyone know of any good resources to learn? Or can at least give me a general idea of how to do drag and drop?

    Did you see this?
    http://java.sun.com/docs/books/tutorial/dnd/index.html
    http://java.sun.com/docs/books/tutorial/reallybigindex.html

  • Creating movable toolbar in swing and dragging the components in realtime?

    hi,
    i started doing a java-swing application.
    my application is about a network simulator.
    i need the following things to be done:
    1.i need to have a movable toolbar. how to do it.Is there any example.
    2.i need to drag all the components from the tool bar onto the view.
    is there any way to drag and drop the components in real time.
    3.if iam able to drag the components and drop onto the view.is there a way to select the component at runtime(by getting system handles).
    if there is any example please refer it to me...
    regards
    varma

    hi,
    thanks for providing the dockable toolbar solution.
    i compiled the program and run the program.
    its runing successfully.
    thanks once again.
    now my immediate step is how to drag and drop components that i place in the toolbar over a view(jviewport)(for example components like rectangle,circle,oval,polygon).Also how to select the component at runtime so that i can give properties for that component or object.
    regards
    varma

  • Drag and Drop doubt!

    Im a new bie to DND (and not Swing!). Is that a Drag source can be only one component always or I can have multiple component as the drag source?
    (im kind of doing win explorer where the DND should work on both the Tree and the Table).
    Thanks,
    ananth

    You can utilize the same Transferable from your tree and table. The DragSource is just a mechanism to initiate the DnD.
    -B

Maybe you are looking for

  • Routing issue in Nexus 7009 due to vPC or hsrp

    we have two site's, on first site we have two nexus 7009 switches (Nexus A  & Nexus B)  and other site is remote site having two 6500 switches. (design attached) we are using hsrp on nexus switches and Active is Nexus A for all vlan's  From one of my

  • How do you transfer CS6 from one Mac computer to another (old to new computer)??

    I recently purchased a new mac and would like to transfer my CS6 license to my new computer. Can anyone help with this??

  • COOISPI: No display of operations/phases from process orders

    hi All, I'm using EHP$4and having this problem The List "Operations/Phases" does not show any output in the process order information system (Transaction COOISPI).,however i used to work on EHP3 and did'nt face this problem please advice Regards, Ahm

  • Plz tell me about the asset/capital material type.

    Hi, Can you plesae tell me how can i handle the scenario of asset/capital item procurement starting from the material type. If i will create seprate material type then from wchich material type i will copy it. regards, Dinesh

  • WLC5508 with SW6500 VSS mode

    Hi all, Unfortunatelly, I haven't enough experience on CISCO wireless controller and I'm not able to find an official answer for this question: My customer has two SW6500 on VSS mode connected via VSL. I wish to know if anyone have already connected