custom JList Cell Renderer - Cell Selection? - java

Good day.
I'm pretty new to CellRenderer and stuff and I am still reading a lot about it. Right now, I'm stuck with something I'm not sure how to work. I was able to put a JPanel inside my JList by setting it's CellRenderer to a custom one. But here is the million dollar question: How can I interact with the controls?
I mean, I want to be able to right click into the cell that contains my jPanel,
Right click to show some actions, and Highlight the selected row.
How can I achieve this?
heres is my code:
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class cellTest extends JPanel{
JLabel label;
public cellTest(){
buildInterface();
}
private void buildInterface(){
label = new JLabel();
setLayout(new CardLayout());
add(label);
}
public void setText(String text){
String pre = "<html><body style='width: 200px;'>";
label.setText(pre+text);
}
}
Second Part:
import Read.Medicine_Cell;
import java.awt.CardLayout;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class testjava {
public static void main(String args[]){
JFrame jf = new JFrame();
jf.setSize(500, 500);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new CardLayout());
JList list = new JList();
DefaultListModel dlm = new DefaultListModel();
dlm.addElement("This is a test");
dlm.addElement("I wanna know if it works");
dlm.addElement("Pardon if im not that good yet");
dlm.addElement("or my problem is basic");
list.setModel(dlm);
list.setCellRenderer(new MyCellRenderer());
jf.add(new JScrollPane(list));
jf.show();
}
static class MyCellRenderer extends DefaultListCellRenderer{
final cellTest cellx = new cellTest();
#Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean hasFocus){
final String text = (String) value;
cellx.setText(text);
return cellx;
}
}
}

Related

JComboBox with JTable as ListCellRenderer component

I am currently working on a JComboBox component where I want to have a JTable inside of a ComboBox for a drop down selection. I have extended the ListCellRenderer and I have the table inside of the popup.
I want to present it two different ways. The first as a label of a bound column of the selected row when the popup is not visible. The second is to show the table with a JScrollPane when the popup is visible.
Unfortunately, When I do this the popup is being shrunk to the row height of the list which only leaves room for the columns of the table.
If I just use the scrollpane I can see the full table but then when the popup is not visible I see the table inside the combobox which is not what I want.
How can I set the height such that the table can fit while still being able to show the label only when the popup is not visible?
Below is a minimal example that compiles and runs:
import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class TableComboBoxMain {
public static void main(String[] args) {
JTableComboBox<Employee> combo = new JTableComboBox<>();
combo.addItem(new Employee("April",3));
//combo.setMaximumRowCount(10);
combo.setRenderer(new TableListCellRenderer(combo));
JFrame frame = new JFrame("Test Table Combo Box");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(combo);
frame.pack();
frame.setVisible(true);
}
public static class Employee{
String name;
int nr;
public Employee(String name, int number ){
this.name =name;
this.nr = number;
}
}
public static class JTableComboBox<E> extends JComboBox<E> implements ListSelectionListener{
#Override
public void valueChanged(ListSelectionEvent e) {System.out.println("Row selected"+e.getFirstIndex());
this.setSelectedIndex(e.getFirstIndex());
}
}
public static class TableListCellRenderer extends JScrollPane implements ListCellRenderer{
JTable table;
JTableComboBox combo;
boolean mouseListenerAdded;
public TableListCellRenderer(JTableComboBox combo){
this.combo=combo;
DefaultTableModel model = new DefaultTableModel();
String[] cols1 = new String[]{"1","2","3","4"};
String[] cols2 = new String[]{"Mark","John","April","Mary"};
model.addColumn("Nr", cols1);model.addColumn("Name",cols2);
table = new JTable(model);table.setShowGrid(true);table.setGridColor(Color.gray);
this.getViewport().add(table);
}
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if(!mouseListenerAdded){
list.addMouseListener(this.getListener());
mouseListenerAdded = true;
}//If this is uncommented then the BasicComboPopup size is always no more than 1 row?!!
if(!combo.isPopupVisible()){
Employee emp = (Employee) value;
return new JLabel(emp.name);
}else
return this;
}
public MouseAdapter getListener(){
MouseAdapter adapter = new MouseAdapter(){
#Override
public void mousePressed(MouseEvent e) {
if(combo.isPopupVisible()){
System.out.println("MouseClicked over list");
int row = table.rowAtPoint(e.getPoint());
if(row>0){
table.setRowSelectionInterval(row-1, row-1);
ListDataEvent event = new ListDataEvent(combo,ListDataEvent.CONTENTS_CHANGED,row-1,row-1);
combo.contentsChanged(event);
}
}
}
};
return adapter;
}
}
}
So I found a solution to the problem. I am not finished yet because there are a few interactions I still want to have:
I want to be able to move columns
I want to be able have popup menu for columns
I want to be able to scroll vertically with the mouse
I wanted to post the solution in case anyone else wants a starting point example. I will update my answer as I solve these additional issues.
Below is the test class:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.table.DefaultTableModel;
public class TableComboBoxMain {
public static void main(String[] args) {
JTableComboBox<String> combo = new JTableComboBox<>();
combo.addItem("");
BasicComboBoxUI ui = new BasicComboBoxUI(){
#Override
protected ComboPopup createPopup() {
return new BasicComboPopup( comboBox ){
#Override
protected int getPopupHeightForRowCount(int maxRowCount) {
return 100;
}
#Override
protected JScrollPane createScroller() {
JScrollPane sp = new JScrollPane( list,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
sp.getHorizontalScrollBar().setFocusable( false );
return sp;
}
};
}
};
combo.setUI(ui);
combo.setRenderer(new TableListCellRenderer(combo));
JFrame frame = new JFrame("Test Table Combo Box");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(combo);
frame.pack();
frame.setVisible(true);
}
public static class Employee{
String name;
int nr;
public Employee(String name, int number ){
this.name =name;
this.nr = number;
}
}
public static class JTableComboBox<E> extends JComboBox<E> implements ListSelectionListener{
#Override
public void valueChanged(ListSelectionEvent e) {
System.out.println("Row selected"+e.getFirstIndex());
this.setSelectedIndex(e.getFirstIndex());
}
}
public static class TableListCellRenderer extends DefaultListCellRenderer{
JTable table;
JTableComboBox combo;
JPanel renderer = new JPanel();
JPanel colHeader = new JPanel();
JScrollPane scroll = new JScrollPane();
boolean mouseListenerAdded;
public TableListCellRenderer(JTableComboBox combo){
this.combo=combo;
DefaultTableModel model = new DefaultTableModel();
String[] cols1 = new String[]{"1","2","3","4","5","6"};
String[] cols2 = new String[]{"Mark","John","April","Mary","Joe","Jack"};
model.addColumn("Nr", cols1);model.addColumn("Name",cols2);
table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
colHeader.add(table.getTableHeader());
renderer.setLayout(new BorderLayout());
renderer.add(colHeader, BorderLayout.NORTH);
renderer.add(table,BorderLayout.CENTER);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scroll.getViewport().add(table);
}
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
list.setFixedCellHeight(table.getRowHeight()*(table.getRowCount()+1)+10);
list.setFixedCellWidth(table.getPreferredSize().width);
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if(!mouseListenerAdded){
list.addMouseListener(this.getListener());
mouseListenerAdded = true;
}
if(!combo.isPopupVisible()){
label.setText("Select...");
if(table.getSelectedRowCount()>0)
label.setText(""+table.getModel().getValueAt(table.getSelectedRow(),1));
return label;
}
return scroll;
}
public MouseAdapter getListener(){
MouseAdapter adapter = new MouseAdapter(){
#Override
public void mousePressed(MouseEvent e) {
if(combo.isPopupVisible()){
System.out.println("MouseClicked over list");
int row = table.rowAtPoint(e.getPoint());
if(row>0){
table.setRowSelectionInterval(row-1, row-1);
ListDataEvent event = new ListDataEvent(combo,ListDataEvent.CONTENTS_CHANGED,row-1,row-1);
combo.contentsChanged(event);
}
}
}
};
return adapter;
}
}
}
The key parts of the solution is that in order to have the table and the features of a table that you would want in a popup is you need to override the UI. Specifically you need to override the createPopup on BasicComboBoxUI, getPopupHeightForRowCount and createScroller on BasicComboPopup. Lastly, when implementing getListCellRenderingComponent you have set a fixed height and width that matches the tables preferred height and width. This will allow the main scroller of the popup to act as the scrollpane for the table.

Add a JScrollPane to a JList

I have the following code:
Main:
package PackageMain;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class Main {
public static JFrame frame = new JFrame("Window");
public static PanelOne p1;
public static PanelTwo p2;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 800, 600);
p1 = new PanelOne();
p2 = new PanelTwo();
frame.setVisible(true);
} catch(Exception e){
}
}
});
}
And class 2:
package PackageMain;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class PanelOne{
public PanelOne(){
loadScreen();
}
public void loadScreen(){
JPanel p1 = new JPanel();
DefaultListModel model = new DefaultListModel<String>();
JList list = new JList<String>(model);
//
JScrollPane scroll = new JScrollPane(list);
list.setPreferredSize(null);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
scroll.setViewportView(list);
//
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
System.out.println("You selected " + list.getSelectedValue());
}
});
p1.add(list);
Main.frame.add(p1);
Main.frame.revalidate();
Main.frame.repaint();
for (int i = 0; i < 100; i++){
model.addElement("test");
}
}
I've tried a bunch of stuff to get the JScrollPane to appear on the JList, but it doesn't want to. My best guess is that the model is screwing things up, but this is a simplified version, and the model needs to be there.
JScrollPane scroll = new JScrollPane(list);
You add the JList to the JScrollPane which is correct.
p1.add(list);
But then you add the JList to the JPanel, which is incorrect. A component can only have a single parent, so theJListis removed from theJScrollPane`.
You need to add the JScrollPane to the JPanel:
p1.add( scroll );
You're adding the list to too many components: to the JScrollPane's viewport -- OK, but also to the p1 JPanel -- not OK. Add it only to the viewport, and then add the JScrollPane to the GUI (p1 if need be).
Also:
There's no need to add the JList to the JScrollPane twice, in the constructor and in the viewport view as you're doing, once is enough.
list.setPreferredSize(null);????
Just add Scroll Pane to the frame rather than the List.
change your line with the below code:
Main.frame.add(scroll);

Wrapping JLabels inside a JPanel thats inside a JScrollPane

My Java is a bit rusty so please bear with me. I have a method in my GUI class that calls another class file which returns a JList. The problem im having is getting the text from the JList, you can see an example of the output below
package com.example.tests;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.example.tests.IJ_runTestAFJ;
public class GUI_v2 extends JFrame
{
private static final long serialVersionUID = 1L;
IJ_CommonSetup setup = new IJ_CommonSetup();
Container c;
JPanel panel;
JScrollPane userScrollPane, errorScrollPane, sysScrollPane;
JTextArea tfUserError, tfSysError;
private JButton resetButton;
public JList<String> errorList;
GUI_v2()
{
resetButton = new JButton();
resetButton.setText("Click to populate TextArea");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
//test.runTest_Login(stUserName,stPwd);
updatePanel();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
panel = new JPanel();
tfSysError = new JTextArea(10,33);
tfSysError.setLineWrap(true);
tfSysError.setEditable(false);
tfSysError.setWrapStyleWord(false);
sysScrollPane = new JScrollPane(tfSysError);
sysScrollPane.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(sysScrollPane);
panel.add(resetButton);
c = getContentPane();
c.add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setSize(400,250); //width, height
setLocation(600,0);
setResizable(false);
validate();
}//close GUI
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI_v2().setVisible(true);
}
});
}
public void updatePanel()
{
errorList = new JList<String>();
errorList = setup.getErrorJList();
tfSysError.append(errorList.getComponent(1).toString());
validate();
}
}// end on class
IJ_CommonSetup.java
package com.example.tests;
import javax.swing.JLabel;
import javax.swing.JList;
public class IJ_CommonSetup{
/**
*
*/
public static String stError = new String();
public static JList<String> stJListError = new JList<String>();
public JList<String> getErrorJList(){
String error1 = new String("TestTestTestTestTestTestTestTestTestTestTestTestTestTest ");
String error2 = new String("ApplesApplesApplesApplesApplesApplesApplesApplesApplesApples ");
JLabel newError1 = new JLabel();
newError1.setText(error1);
JLabel newError2 = new JLabel(error2);
stJListError.add(newError1);
stJListError.add(newError2);
return stJListError;
}
}
im having some trouble getting labels to wrap inside a panel that's
inside a Scrollpane. At the moment if the string thats added to the
label is long it is aligned to the left which is fine but the label
stretches outside the panel cutting off the end of the string.
use JTextArea(int, int) in JScrollPane
setEditable(false) for JTextArea
instead of JLabels added to JPanel (in JScrollPane)
Normal text in a JLabel doesn't wrap. You can try using HTML:
String text = "<html>long text here</html";

Inserterting JLabel into JComboBox

http://prntscr.com/1gpdqe
Sorry for the link it's my first time in this forum.
Can anyone please help me?
I've been trying to make an image and a text show together in one line on a JComboBox, after searching on sites, I could not find a solution to my problem.
The JLabel above the JLabel works but the JLabel in the JComboBox doesn't.
Maybe this example will sort things out:
On getListCellRendererComponent I used Label + Icon
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ShowConboWithIcons extends JFrame {
private static final long serialVersionUID = 1L;
private static final ImageIcon INFO_ICON = new ImageIcon("info.png");
private static final ImageIcon NONE_ICON = new ImageIcon("none.png");
public final String NONE_STR = "None";
private final String INFO_STR = "Info";
private JComboBox comboBox;
private JPanel topPanel;
private String[] str_arr = null;
public ShowConboWithIcons(String[] str_arr) {
this.str_arr = str_arr;
}
public void createGUI(){
setMinimumSize(new Dimension(100,100));
setTitle("Demo");
setLocation(200, 200);
topPanel = new JPanel();
getContentPane().add(topPanel, BorderLayout.CENTER);
Map<Object, Icon> icons = new HashMap<Object, Icon>();
icons.put(NONE_STR, NONE_ICON);
icons.put(INFO_STR, INFO_ICON);
comboBox = new JComboBox();
comboBox.setRenderer(new IconListRenderer(icons));
comboBox.addItem("None");
for(String val : str_arr){
comboBox.addItem(val);
}
topPanel.add(comboBox);
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
String[] str_arr = {"A", "B", "C", "D", "E"};
ShowConboWithIcons T = new ShowConboWithIcons(str_arr);
T.createGUI();
T.setVisible(true);
}
class IconListRenderer extends DefaultListCellRenderer{
private static final long serialVersionUID = 1L;
private Map<Object, Icon> icons = null;
public IconListRenderer(Map<Object, Icon> icons){
this.icons = icons;
}
#Override
public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus)
{
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
// Get icon to use for the list item value
Icon icon = icons.get(value);
if(!value.toString().equals(NONE_STR)){
icon = icons.get(INFO_STR);
}
// Set icon to display for value
label.setIcon(icon);
return label;
}
}
}
Don't insert a JLabel into a JComboBox. Use a ListCellRenderer instead. Read Providing a Custom Renderer

How to add icon near arrow icon for JComboBox

I would like to create JComboBox control similar with the URL textbox of Firefox. Does anyone know how to customize the textfield of the JComboBox. I want to add some icons on the ALIGN.HORIZONTAL_RIGHT near the arrow button of the JComboBox
Thanks for your very detail explanation. Actually I will combine DefaultListCellRenderer and add the icon to the combo box like following code
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
public Main() {
// Create icon "C"
JButton jb = new JButton("C");
jb.setMargin(new Insets(0, 0, 0, 0));
jb.setBounds(245, 2, 18, 18);
// Create combo box
String[] languages = new String[]{"Java", "C#", "PHP"};
JComboBox jc = new JComboBox(languages);
// jc.setEditable(true);
jc.add(jb);
getContentPane().add(jc);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(300, 58));
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final Main main = new Main();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
main.setVisible(true);
}
});
}
}
But when I put jc.setEditable(true); the combo editor hided my icon. I'm thinking another way to simulate Firefox awesome bar. Do you have any idea for this?
To change how a component renders, you generally work with what are called Renderers.
For combobox, look at how to create a custom combobox renderer. Just a quick glance, but for your case, a simple configuration of DefaultListCellRenderer may be enough, since you can set the JLabel properties to position the text to the image. If not, just extend from it.
Remember also that you have to set a model that includes the icon so that the combobox renderer can get it - might want to do a custom ComboBoxModel too, depending on your data object.
Here is completed example that demonstrate it:
package com.demo.combo.icon;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ShowConboWithIcons extends JFrame {
private static final long serialVersionUID = 1L;
private static final ImageIcon INFO_ICON = new ImageIcon("info.png");
private static final ImageIcon NONE_ICON = new ImageIcon("none.png");
public final String NONE_STR = "None";
private final String INFO_STR = "Info";
private JComboBox comboBox;
private JPanel topPanel;
private String[] str_arr = null;
public ShowConboWithIcons(String[] str_arr) {
this.str_arr = str_arr;
}
public void createGUI(){
setMinimumSize(new Dimension(100,100));
setTitle("Demo");
setLocation(200, 200);
topPanel = new JPanel();
getContentPane().add(topPanel, BorderLayout.CENTER);
Map<Object, Icon> icons = new HashMap<Object, Icon>();
icons.put(NONE_STR, NONE_ICON);
icons.put(INFO_STR, INFO_ICON);
comboBox = new JComboBox();
comboBox.setRenderer(new IconListRenderer(icons));
comboBox.addItem("None");
for(String val : str_arr){
comboBox.addItem(val);
}
topPanel.add(comboBox);
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
String[] str_arr = {"A", "B", "C", "D", "E"};
ShowConboWithIcons T = new ShowConboWithIcons(str_arr);
T.createGUI();
T.setVisible(true);
}
class IconListRenderer extends DefaultListCellRenderer{
private static final long serialVersionUID = 1L;
private Map<Object, Icon> icons = null;
public IconListRenderer(Map<Object, Icon> icons){
this.icons = icons;
}
#Override
public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus)
{
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
// Get icon to use for the list item value
Icon icon = icons.get(value);
if(!value.toString().equals(NONE_STR)){
icon = icons.get(INFO_STR);
}
// Set icon to display for value
label.setIcon(icon);
return label;
}
}
}
Preview:

Categories