La selezione e' multipla. Il problema scatta durante la selezione di piu' stringhe alla volta col ctrl premuto. Se viene selezionata solo una stringa la stampa va a buon fine, nel caso della selezione multipla mi stampa la selezione multipla + la selezione precedente. Pubblico il codice del MouseListener e del valueChanged() del ActionListner della JList.
class ClickListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
if (e.getSource() != null) {
JList<String> list = (JList<String>) e.getSource();
String name = list.getName();
for (Map.Entry<String, JList<String>> val : labelMap.entrySet()) {
if (val.getKey().equals(name)) {
selectedList = val.getValue();
updateList(selectedList);
switch (name) {
case "ENTITY":
entity.setEntityName(selectedList.getSelectedValue());
break;
case "SIGNAL":
List<String> selectedValues = selectedList.getSelectedValuesList();
/*
for (String s : selectedValues) {
if (s.contains("in")) {
input.add(s);
} else {
output.add(s);
}
}
*/
signal.addAll(selectedValues);
for(String str: signal)
System.out.println(str);
break;
case "COMPONENT":
component.setCompName(selectedList.getSelectedValue());
break;
case "PORT":
port.setPortName(selectedList.getSelectedValue());
break;
default:
throw new IllegalStateException("Unexpected value: " + name);
}
}
}
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
ActionListner
class AddListener implements ActionListener, DocumentListener {
private boolean alreadyEnabled = false;
private JButton button;
AddListener(JButton button) {
this.button = button;
}
//Required by ActionListener.
public void actionPerformed(ActionEvent e) {
String name = textField.getText();
if (name.equals("") || alreadyInList(name)) {
Toolkit.getDefaultToolkit().beep();
textField.requestFocusInWindow();
textField.selectAll();
return;
}
int index = selectedList.getSelectedIndex();
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item
index++;
}
listModel.insertElementAt(name, index);
//Reset the text field.
textField.requestFocusInWindow();
textField.setText("");
//Select the new item and make it visible.
selectedList.setSelectedIndex(index);
selectedList.ensureIndexIsVisible(index);
}
boolean alreadyInList(String name) {
return listModel.contains(name);
}
//Required by DocumentListener.
public void insertUpdate(DocumentEvent e) {
enableButton();
}
//Required by DocumentListener.
public void removeUpdate(DocumentEvent e) {
handleEmptyTextField(e);
}
//Required by DocumentListener.
public void changedUpdate(DocumentEvent e) {
if (!handleEmptyTextField(e)) {
enableButton();
}
}
private void enableButton() {
if (!alreadyEnabled) {
button.setEnabled(true);
}
}
private boolean handleEmptyTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
button.setEnabled(false);
alreadyEnabled = false;
return true;
}
return false;
}
}