Double click event on JList element - java

I've got a problem, and I can't find an answer by myself.
I'm making a simple chat in java, and I populated a JList with names. When a name is double-clicked, that name needs to be passed as an argument to another frame (as a recipient name). But, double click does not work
I've got a main class InstantMessageFrame, in which a JList named friends is initialized and filled with an array of strings.
private JList<String> friends;
String names[] = { "Ana", "Banana", "Cikla", "Doris", "Ema", "Mirna","Matea","Veronika","Vera","Marta","Mirta","Davor","Marko","Matko","Kloki" };
JList<String> friends = new JList<String>(names);
Also,I added a listener to my JList
DisplayMessageDialog dmd = new DisplayMessageDialog();
friends.addMouseListener(dmd);
This is my DisplayMessageDialog class which checks if there was a double click. If there is a double click, a new Frame should appear. None of the lines in the first "if" statement executes (one with the e.getClickCount())
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DisplayMessageDialog extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
JList theList = (JList) e.getSource();
if (e.getClickCount() == 2)
{
int index = theList.locationToIndex(e.getPoint());
if (index >= 0)
{
Object o = theList.getModel().getElementAt(index);
InstantMessageDialog imd = new InstantMessageDialog(null, o.toString());
imd.setVisible(true);
System.out.println("Double-clicked on: " + o.toString());
}
}
}
}
This is how it should look like:
http://i.stack.imgur.com/Bex5U.png
And when double clicked,a new frame should appear (in code "InstantMessageDialog" object)
http://i.stack.imgur.com/yjvW6.png
And it should look like this.

So working off your out-of-context code snippets, this example seems to work just fine, so I can only surmise that your problem is else where in your code that you're not showing us...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JList<String> friends;
public TestPane() {
String names[] = {"Ana", "Banana", "Cikla", "Doris", "Ema", "Mirna", "Matea", "Veronika", "Vera", "Marta", "Mirta", "Davor", "Marko", "Matko", "Kloki"};
JList<String> friends = new JList<String>(names);
setLayout(new BorderLayout());
add(new JScrollPane(friends));
DisplayMessageDialog dmd = new DisplayMessageDialog();
friends.addMouseListener(dmd);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
public class DisplayMessageDialog extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
JList theList = (JList) e.getSource();
// if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
if (e.getClickCount() == 2) {
int index = theList.locationToIndex(e.getPoint());
if (index >= 0) {
Object o = theList.getModel().getElementAt(index);
//InstantMessageDialog imd = new InstantMessageDialog(null, o.toString());
//imd.setVisible(true);
JOptionPane.showMessageDialog(theList, "Double-clicked on: " + o.toString());
System.out.println("Double-clicked on: " + o.toString());
}
}
}
}
}
I might add though, you "double click" check probably should be something more like...
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
Assuming your really only want to respond to left mouse clicks

When a name is double-clicked...
When designing a GUI it is always a good idea to provide support for the mouse and keyboard.
Check out List Action. It adds Action support to a JList. All you need to do is provide the Action. Then the Action can be invoked by:
double clicking, or
using the Enter key.
The most basic example of using the ListAction would be:
String[] data = { "zero", "one", "two", "three", "four", "five" };
JList list = new JList( data );
Action displayAction = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
JList list = (JList)e.getSource();
System.out.println(list.getSelectedValue());
}
};
ListAction la = new ListAction(list, displayAction);

Related

Stepped ComboBox refresh

