Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8914435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:43:03+00:00 2026-06-15T04:43:03+00:00

I have multiple DnD panels layered on a Jpanel with boarder layout..Each DnD panel

  • 0

I have multiple DnD panels layered on a Jpanel with boarder layout..Each DnD panel has a Text field set as a drop target. Only last DnD panel gets the focus. Others do not act as drop targets. Here is the code for DnD panel

public class DnDPanel extends javax.swing.JPanel {
/** Creates new form DnDPanel */

public DnDPanel() {
    initComponents();        
    dndTextField.setDropTarget(new DropTarget() {

        @Override
        public synchronized void drop(DropTargetDropEvent evt) {
            try {
                dndTextField.setBackground(Color.WHITE);
                evt.acceptDrop(DnDConstants.ACTION_COPY);
                List<File> droppedFiles = (List<File>) evt.getTransferable().getTransferData(
                        DataFlavor.javaFileListFlavor);
                for (File file : droppedFiles) {
                    dndTextField.setText(file.getAbsolutePath());
                }
                dndTextField.setBackground(Color.getHSBColor(0.56f, 1.0f, 0.9f));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public synchronized void dragEnter(DropTargetDragEvent dtde) {
            //Change cursor...
            Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
            setCursor(cursor);
            dndTextField.setText("Drop Here !!");
            dndTextField.setBackground(Color.getHSBColor(0.56f, 1.0f, 0.9f));
        }

        @Override
        public synchronized void dragExit(DropTargetEvent dtde) {
            Cursor cursor = new Cursor(Cursor.DEFAULT_CURSOR);
            setCursor(cursor);
            dndTextField.setBackground(Color.WHITE);
            dndTextField.setText("Drag n drop a Folder");
        }
    });
}

public static void setText(String text)
{
    dndTextField.setText(text);
}

public static String getPath()
{
    return dndTextField.getText();
}


/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    dndTextField = new javax.swing.JTextField();

    setPreferredSize(new java.awt.Dimension(250, 250));

    dndTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    dndTextField.setText("DnD");
    dndTextField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            dndTextFieldActionPerformed(evt);
        }
    });

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(dndTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(dndTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
            .addContainerGap())
    );
}// </editor-fold>

private void dndTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:
}                                            

// Variables declaration - do not modify
private static javax.swing.JTextField dndTextField;
// End of variables declaration
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:43:05+00:00Added an answer on June 15, 2026 at 4:43 am

    One major problem is that you’re using a static variable for your JTextField:

    private static javax.swing.JTextField dndTextField;
    

    The solution is simple: dont’ do this! If you want each JPanel to have its own JTextField that behaves independently of the other, than they should be non-static. In fact you should strive to minimize use of static anything in your programs unless there’s a very strong indication for doing so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple table rows that has a set of radio buttons on each
I have multiple input fields on one page. Each input field have a text
I have multiple textboxes, every textbox has a related hidden field, the hidden field's
I have multiple input text fields grouped in divs: <div class=container0> <input class=containerItem0 name=containerItem[0][0]
I have multiple autocomplete inputs on my page. Behind the scenes, each autocomplete list
I have multiple divs, each with a unique id and image. I would like
I have multiple images and I want to bind draggable events to each of
I have multiple branches of a project checked out, each under their own directory
i have multiple files each containing 8/9 columns. for a single file : I
I have multiple divs on my page.I want to copy selected text from div1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.