Initial commit
This commit is contained in:
21
src/main/java/org/lumijiez/gui/util/ComboRenderer.java
Normal file
21
src/main/java/org/lumijiez/gui/util/ComboRenderer.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package org.lumijiez.gui.util;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
// Generic function that takes a ComboBox with a <T> object and sets the DefaultListCellRenderer according to the object
|
||||
public class ComboRenderer {
|
||||
public static <T> void setRenderer(JComboBox<T> comboBox, T[] items) {
|
||||
comboBox.setModel(new DefaultComboBoxModel<>(items));
|
||||
comboBox.setRenderer(new DefaultListCellRenderer() {
|
||||
@Override
|
||||
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
||||
if (value != null) {
|
||||
setText(value.toString());
|
||||
}
|
||||
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user