Reputation: 884
I am trying to populate a JTextArea
from a JList
when the user double clicks the item. I am not completely sure how to accomplish this, here is what I have so far.
// Create Top Right JPanel and JList
String[] listB = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
"More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content",
"More and more content" };
final JList listBottom = new JList(listB);
listBottom.setVisibleRowCount(12);
JScrollPane scrollPaneB = new JScrollPane(listBottom);
panelBottom.add(scrollPaneB);
scrollPaneB.setViewportView(listBottom);
scrollPaneB.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
scrollPaneB.setVisible(true);
//listBottom.setVisible(true);
listBottom.setBorder(BorderFactory.createLoweredBevelBorder());
// Create Top Right Action Listener
listBottom.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent arg0) {
Selection selectionB = (Selection)listBottom.getSelectedValue();
textField.setText(selectionB.getContents());
}
});
Upvotes: 2
Views: 812
Reputation: 324157
I am trying to populate a JTextArea from a JList when the user double clicks the item.
When designing a GUI the user should be able to use the mouse or the keyboard to invoke an Action on a component.
Check out List Action. It will invoke an Action
when you double click
or use the Enter
key. All you have to do is create the Action
.
Upvotes: 0
Reputation: 591
You simply add a mouseClicked listener, and check if the mouse clicks the JList.
String[] listB = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
"More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content",
"More and more content" };
final JList listBottom = new JList(listB);
....//other code
listBottom.addMouseListener(new MouseAdapter(){
//Called when you click the JList
public void mouseClicked(MouseEvent e) {
JList list = (JList)e.getSource();
//Makes sure it only registers for single clicks(always registers even on double clicks, just registers twice.)
if (e.getClickCount() == 1) {
//Gets the point where you clicked and returns the index of the element at that point
int index = list.locationToIndex(e.getPoint());
//Makes sure that where you clicked was an element and that it is a String(We know it will be but its better to be safe)
if(list.getModel().getElementAt(index) != null&&list.getModel().getElementAt(index) instanceof String){
//Populates your textField with the element at this index
textField.setText(list.getModel().getElementAt(index));
}
}
}
});
Hope it helps!
Upvotes: 4