0

I am doing a DragNDrop Activity. There is a textfield(q1ans) and 2 choices(q1c1 & q1c2). In the question, q1c1 is the right answer, but both q1c1 & q1c2 are able to be dragged into the textfield. How do i reject q1c2 when it is dragged into the textfield? This is what i have so far:

    q1ans = new JTextField();
    q1ans.setEditable(false);
    q1ans.setHorizontalAlignment(SwingConstants.CENTER);
    q1ans.setBounds(380, 147, 44, 20);
    add(q1ans);
    q1ans.setTransferHandler(new TransferHandler("text"));
    q1ans.setColumns(10);

    question1 = new JTextField();
    question1.setEnabled(false);
    question1.setEditable(false);
    question1.setText("[ ] c,d //where c and d are integer arrays");
    question1.setBounds(424, 147, 208, 20);
    add(question1);
    question1.setColumns(10);


    q1c1 = new JTextField();
    q1c1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            q1c1.selectAll();
        }
    });
    q1c1.setEditable(false);
    q1c1.setDragEnabled(true);
    q1c1.setText("int");
    q1c1.setBounds(264, 197, 21, 23);
    add(q1c1);
    q1c1.setColumns(10);

    q1c2 = new JTextField();
    q1c2.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseEntered(MouseEvent e) {
            q1c2.selectAll();
        }
    });
    q1c2.setText("string");
    q1c2.setEditable(false);
    q1c2.setDragEnabled(true);
    q1c2.setColumns(10);
    q1c2.setBounds(341, 197, 34, 23);
    add(q1c2);
  • 2
    Welcome to the wonderful world of "oh my god, why is this so complicated". DnD is not as simple as people think. You could have a look at [this example](http://stackoverflow.com/questions/13639804/drag-and-drop-files-from-os-into-jtable-java/13640115#13640115) and [this example](http://stackoverflow.com/questions/22161412/java-drop-and-drag-label-to-join-correct-image/22161571#22161571) or [this example](http://stackoverflow.com/questions/13855184/drag-and-drop-custom-object-from-jlist-into-jlabel/13856193#13856193) which makes use of the newer `Transfer` API – MadProgrammer Feb 01 '17 at 21:31
  • 1
    Or [this example](http://stackoverflow.com/questions/15080244/java-drag-n-drop-files-of-specific-extension-on-jframe/15080654#15080654) – MadProgrammer Feb 01 '17 at 21:32
  • oh my god, why is this so complicated – Rui Isolet Feb 04 '17 at 06:58
  • Welcome to the wonderful world of DnD – MadProgrammer Feb 04 '17 at 07:10

0 Answers0