Results 1 to 20 of 20
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By Norm

Thread: how to convert DropTargetListener into a method

  1. #1
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default how to convert DropTargetListener into a method

    Hello guys i have a problem here.
    from my Main_Frame.java there is a jTextArea1 that has a drop and drop listener and here's what i want to do. if ever i drog a file into my jTextArea i want to display absolute path of the file. how to do that? this code is working but i cant display the absolute path on my jTextArea. any help?

    Java Code:
    //Main_Frame.java
    
    DropTarget dt;
    dt = new DropTarget(jTextArea1, new DebugDropListener());

    Java Code:
    //DebugDropListener.java
    
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.Transferable;
    import java.awt.datatransfer.UnsupportedFlavorException;
    import java.awt.dnd.DnDConstants;
    import java.awt.dnd.DropTargetDragEvent;
    import java.awt.dnd.DropTargetDropEvent;
    import java.awt.dnd.DropTargetEvent;
    import java.awt.dnd.DropTargetListener;
    import java.awt.dnd.InvalidDnDOperationException;
    import java.io.IOException;
    
    class DebugDropListener implements DropTargetListener {
        public DebugDropListener(){
    
        }
        @Override
        public void dragEnter(DropTargetDragEvent dtde) {
     
        }
        @Override
        public void dragOver(DropTargetDragEvent dtde) {
           
        }
        @Override
        public void dropActionChanged(DropTargetDragEvent dtde) {
           
        }
        @Override
        public void dragExit(DropTargetEvent dte) {
            
        }
    
        @Override
        public void drop(DropTargetDropEvent dtde) {
     try {
            // Get the dropped object and try to figure out what it is.
            Transferable tr = dtde.getTransferable( );
            DataFlavor[] flavors = tr.getTransferDataFlavors( );
            for (int i = 0; i < flavors.length; i++) {
              System.out.println("Possible flavor: " + flavors[i].getMimeType( ));
              // Check for file lists specifically.
              if (flavors[i].isFlavorJavaFileListType( )) {
                // Great! Accept copy drops.
                dtde.acceptDrop(DnDConstants.ACTION_COPY);
    
                // Add the list of filenames to our text area.
                java.util.List list =
                    (java.util.List)tr.getTransferData(flavors[i]);
                for (int j = 0; j < list.size( ); j++) {
                //  ta.append(list.get(j) + "\\\\n");
                  System.out.println(list.get(j));
                }
                // If we made it this far, everything worked.
                dtde.dropComplete(true);
                return;
              }
            }
            
            System.out.println("Drop failed: " + dtde);
            dtde.rejectDrop( );
          } catch (UnsupportedFlavorException e) {
            e.printStackTrace( );
            dtde.rejectDrop( );
          } catch (InvalidDnDOperationException e) {
            e.printStackTrace( );
            dtde.rejectDrop( );
          } catch (IOException e) {
            e.printStackTrace( );
            dtde.rejectDrop( );
          }  
        }
    }
    thanks in advance..

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Is the problem getting the information you want to show
    or getting a reference to where you want to show it?

  3. #3
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    "getting the information you want to show"

    i want to display absolute path of the file into jtextArea. dont know how because i cant convert it into a method.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Have you tried printing out all the information from the classes you are using in the drop listener? It could be there.

    i cant convert it into a method.
    Sorry, I have no idea what you want to convert.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you get the absolute path and print it onto the console via a println statement? If so, then it's a simple matter of giving your drop listener a PropertyChangeSupport object and let the GUI listen to it via a PropertyChangeListener and respond to successful drops.
    Jhovarie likes this.

  6. #6
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    "Can you get the absolute path and print it onto the console via a println statement? "

    yes.. if i drop a file into my jTextArea1 it print the correct absolute path using println in console my problem is that i cant display the path into my jTextArea1

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    If you can print a String on the console, then you can put that String into a text area. Why can't you do this:
    refToTextArea.setText(theString); // put the String into the text area

  8. #8
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    because my jTextArea is in Main_Frame.java and my code DebugDropListener.java is the one that can detect the absolute path see my code.

    Java Code:
    import java.awt.dnd.DropTarget;
    
    public class Main_Frame extends javax.swing.JFrame {
    
        public Main_Frame() {
            initComponents();
            DropTarget dt;
    dt = new DropTarget(jTextArea1, new DebugDropListener());
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(23, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(32, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new Main_Frame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        // End of variables declaration
    }
    and see my code DebugDropListener.java above
    Last edited by Jhovarie; 08-07-2011 at 12:51 AM.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Pass a reference to the class with the text area to the drop listener class. Add a setTextArea method to the class with the text area. The drop listener can call the setTextArea method to set the text area.
    Jhovarie likes this.

  10. #10
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    i try this but its not working any suggestion?

    Java Code:
    in my Main_Frame.java
        private void jTextArea1PropertyChange(java.beans.PropertyChangeEvent evt) {
    DebugDropListener f = new DebugDropListener();
       f.Act(jTextArea1);
        }
    Java Code:
    //in my DebugDropListener
    private JTextArea text = new JTextArea();
        public void Act(JTextArea jTextArea1){
         jTextArea1 =  text;
        }
        
        @Override
        public void drop(DropTargetDropEvent dtde) {
     try {
            // Get the dropped object and try to figure out what it is.
            Transferable tr = dtde.getTransferable( );
            DataFlavor[] flavors = tr.getTransferDataFlavors( );
            for (int i = 0; i < flavors.length; i++) {
              System.out.println("Possible flavor: " + flavors[i].getMimeType( ));
              // Check for file lists specifically.
              if (flavors[i].isFlavorJavaFileListType( )) {
                // Great! Accept copy drops.
                dtde.acceptDrop(DnDConstants.ACTION_COPY);
    
                // Add the list of filenames to our text area.
                java.util.List list =
                    (java.util.List)tr.getTransferData(flavors[i]);
                for (int j = 0; j < list.size( ); j++) {
                //  ta.append(list.get(j) + "\\n");
                  System.out.println(list.get(j));
                  text.setText(""+list.get(j)); //<-- not working
                }
                // If we made it this far, everything worked.
                dtde.dropComplete(true);
                return;
              }
            }

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    its not working
    Please explain what "not working" means.

    Why do you create an instance of JTextArea here?
    Java Code:
    private JTextArea text = new JTextArea();
    Just set the variable to null.


    Take a look at the following:
    Java Code:
        jTextArea1 =  text;
    Last edited by Norm; 08-07-2011 at 02:36 AM.

  12. #12
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    dont know how to do it.

    Java Code:
         for (int j = 0; j < list.size( ); j++) {
                //  ta.append(list.get(j) + "\\n");
                  System.out.println(list.get(j));
                  text.setText(""+list.get(j)); //<-- not working // dont know how to set the textarea of Main_Frame.java
                }
                // If we made it this far, everything worked.
    "Pass a reference to the class with the text area to the drop listener class. Add a setTextArea method to the class with the text area. The drop listener can call the setTextArea method to set the text area."

    any example what you are talking about?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Look at the code in you Act() method. Double check that assignment statement.

  14. #14
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    can you get me closer check my code.

    Java Code:
    private JTextArea text;
        public void Act(JTextArea jTextArea1){
          text = jTextArea1;
          text.setText("this is working to set \nthe string of jTextArea");
        }
      
        @Override
        public void drop(DropTargetDropEvent dtde) {
     try {
            Transferable tr = dtde.getTransferable( );
            DataFlavor[] flavors = tr.getTransferDataFlavors( );
            for (int i = 0; i < flavors.length; i++) {
              System.out.println("Possible flavor: " + flavors[i].getMimeType( ));
              if (flavors[i].isFlavorJavaFileListType( )) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY);
    
                java.util.List list =
                    (java.util.List)tr.getTransferData(flavors[i]);
                for (int j = 0; j < list.size( ); j++) {
                //  ta.append(list.get(j) + "\\n");
                  System.out.println(list.get(j));
                  text.setText(""+list.get(j)); //<-- this one doest not set the string of jTextArea
                }
                dtde.dropComplete(true);
                return;
              }
            }

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Is the desired string printed by the println? Can you show what is printed out here?
    Java Code:
                  System.out.println(list.get(j));
                  text.setText(""+list.get(j)); //<-- this one doest not set the string of jTextArea

  16. #16
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    Yes println work it show the absolute path of the file.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    But the setText() did not show the value you wanted???
    How many lines did: System.out.println(list.get(j));
    print out?
    Were there any lines printed after the path was printed?
    Change the setText() to append() to get all the values in list to the text area.

  18. #18
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    this one work i just add "static" after private

    Java Code:
    private static JTextArea text;
        public void Act(JTextArea jTextArea1){
          text = jTextArea1;
        }
    and set the text area using this code and it works.
    Java Code:
    text.setText(text.getText()+""+list.get(j)+"\n");
    thanks Norm and Fubarable

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jhovarie View Post
    this one work i just add "static" after private

    Java Code:
    private static JTextArea text;
        public void Act(JTextArea jTextArea1){
          text = jTextArea1;
        }
    and set the text area using this code and it works.
    Java Code:
    text.setText(text.getText()+""+list.get(j)+"\n");
    This is a very bad idea and makes your code non-object oriented. Please don't do this.

  20. #20
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This is a very bad idea and makes your code non-object oriented. Please don't do this.
    ok so can you post a good example?

Similar Threads

  1. Replies: 18
    Last Post: 04-18-2011, 05:39 AM
  2. Convert Recursive Method to Java
    By snowMac in forum Advanced Java
    Replies: 1
    Last Post: 03-08-2011, 01:38 AM
  3. Replies: 1
    Last Post: 07-27-2010, 06:01 PM
  4. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •