Bueno, bueno, si uso el código del ejemplo, así nomás anda bien:
Código Java:
Ver originalpackage prueba;
import java.awt.*;
import javax.swing.*;
public class Cuerpo
extends JPanel { private Cuerpo() {
CheckableItem[] m = {
new CheckableItem("aaa", false),
new CheckableItem("bbbbb", true),
new CheckableItem("111", false),
new CheckableItem("33333", true),
new CheckableItem("2222", true),
new CheckableItem("ccccccc", false)
};
p.
add(new JLabel("Default:")); p.add(new JComboBox<>(m));
p.
add(Box.
createVerticalStrut(20)); p.
add(new JLabel("CheckedComboBox:")); p.add(new CheckedComboBox<>(new CheckableComboBoxModel<>(m)));
}
public static void main
(String...
args) { @Override public void run() {
createAndShowGui();
}
});
}
public static void createAndShowGui() {
try {
ex.printStackTrace();
}
frame.getContentPane().add(new Cuerpo());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Código Java:
Ver originalpackage prueba;
import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.event.*;
import javax.accessibility.Accessible;
import javax.swing.*;
import javax.swing.plaf.basic.ComboPopup;
public class CheckedComboBox <E extends CheckableItem> extends JComboBox<E> {
private boolean keepOpen;
protected CheckedComboBox() {
super();
}
protected CheckedComboBox(ComboBoxModel<E> model) {
super(model);
}
@Override
}
@Override
public void updateUI() {
setRenderer(null);
removeActionListener(listener);
super.updateUI();
listener = e -> {
if ((e.
getModifiers() & AWTEvent.
MOUSE_EVENT_MASK) != 0) { updateItem(getSelectedIndex());
keepOpen = true;
}
};
setRenderer(new CheckBoxCellRenderer<>());
addActionListener(listener);
Accessible a
= getAccessibleContext
().
getAccessibleChild(0); updateItem
(((ComboPopup) a
).
getList().
getSelectedIndex()); }
}
});
}
protected void updateItem(int index) {
if (isPopupVisible()) {
CheckableItem item = getItemAt(index);
item.setSelected(!item.isSelected());
ComboBoxModel<E> m = getModel();
if (m instanceof CheckableComboBoxModel) {
((CheckableComboBoxModel<E>) m).fireContentsChanged(index);
}
}
}
@Override
public void setPopupVisible(boolean v) {
if (keepOpen) {
keepOpen = false;
} else {
super.setPopupVisible(v);
}
}
}
Código Java:
Ver originalpackage prueba;
public class CheckableItem {
private boolean selected;
protected CheckableItem
(String text,
boolean selected
) { this.text = text;
this.selected = selected;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
@Override
return text;
}
}
Código Java:
Ver originalpackage prueba;
import javax.swing.DefaultComboBoxModel;
public class CheckableComboBoxModel <E> extends DefaultComboBoxModel<E> {
@SuppressWarnings("PMD.UseVarargs")
public CheckableComboBoxModel(E[] items) {
super(items);
}
public void fireContentsChanged(int index) {
super.fireContentsChanged(this, index, index);
}
}
Código Java:
Ver originalpackage prueba;
import java.awt.Component;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.swing.*;
public class CheckBoxCellRenderer<E extends CheckableItem> implements ListCellRenderer<E> {
@Override
public Component getListCellRendererComponent
(JList
<? extends E
> list, E value,
int index, boolean isSelected, boolean cellHasFocus) {
if (index < 0) {
String txt
= getCheckedItemString
(list.
getModel()); label.setText(txt.isEmpty() ? " " : txt);
return label;
} else {
check.setText(Objects.toString(value, ""));
check.setSelected(value.isSelected());
if (isSelected) {
check.setBackground(list.getSelectionBackground());
check.setForeground(list.getSelectionForeground());
} else {
check.setBackground(list.getBackground());
check.setForeground(list.getForeground());
}
return check;
}
}
private static <E
extends CheckableItem
> String getCheckedItemString
( ListModel<E> model) {
return IntStream.range(0, model.getSize())
.mapToObj(model::getElementAt)
.filter(CheckableItem::isSelected)
.map(Objects::toString)
.sorted()
.collect(Collectors.joining(", "));
}
}
Sin tocarle el código funciona bien, solo copien su código y verán a lo que me refiero, ahora si hago lo mismo con un jframe directamente no me pone nada, lo único que le cambio es esto:
Código Java:
Ver originalpackage app.vista.interfaz;
import app.vista.util.CheckableComboBoxModel;
import app.vista.util.CheckableItem;
import app.vista.util.CheckedComboBox;
public class probando
extends javax.
swing.
JFrame { public probando() {
initComponents();
CheckableItem[] m = {
new CheckableItem("aaa", false),
new CheckableItem("bbbbb", true),
new CheckableItem("111", false),
new CheckableItem("33333", true),
new CheckableItem("2222", true),
new CheckableItem("ccccccc", false)
};
cboxprueba = new CheckedComboBox(new CheckableComboBoxModel(m));
/*
cboxprueba.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
SwingUtilities.invokeLater(() -> {
String criterio = cboxprueba.getEditor().getItem().toString();
cargarServicios(criterio);
});
}
});*/
}
/*
private void cargarServicios(String criterio){
ModeloServicioCombo modelo = new ModeloServicioCombo(
criterio.toUpperCase());
if(modelo.getSize() > 0){
cboxprueba.setModel(modelo);
//cboxprueba.setRenderer(new CeldaServicioCombo());
cboxprueba.setRenderer(new CheckBoxCellRenderer());
cboxprueba.setSelectedItem(criterio);
cboxprueba.showPopup();
} else {
cboxprueba.hidePopup();
}
}*/
// Variables declaration - do not modify
private javax.swing.JComboBox<String> cboxprueba;
private javax.
swing.
JPanel pnlpantalla
; // End of variables declaration
}
¿Ustedes le encuentran la falla? porque no lo he hecho.
Espero sus respuestas y saludos.