A Stepped ComboBox is very useful to make the drop-down pop-up wider than the text field. However when new content is added to the list, the pop-up gets its initial width back.
By default
After refresh (new element)
SSCCE
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxUI;
public class SteppedComboBoxRefresh extends JFrame {
private List<String> list;
private final SteppedComboBox combo;
public SteppedComboBoxRefresh() {
super("SteppedComboBox Refresh");
this.list = new ArrayList<String>(Arrays.asList(new String[]{
"AAA", "AAAAAA"
}));
this.combo = new SteppedComboBox(this.list.toArray());
this.combo.setDimensions(50);
JButton addButton = new JButton("Add longer string");
addButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
list.add(list.get(list.size()-1) + "AAA");
combo.setModel(new DefaultComboBoxModel(list.toArray()));
combo.setDimensions(50);
}
});
getContentPane().setLayout(new FlowLayout());
getContentPane().add(this.combo);
getContentPane().add(addButton);
}
public static void main (String args[]) {
SteppedComboBoxRefresh f = new SteppedComboBoxRefresh();
f.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize (300, 100);
f.setVisible(true);
}
}
class SteppedComboBoxUI extends MetalComboBoxUI {
#Override
protected ComboPopup createPopup() {
BasicComboPopup popup = new BasicComboPopup( this.comboBox ) {
#Override
public void show() {
Dimension popupSize = ((SteppedComboBox)this.comboBox).getPopupSize();
popupSize.setSize( popupSize.width,
getPopupHeightForRowCount( this.comboBox.getMaximumRowCount() ) );
Rectangle popupBounds = computePopupBounds( 0,
this.comboBox.getBounds().height, popupSize.width, popupSize.height);
this.scroller.setMaximumSize( popupBounds.getSize() );
this.scroller.setPreferredSize( popupBounds.getSize() );
this.scroller.setMinimumSize( popupBounds.getSize() );
this.list.invalidate();
int selectedIndex = this.comboBox.getSelectedIndex();
if ( selectedIndex == -1 ) {
this.list.clearSelection();
} else {
this.list.setSelectedIndex( selectedIndex );
}
this.list.ensureIndexIsVisible( this.list.getSelectedIndex() );
setLightWeightPopupEnabled( this.comboBox.isLightWeightPopupEnabled() );
show( this.comboBox, popupBounds.x, popupBounds.y );
}
};
popup.getAccessibleContext().setAccessibleParent(this.comboBox);
return popup;
}
}
class SteppedComboBox extends JComboBox {
protected int popupWidth;
public SteppedComboBox(ComboBoxModel aModel) {
super(aModel);
setUI(new SteppedComboBoxUI());
this.popupWidth = 0;
}
public SteppedComboBox(final Object[] items) {
super(items);
setUI(new SteppedComboBoxUI());
this.popupWidth = 0;
}
public SteppedComboBox(Vector items) {
super(items);
setUI(new SteppedComboBoxUI());
this.popupWidth = 0;
}
public void setPopupWidth(int width) {
this.popupWidth = width;
}
public Dimension getPopupSize() {
Dimension size = getSize();
if (this.popupWidth < 1) {
this.popupWidth = size.width;
}
return new Dimension(this.popupWidth, size.height);
}
public void setDimensions(int width) {
Dimension d = getPreferredSize();
setPreferredSize(new Dimension(width, d.height));
setPopupWidth(d.width);
}
}
The ComboBox still uses its previous PreferredSize. It's needed to set the preferred size back to null, so that we get the size which is preferred by the new content in the list.
void javax.swing.JComponent.setPreferredSize(Dimension preferredSize)
Sets the preferred size of this component. If preferredSize is null, the UI will be asked for the preferred size.
public void setDimensions(int width) {
setPreferredSize(null);
Dimension d = getPreferredSize();
setPreferredSize(new Dimension(width, d.height));
setPopupWidth(d.width);
}
Result
You could use the Combo Box Popup.
It is a more flexible version of the Stepped Combo Box. Best of all it can be used on any combo box since the logic is implemented in a `PopupMenuListener'.
You can control the maximum width of the popup. You can even have the popup display above the combo box instead of below.

How to override the JPopupMenu method show?

my goal is to highlight a jlist item after a rightclick then show a jpopupmenu..
I read advice that overriding the method show is the way..
in my case i declare my jpopupmenu at the beginning of my class
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem masterlist,settings;
then i have a method to set up my menuItems
public void setPopupMenu(int depth//ignore this var){
//popupMenu = new JPopupMenu();
popupMenu.removeAll();
masterlist = new JMenuItem("Masterlist");
settings = new JMenuItem("Settings");
//action for marsterlist
masterlist.addActionListener(new ActionListener(){
//stuff here
}
});
//action for settings
settings.addActionListener(new ActionListener(){
//stuff here
}
});
popupMenu.add(masterlist);
popupMenu.add(settings);
}
and my list is in a different method
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);
list.setComponentPopupMenu(popupMenu);
I tried putting this on a mouseAdapter of my list but the popupmenu fires first and ignores highlighting...
if ( SwingUtilities.isRightMouseButton(mouseEvent) )//highlight the right clicked item
{
int row = list.locationToIndex(mouseEvent.getPoint());
list.setSelectedIndex(row);
String val = (String)list.getSelectedValue();
System.out.println(val);
}
i know that overriding is something like this
popupmenu = new JPopupMenu(){
#Override
public void show(){}
}
but i cant do that because i am manipulating the menuitems on a method...
or is there any other approach that anyone can suggest...
Rather then trying to modify the state of the JPopupMenu, why not simply modify the state of the menu item when you detect a right click...
So, basically, I make use of the Actions API to define a menu item for a JPopupMenu, which allows it to register a ListSelectionListener to the underlying JList...
public class ShowItemAction extends AbstractAction {
private JList list;
public ShowItemAction(JList list) {
this.list = new JList();
putValue(NAME, "Showing ...");
list.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int index = list.getSelectedIndex();
String value = list.getSelectedValue().toString();
putValue(NAME, "Showing " + value + " # " + index);
}
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
// The actual action to be performed when selected
}
}
All this does is, when the selection is changed, is change the text (NAME) of the action, which changes the text of the menu item, based on the currently selected row.
When I create the JList, I assign it a JPopupMenu via the setComponentPopupMenu method, which means I no longer need to care about it and it will be displayed appropriately based on the current look and feel requirements
JList list = new JList(model);
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(new ShowItemAction(list));
list.setComponentPopupMenu(popupMenu);
I then add a MouseListener to the JList which I use to change the row selection when you right click on the JList...
list.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
JList list = (JList)e.getComponent();
if (SwingUtilities.isRightMouseButton(e)) {
int row = list.locationToIndex(e.getPoint());
list.setSelectedIndex(row);
}
}
});
This not entirely fool proof, as if you right click the JList while the popup is visible, the MouseListener doesn't appear to get notified :P
Runnable example...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import static javax.swing.Action.NAME;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultListModel model = new DefaultListModel();
model.addElement("One");
model.addElement("Two");
model.addElement("Three");
model.addElement("Four");
JList list = new JList(model);
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(new ShowItemAction(list));
list.setComponentPopupMenu(popupMenu);
list.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
int row = list.locationToIndex(e.getPoint());
list.setSelectedIndex(row);
}
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(list));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ShowItemAction extends AbstractAction {
private JList list;
public ShowItemAction(JList list) {
this.list = new JList();
putValue(NAME, "Showing ...");
list.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int index = list.getSelectedIndex();
String value = list.getSelectedValue().toString();
putValue(NAME, "Showing " + value + " # " + index);
}
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
}

How to filter JTable with respect to a specific column?

I have JFrame with a JTable and JComboBox with choices
"Leave Code"
"Leave Description"
I need to filter the table data with respect to leave code when leave code is selected in the combo box and filter with respect to leavedesc when leave description is selected in the combo box.
This is the function that I've used to filter the data:
private void filtervalue(String filterString) {
TableModel model = tableLeave.getModel();
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
tableLeave.setRowSorter(sorter);
if (filterString.length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + filterString));
}
}
But the problem is it will sort by leavedesc as well as leavecode, so no effect by the combo box. Please help me to implement column based filter.
Without a runnable example, I can't be 100% sure, but I would guess that setting the RowSorter each time you change the filter text is causing the table to be re-sorted.
Instead, set a TableRowSorter when you create the JTable and simply update the filter, for example...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class TableFilter {
public static void main(String[] args) {
new TableFilter();
}
private JTable table;
private JComboBox filterBy;
private JTextField filterText;
public TableFilter() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel(new Object[]{"Code", "Description"}, 0);
model.addRow(new Object[]{"A001", "Holidays"});
model.addRow(new Object[]{"B001", "Sick"});
model.addRow(new Object[]{"A002", "Zombitse"});
model.addRow(new Object[]{"C001", "Crazy bin"});
model.addRow(new Object[]{"C002", "Postal"});
model.addRow(new Object[]{"D002", "Job Interview"});
model.addRow(new Object[]{"D004", "it's sunny outside"});
table = new JTable(model);
table.setRowSorter(new TableRowSorter<TableModel>(model));
JPanel filterPane = new JPanel(new GridBagLayout());
filterBy = new JComboBox(new Object[]{"Nothing", "Code", "Description"});
filterText = new JTextField(20);
filterPane.add(filterBy);
filterPane.add(filterText);
filterBy.setSelectedIndex(0);
filterBy.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
updateFilter();
}
});
filterText.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
updateFilter();
}
#Override
public void removeUpdate(DocumentEvent e) {
updateFilter();
}
#Override
public void changedUpdate(DocumentEvent e) {
updateFilter();
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(filterPane, BorderLayout.NORTH);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected void updateFilter() {
Object selected = filterBy.getSelectedItem();
TableRowSorter<TableModel> sorter = (TableRowSorter<TableModel>) table.getRowSorter();
String text = "(?i)" + filterText.getText();
if ("Nothing".equals(selected)) {
sorter.setRowFilter(null);
} else {
int col = -1;
if ("Code".equals(selected)) {
col = 0;
} else if ("Description".equals(selected)) {
col = 1;
}
sorter.setRowFilter(RowFilter.regexFilter(text, col));
}
}
}
This example demonstrates real time updates, so as you type, the table will be filtered...

Keylistener not responding to numlock numbers

i'm making a keylistener that listens to ctrl-1 and ctrl-2.
Im making a quiz for teams. Team 1 should press ctrl-1 if they want to answer. Team 2 should press ctrl-2 if they want to answer.
The reason i chose for ctrl is because there are 2 control keys. So 2 teams can play against eachother on 1 keyboard.
I want team 1 to use the left control and the numbers under F1-F12.
And team 2 to use the right control and the numbers on the numlock.
My code registrates the triggers of team 1 but not from team 2.
Here is my code :
public void keyPressed(KeyEvent e) {
if((QuizController)getController() != null){
if(e.getKeyCode () == KeyEvent.VK_1){
if((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)
System.out.println("Team 1");
}
if(e.getKeyCode () == KeyEvent.VK_2){
if((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)
System.out.println("Team 2");
}
}
}
EDIT : I just did it with key bindings, gives the same problem, here is the code.
AbstractAction team1 = new AbstractAction() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Team 1");
}
};
AbstractAction team2 = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Team 2");
}
};
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_1, java.awt.event.InputEvent.CTRL_DOWN_MASK),"actionMap1");
getActionMap().put("actionMap1", team1);
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_2, java.awt.event.InputEvent.CTRL_DOWN_MASK),"actionMap2");
getActionMap().put("actionMap2", team2);
Thank you!
Firstly, I would highly recommend using the key bindings API.
Second KeyEvent.VK_1 is not the same event that is raised for numpad+1, this is triggered by KeyEvent.VK_NUMPAD1 instead, it's a different key event, just like the function keys are raise KeyEvent.VK_F1 to 12 events.
For example...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class KeyBindingsTest {
public static void main(String[] args) {
new KeyBindingsTest();
}
public KeyBindingsTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel state;
public TestPane() {
setLayout(new GridBagLayout());
state = new JLabel("Nothing here");
add(state);
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.CTRL_DOWN_MASK), "Ctrl+1");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_2, KeyEvent.CTRL_DOWN_MASK), "Ctrl+2");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD1, KeyEvent.CTRL_DOWN_MASK), "Ctrl+1");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK), "Ctrl+2");
ActionMap am = getActionMap();
am.put("Ctrl+1", new MessageAction("Ctrl+1"));
am.put("Ctrl+2", new MessageAction("Ctrl+2"));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public class MessageAction extends AbstractAction {
private String message;
public MessageAction(String message) {
this.message = message;
}
#Override
public void actionPerformed(ActionEvent e) {
state.setText(message);
}
}
}
}

Drag and Drop custom object from JList into JLabel

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.
This is about as far as I've gotten:
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
}
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?
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.
Drag and Drop can be a complex beast, not made any easier by the conflicting information that's available. Personally, I like to avoid the Transfer API, but I'm old school like that.
The glue to DnD really is the DataFlavor. I prefer to roll my own, makes life a lot easier.
In this example, I've used a single TransferHandler, but realistically, you really should have one for dragging and one for dropping, in particular, you should have one for each component you want to drop onto.
The main reason for this is, I put a trap in my canImport method to reject it if your dragging over a JList, so you can only drop it on the JLabel, this is a little hack and probably not the best idea.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.io.IOException;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DnDTransferableTest {
public static void main(String[] args) {
new DnDTransferableTest();
}
public DnDTransferableTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
#SuppressWarnings("serial")
public class TestPane extends JPanel {
private JList<ListItem> list;
private JLabel label;
public TestPane() {
list = new JList<ListItem>();
list.setDragEnabled(true);
list.setTransferHandler(new ListTransferHandler());
DefaultListModel<ListItem> model = new DefaultListModel<ListItem>();
for (int index = 0; index < 10; index++) {
model.addElement(new ListItem("Item " + index));
}
list.setModel(model);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(list), gbc);
label = new JLabel("Drag on me...");
gbc.gridx++;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
add(label, gbc);
label.setTransferHandler(new ListTransferHandler());
}
}
#SuppressWarnings("serial")
public class ListTransferHandler extends TransferHandler {
#Override
public boolean canImport(TransferSupport support) {
return (support.getComponent() instanceof JLabel) && support.isDataFlavorSupported(ListItemTransferable.LIST_ITEM_DATA_FLAVOR);
}
#Override
public boolean importData(TransferSupport support) {
boolean accept = false;
if (canImport(support)) {
try {
Transferable t = support.getTransferable();
Object value = t.getTransferData(ListItemTransferable.LIST_ITEM_DATA_FLAVOR);
if (value instanceof ListItem) {
Component component = support.getComponent();
if (component instanceof JLabel) {
((JLabel)component).setText(((ListItem)value).getText());
accept = true;
}
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
return accept;
}
#Override
public int getSourceActions(JComponent c) {
return DnDConstants.ACTION_COPY_OR_MOVE;
}
#Override
protected Transferable createTransferable(JComponent c) {
Transferable t = null;
if (c instanceof JList) {
#SuppressWarnings("unchecked")
JList<ListItem> list = (JList<ListItem>) c;
Object value = list.getSelectedValue();
if (value instanceof ListItem) {
ListItem li = (ListItem) value;
t = new ListItemTransferable(li);
}
}
return t;
}
#Override
protected void exportDone(JComponent source, Transferable data, int action) {
System.out.println("ExportDone");
// Here you need to decide how to handle the completion of the transfer,
// should you remove the item from the list or not...
}
}
public static class ListItemTransferable implements Transferable {
public static final DataFlavor LIST_ITEM_DATA_FLAVOR = new DataFlavor(ListItem.class, "java/ListItem");
private ListItem listItem;
public ListItemTransferable(ListItem listItem) {
this.listItem = listItem;
}
#Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{LIST_ITEM_DATA_FLAVOR};
}
#Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(LIST_ITEM_DATA_FLAVOR);
}
#Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return listItem;
}
}
public static class ListItem {
private String text;
public ListItem(String text) {
this.text = text;
}
public String getText() {
return text;
}
#Override
public String toString() {
return getText();
}
}
}

Categories