Note that there are some explanatory texts on larger screens.

plurals
  1. PODrag and Drop custom object from JList into JLabel
    text
    copied!<p>I have a JList containing an ArrayList of custom objects and I'm trying to create a drag and drop into fields. I'm having trouble understanding how to package and receive the object in Transferable. </p> <p>This is about as far as I've gotten:</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; public class FlightProjectInterface extends JFrame{ //create GUI Objects private JFrame primaryFrame; private JPanel createFlightPanel; private JPanel aircraftLayout; private JList personsJList, personsOnFlightJList; private JTextField pilotLabel, coPilotLabel, backseat1Label, backseat2Label; public FlightProjectInterface(){ //establish frame super("Create Flight"); setLayout( new FlowLayout()); //aircraftPanel aircraftLayout = new JPanel(); aircraftLayout.setLayout(new GridLayout(2,2)); pilotLabel = new JTextField("Drag Pilot Here"); //build person load list DefaultListModel listModel = new DefaultListModel(); for (Person person : Database.persons) listModel.addElement(person); personsJList = new JList(listModel); personsJList.setVisibleRowCount(5); personsJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); personsJList.setDragEnabled(true); add( new JScrollPane(personsJList) ); aircraftLayout.add(pilotLabel); add(aircraftLayout); }//end constructor } </code></pre> <p>Clarification: I'm having trouble taking the object selection from the JList and creating a Transferable out of it. With the code above, the toString representation of the object is simply pasted in the text field, so I'm not able to pull object data from the dropped location. How can I "package" the object itself and drop it into a placeholder that I can reference the object itself from the GUI?</p> <p>Ideally, there would be 4 fields that each contains an object that can be dropped. The person would be removed from the list if they are dropped, but returned to the list if replaced. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload