当用户双击该项目时,我尝试从JTextArea
填充JList
。我不完全确定如何实现这一点,这是我到目前为止所做的。
// 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());
}
});
答案 0 :(得分:4)
您只需添加一个mouseClicked侦听器,并检查鼠标是否单击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));
}
}
}
});
希望它有所帮助!
答案 1 :(得分:0)
我试图在用户双击项目时从JList填充JTextArea。
在设计GUI时,用户应该能够使用鼠标或键盘来调用组件上的Action。
结帐List Action。当您Action
或使用double click
密钥时,它会调用Enter
。您所要做的就是创建Action
。