@Override public Component getListCellRendererComponent( JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus) { label.setText(Objects.toString(value, "")); this.list = list; this.index = index; if (isSelected) { setBackground(list.getSelectionBackground()); label.setForeground(list.getSelectionForeground()); } else { setBackground(index % 2 == 0 ? EVEN_COLOR : list.getBackground()); label.setForeground(list.getForeground()); } MutableComboBoxModel m = (MutableComboBoxModel) list.getModel(); if (index < 0 || m.getSize() - 1 <= 0) { setOpaque(false); deleteButton.setVisible(false); label.setForeground(list.getForeground()); } else { boolean f = index == rolloverIndex; setOpaque(true); deleteButton.setVisible(true); deleteButton.getModel().setRollover(f); deleteButton.setForeground(f ? Color.WHITE : list.getForeground()); } return this; }
@Override public void updateUI() { if (Objects.nonNull(cbml)) { JList<?> list = getList(); if (Objects.nonNull(list)) { list.removeMouseListener(cbml); list.removeMouseMotionListener(cbml); } } super.updateUI(); setRenderer(new ButtonsRenderer<E>(this)); JList<?> list = getList(); if (Objects.nonNull(list)) { cbml = new CellButtonsMouseListener(); list.addMouseListener(cbml); list.addMouseMotionListener(cbml); } }
@Override public void mousePressed(MouseEvent e) { JList list = (JList) e.getComponent(); Point pt = e.getPoint(); int index = list.locationToIndex(pt); if (index >= 0) { JButton button = getButton(list, pt, index); if (Objects.nonNull(button)) { listRepaint(list, list.getCellBounds(index, index)); } } }
@Override public void mouseMoved(MouseEvent e) { JList list = (JList) e.getComponent(); Point pt = e.getPoint(); int index = list.locationToIndex(pt); if (!list.getCellBounds(index, index).contains(pt)) { if (prevIndex >= 0) { Rectangle r = list.getCellBounds(prevIndex, prevIndex); listRepaint(list, r); } index = -1; prevButton = null; return; } if (index >= 0) { JButton button = getButton(list, pt, index); ButtonsRenderer renderer = (ButtonsRenderer) list.getCellRenderer(); if (Objects.nonNull(button)) { renderer.rolloverIndex = index; if (!button.equals(prevButton)) { Rectangle r = list.getCellBounds(prevIndex, index); listRepaint(list, r); } } else { renderer.rolloverIndex = -1; Rectangle r = null; if (prevIndex == index) { if (prevIndex >= 0 && Objects.nonNull(prevButton)) { r = list.getCellBounds(prevIndex, prevIndex); } } else { r = list.getCellBounds(index, index); } listRepaint(list, r); prevIndex = -1; } prevButton = button; } prevIndex = index; }
private static void listRepaint(JList list, Rectangle rect) { if (Objects.nonNull(rect)) { list.repaint(rect); } }