Taking this example by mKorbel as an example:
I have achieved creating the desired behavior by using the mechanisms suggested by mKorbel. This works as long as I have one row painted green (unselected). However if I add one row, select it and then add a new row, it works fine, I get a new unselected row. If I add a second row and I selected then it is painted red correctly, but upon adding more rows after the second row (two red rows) they are all selected by default, and that is not what I want. I want all the rows in green until I click on them (double click). Does anyone know why is this happening? Why does it work as long as I have 1 cell unselected? why if, I have more than two or all the, rows selected, it keeps adding new rows in selected mode? THNX
My code for the mouse event is as follows:
m_list = new JList<String>(m_listModel)
{
private MyCellRenderer cellRenderer = new MyCellRenderer();
// emulate control down for multiple non contiguous selection on the
// list.
@Override
// TODO fix here
public void processMouseEvent(MouseEvent event) {
int modifiers = event.getModifiers() | InputEvent.CTRL_MASK;
m_myME = new MouseEvent((Component) event.getSource(),
event.getID(), event.getWhen(), modifiers,
event.getX(), event.getY(), event.getXOnScreen(),
event.getYOnScreen(), event.getClickCount(),
event.isPopupTrigger(), event.getButton());
//if clicked twice
if (event.getClickCount() == 2) {
//if the flag is set to true consume event
if ((MyCellRenderer.getFlag() == true)) {
m_urlName = MyCellRenderer.getValue();
m_myME.consume();
//initiate parsing
initiateParsing();
}else{
m_urlName = MyCellRenderer.getValue();
}
//if it is not consume it will emulate CTRL_MASK
if (!m_myME.isConsumed()) {
super.processMouseEvent(m_myME);
m_urlName = MyCellRenderer.getValue();
//initiate parsing process
initiateParsing();
}
}
}
};
The code in the CellRenderer is as follows:
public static class MyCellRenderer extends JLabel implements
ListCellRenderer {
private static final long serialVersionUID = 1L;
private static boolean myFlag = false;
private static String thisValue;
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText(value.toString());
Color background = null;
Color foreground = null;
if (isSelected == true) {
background = Color.RED;
foreground = Color.WHITE;
myFlag = true;
} else {
background = Color.GREEN;
foreground = Color.BLACK;
myFlag = false;
}
setBackground(background);
setForeground(foreground);
public static class MyCellRenderer extends JLabel implements
ListCellRenderer {
private static final long serialVersionUID = 1L;
private static boolean myFlag = false;
private static String thisValue;
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText(value.toString());
Color background = null;
Color foreground = null;
if (isSelected == true) {
background = Color.RED;
foreground = Color.WHITE;
myFlag = true;
} else {
background = Color.GREEN;
foreground = Color.BLACK;
myFlag = false;
}
setBackground(background);
setForeground(foreground);
// the string where its pointing at
thisValue = value.toString();
m_index = index;
return this;
}
public static boolean getFlag() {
return myFlag;
}
public static String getValue() {
return thisValue;
}
}