我有一个带有字符串数组的JList。基本上它显示餐馆菜单。 在JList旁边我有另一个空的JList。每当用户双击第一个JList(显示菜单的位置)中的字符串时,我希望它显示在旁边的下一个JList上。
我该怎么做?
答案 0 :(得分:12)
你可以尝试
final JList list = new JList(dataModel);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
String selectedItem = (String) list.getSelectedValue();
// add selectedItem to your second list.
DefaultListModel model = (DefaultListModel) list2.getModel();
if(model == null)
{
model = new DefaultListModel();
list2.setModel(model);
}
model.addElement(selectedItem);
}
}
};
list.addMouseListener(mouseListener);
答案 1 :(得分:2)
您可能还想通过添加KeyListener按下Enter键来执行此操作:
07.08.2015 16:11:36 AMgr: Agent ('TestLDAP' in 'test/tel_sez3.nsf') error message: org.apache.directory.ldap.client.api.exception.InvalidConnectionException: Cannot connect on the server, the connection is null
07.08.2015 16:11:36 AMgr: Agent ('TestLDAP' in 'test/tel_sez3.nsf') error message: at org.apache.directory.ldap.client.api.LdapNetworkConnection.checkSession(LdapNetworkConnection.java:270)
07.08.2015 16:11:36 AMgr: Agent ('TestLDAP' in 'test/tel_sez3.nsf') error message: at org.apache.directory.ldap.client.api.LdapNetworkConnection.bindAsync(LdapNetworkConnection.java:1274)
07.08.2015 16:11:36 AMgr: Agent ('TestLDAP' in 'test/tel_sez3.nsf') error message: at org.apache.directory.ldap.client.api.LdapNetworkConnection.bind(LdapNetworkConnection.java:1185)
07.08.2015 16:11:36 AMgr: Agent ('TestLDAP' in 'test/tel_sez3.nsf') error message: at org.apache.directory.ldap.client.api.LdapNetworkConnection.bind(LdapNetworkConnection.java:1043)
07.08.2015 16:11:36 AMgr: Agent ('TestLDAP' in 'test/tel_sez3.nsf') error message: at JavaAgent.NotesMain(Unknown Source)
07.08.2015 16:11:36 AMgr: Agent ('TestLDAP' in 'test/tel_sez3.nsf') error message: at lotus.domino.AgentBase.runNotes(Unknown Source)
07.08.2015 16:11:36 AMgr: Agent ('TestLDAP' in 'test/tel_sez3.nsf') error message: at lotus.domino.NotesThread.run(Unknown Source)
我知道这不是双击,但有些人想要按照我想要的方式使用Enter按钮。
答案 2 :(得分:1)
public void addActionListener(final ActionListener al) {
jList.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
al.actionPerformed(new ActionEvent(e.getSource(), e.getID(), "ENTER"));
}
}
});
jList().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
al.actionPerformed(new ActionEvent(e.getSource(), e.getID(), "ENTER"));
}
}
});
}
答案 3 :(得分:0)
我已经在你的代码中已经完成了另一个问题吗? [link] I want to add an action listener from one JList to another JList and how can a JList appear with out any text inside?
唯一的想法是你必须把它放到@Bala R的if语句中检查点击次数:
if(e.getClickCount()== 2){
//你的代码
}
实际上你最好使用addElement(selectedItem);方法,如在@Bala R的代码中而不是 add(orderList.getModel()。getSize(),selectedItem);在我的代码中。两者都将项添加到最后,但addElement看起来更好,您不需要检索模型的大小。
Oi,Boro。