问题
I have JList that is loaded with Object has its ListCellRenderer and mouseListener, here is the code:
MyListCellRenderer:
public class MyListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component cell = null;
if (value instanceof Domain) {
Domain domain = (Domain)value;
int status = domain.getStatus();
String naziv = domain.getNaziv();
cell = super.getListCellRendererComponent(list,naziv,index, isSelected, cellHasFocus);
if (status == 4 & !isSelected) {
cell.setBackground( Color.red );
cell.setForeground(Color.WHITE);
}
}
return cell;
}
}
JList:
DefaultListModel<Domain> modelRN = new DefaultListModel<Domain>();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 86, 390, 199);
contentPane.add(scrollPane);
JList<Domain> listRN = new JList<Domain>(modelRN);
scrollPane.setViewportView(listRN);
listRN.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
listRN.setSelectionModel(m);
listRN.setCellRenderer( new MyListCellRenderer());
Domain class:
public class Domain {
private Integer id;
private String naziv;
private Integer status;
public Domain(){}
public Integer getId() {return id;}
public void setId(int i){id = i;}
public String getNaziv(){return naziv;}
public void setNaziv(String n){naziv = n;}
public Integer getStatus(){return status;}
public void setStatus(int s){status = s;}
public String toString(){return naziv; }
}
That is the setup. What am I doing is getting data from a data base and loading it to a JList. The next step is the one when I click on a row in the list I want another Jlist to load corresponding filed from a data base.
Everything is working fine. I get the right values at my terminal but when I try to load the values to the other JList the first one that triggered the job is acting strange.What I mean by acting strange is the following. The list that has a mouseListener attached to it has 9 Domain elements in it(it only shows Domain.getName()). When I make a selection(one click) I get the right data in the second Jlist loaded, but the selection marker moves in the first one on its own. It moves one spot down, and it happens 9 times, and I don't have a clue why is that happening.
This is the listener code:
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
d = listRN.getSelectedValue(); //getting the object from the first list
System.out.println(d.getNaziv());
}
//RN-->>PL
ArrayList<Domain> x = new DBsearch(d).Conn();
modelPL.clear();
for(int i=0;i<x.size();i++){
modelPL.addElement(x.get(i)); //adding to second List
System.out.println(x.get(i).getNaziv());
}
}
};
listRN.addMouseListener(mouseListener);
Does anyone have a clue to whats going on here? Thanks
来源:https://stackoverflow.com/questions/27063733/mouselistener-has-my-jlist-acting-strange