Ciao, programmatori!
Sto cercando di risolvere un problema che mi pare facile ma non riesco a risolverlo fino alla fine, spero che mi possiate aiutare.
Sto lavorando su un programma abbastanza primitivo che lavora principalmente con le stringhe, sto cercando di far apparire il contenuto in una JList tramite il click su uno degli elementi dell'altra JList.Vi faccio un esempio, se nella prima JList ho il numero di maglia di Michael Jordan, che e' 23, cliccandoci sopra mi escono i suoi nome e cognome nell'altra JList.
Ho affrontato il mio problema creando una HashMap composta da chiave di tipo String e da valore di tipo DefaultListModel<String> perche' nel mio caso lavoro con le porte logiche ed i corrispettivi segnali. Potete vederlo nell'immagine allegato sotto. Ho fatto delle prove sia con il MouseListener sia con il ListSelectionListener della lista e non riesco a visualizzare il contenuto nella JList "COMPONENT SIGNALS".
Quindi, cliccando sull'elemento or della JList COMPONENT NAMES mi devono uscire i suoi segnali nella JList a fianco.
Vi allego il codice.
Grazie dell'aiuto.
MouseListener
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();
System.out.println(list.getName());
for (Map.Entry<String, JList<String>> val : labelMap.entrySet()) {
if (val.getKey().equals(list.getName())) {
updateList(val.getValue());
switch (list.getName()) {
case "MODEL ENTITY":
setEntity();
break;
case "MODEL COMPONENT":
setComponent();
//removes duplicates
break;
case "MODEL SIGNALS":
List<String> values = selectedList.getSelectedValuesList();
for (String s : values) {
if (!(signal.contains(s)))
signal.add(s);
}
break;
default:
throw new IllegalStateException("Unexpected value: " + list.getName());
}
}
}
// * char as regex
if (list.getName().equals("COMPWIRES")) {
updateList(list);
buffer.append(selectedList.getSelectedValue()).append("*");
}
if (list.getName().equals("ENTWIRES")) {
updateList(list);
buffer.append(selectedList.getSelectedValue()).append("*");
}
if (list.getName().equals("SIGNALS")) {
updateList(list);
buffer.append(selectedList.getSelectedValue()).append("*");
}
if (list.getName().equals("COMPNAMES")) {
updateList(list);
componentName = selectedList.getSelectedValue();
for(Map.Entry<String, DefaultListModel<String>> kv: components.entrySet()) {
if(kv.getKey().equals(componentName)) {
componentModel = kv.getValue();
}
}
System.out.println(componentName);
}
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
Parte del frame con le liste
private Map<String, DefaultListModel<String>> components = new HashMap<>();
private void createInstance() {
JList compWires;
JList compNames;
DefaultListModel<String> cNames = new DefaultListModel<>();
DefaultListModel<String> compPort = new DefaultListModel<>();
for (Component c : component) {
//Component's name
if (!cNames.contains(c.getName())) {
cNames.addElement(c.getName());
}
if (!compPort.contains(c.getPort().getInput()) && !compPort.contains(c.getPort().getOutput())) {
String input = c.getPort().getInput();
String output = c.getPort().getOutput();
String iType = input.substring(input.indexOf(":") + 2, input.indexOf(":") + 4);
String iDataType = input.substring(input.indexOf(":") + 5);
String oType = output.substring(output.indexOf(":") + 2, output.indexOf(":") + 5);
String oDataType = output.substring(output.indexOf(":") + 6);
if (!input.contains(",")) {
compPort.addElement(input);
} else {
ArrayList<String> inputs = new ArrayList<>(Arrays.asList(input.split(",")));
for (int i = 0; i < inputs.size() - 1; i++) {
String s = inputs.get(i);
StringBuilder aux = new StringBuilder();
if (s.charAt(0) == ' ') {
s = s.substring(1);
}
aux.append(s).append(": ").append(iType).append(" ").append(iDataType);
s = aux.toString();
compPort.addElement(s);
aux.setLength(0);
}
int index = inputs.size() - 1;
String s = inputs.get(index);
if (s.charAt(0) == ' ')
s = s.substring(1);
compPort.addElement(s);
}
if (!output.contains(",")) {
compPort.addElement(output);
} else {
ArrayList<String> outputs = new ArrayList<>(Arrays.asList(output.split(",")));
for (int i = 0; i < outputs.size() - 1; i++) {
String s = outputs.get(i);
if (!s.contains(oType)) {
if (s.charAt(0) == ' ')
s = s.substring(1);
StringBuilder aux = new StringBuilder();
aux.append(s).append(": ").append(oType).append(" ").append(oDataType);
s = aux.toString();
compPort.addElement(s);
aux.setLength(0);
}
int index = outputs.size() - 1;
String tmp = outputs.get(index);
if (tmp.charAt(0) == ' ')
tmp = tmp.substring(1);
compPort.addElement(tmp);
}
}
}
//adds component's name + its signals list
components.put(c.getName(), compPort);
}
System.out.println("map elements");
System.out.println(Collections.singletonList(components));
compNames = new JList(cNames);
compNames.setName("COMPNAMES");
compNames.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
compNames.setVisibleRowCount(-1);
compNames.getSelectionModel().addListSelectionListener(this);
compNames.addMouseListener(new ClickListener());
JScrollPane compNamesScroller = new JScrollPane(compNames);
compNamesScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel compNamesContainer = new JPanel(new BorderLayout());
compNamesContainer.add(compNamesScroller);
compNamesContainer.setBorder(BorderFactory.createTitledBorder("COMPONENT NAMES"));
compWires = new JList();
compWires.setName("COMPWIRES");
compWires.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
compWires.setVisibleRowCount(-1);
compWires.getSelectionModel().addListSelectionListener(this);
compWires.addMouseListener(new ClickListener());
JScrollPane compWiresScroller = new JScrollPane(compWires);
compWiresScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel componentWiresContainer = new JPanel(new BorderLayout());
componentWiresContainer.add(compWiresScroller);
componentWiresContainer.setBorder(BorderFactory.createTitledBorder("COMPONENT SIGNALS"));
//entity list
JList entWires;
DefaultListModel<String> entPort = new DefaultListModel<>();
String input = entity.getPort().getInput();
String output = entity.getPort().getOutput();
String iType = input.substring(input.indexOf(":") + 2, input.indexOf(":") + 4);
String iDataType = input.substring(input.indexOf(":") + 5);
String oType = output.substring(output.indexOf(":") + 2, output.indexOf(":") + 5);
String oDataType = output.substring(output.indexOf(":") + 6);
if (!input.contains(",")) {
entPort.addElement(input);
} else {
ArrayList<String> inputs = new ArrayList<>(Arrays.asList(input.split(",")));
for (int i = 0; i < inputs.size() - 1; i++) {
String s = inputs.get(i);
StringBuilder aux = new StringBuilder();
if (s.charAt(0) == ' ') {
s = s.substring(1);
}
aux.append(s).append(": ").append(iType).append(" ").append(iDataType);
s = aux.toString();
entPort.addElement(s);
aux.setLength(0);
}
int index = inputs.size() - 1;
String s = inputs.get(index);
if (s.charAt(0) == ' ')
s = s.substring(1);
entPort.addElement(s);
}
if (!output.contains(",")) {
entPort.addElement(output);
} else {
ArrayList<String> outputs = new ArrayList<>(Arrays.asList(output.split(",")));
for (int i = 0; i < outputs.size() - 1; i++) {
String s = outputs.get(i);
if (!s.contains(oType)) {
if (s.charAt(0) == ' ')
s = s.substring(1);
StringBuilder aux = new StringBuilder();
aux.append(s).append(": ").append(oType).append(" ").append(oDataType);
s = aux.toString();
entPort.addElement(s);
aux.setLength(0);
}
int index = outputs.size() - 1;
String tmp = outputs.get(index);
if (tmp.charAt(0) == ' ')
tmp = tmp.substring(1);
entPort.addElement(tmp);
}
}
entWires = new JList(entPort);
entWires.setName("ENTWIRES");
entWires.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
entWires.setVisibleRowCount(-1);
entWires.getSelectionModel().addListSelectionListener(this);
entWires.addMouseListener(new ClickListener());
JScrollPane entityWiresScroller = new JScrollPane(entWires);
entityWiresScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel entityContainer = new JPanel(new BorderLayout());
entityContainer.add(entityWiresScroller);
entityContainer.setBorder(BorderFactory.createTitledBorder("ENTITY SIGNALS"));
DefaultListModel<String> signals = new DefaultListModel<>();
for (String x : signal) {
signals.addElement(x);
}
JList signalsList = new JList(signals);
signalsList.setName("SIGNALS");
signalsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
signalsList.setVisibleRowCount(-1);
signalsList.getSelectionModel().addListSelectionListener(this);
signalsList.addMouseListener(new ClickListener());
JScrollPane signalsScroller = new JScrollPane(signalsList);
signalsScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel signalsContainer = new JPanel(new BorderLayout());
signalsContainer.add(signalsScroller);
signalsContainer.setBorder(BorderFactory.createTitledBorder("SIGNAL"));
JPanel listContainer = new JPanel(new GridLayout());
listContainer.add(compNamesContainer);
listContainer.add(componentWiresContainer);
listContainer.add(signalsContainer);
listContainer.add(entityContainer);
JButton bindingButton = new JButton("BIND");
bindingButton.setActionCommand("BIND");
bindingButton.addActionListener(new BindingListener());
JButton addInstanceButton = new JButton("ADD INSTANCE");
addInstanceButton.setActionCommand("ADD INSTANCE");
addInstanceButton.addActionListener(new AddInstanceListener());
JPanel buttonContainer = new JPanel(new GridLayout(2, 0));
buttonContainer.add(bindingButton);
buttonContainer.add(addInstanceButton);
JPanel mainContainer = new JPanel(new BorderLayout());
mainContainer.add(listContainer);
mainContainer.add(buttonContainer, BorderLayout.EAST);
JFrame frame = new JFrame();
frame.setSize(1200, 200);
frame.setTitle("Bindings");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.add(mainContainer);
}