+ Creating a JList with icons and text Joris Van den Bogaert Main.java: import java.awt.image.*; import javax.swing.*; import java.util.*; import java.awt.*; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("JList ImageIcon Demonstration"); DefaultListModel dlm = new DefaultListModel(); dlm.addElement(new ListEntry("Audio", new ImageIcon("audio.gif"))); dlm.addElement(new ListEntry("Control Panel", new ImageIcon("controlpanel.gif"))); dlm.addElement(new ListEntry("Folder", new ImageIcon("folder.gif"))); dlm.addElement(new ListEntry("Local Disk (C:)", new ImageIcon("mycomp.gif"))); dlm.addElement(new ListEntry("doc on '192.168.0.1' (Z:)", new ImageIcon("network.gif"))); JList list = new JList(dlm); list.setCellRenderer(new ListEntryCellRenderer()); frame.getContentPane().add(BorderLayout.CENTER, new JScrollPane(list)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } } class ListEntry { private String value; private ImageIcon icon; public ListEntry(String value, ImageIcon icon) { this.value = value; this.icon = icon; } public String getValue() { return value; } public ImageIcon getIcon() { return icon; } public String toString() { return value; } } class ListEntryCellRenderer extends JLabel implements ListCellRenderer { private JLabel label; public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { ListEntry entry = (ListEntry) value; setText(value.toString()); setIcon(entry.getIcon()); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setEnabled(list.isEnabled()); setFont(list.getFont()); setOpaque(true); return this; } }
Creating a JList with icons and text
Joris Van den BogaertMain.java:
You might also like
Force the JScrollPane to scroll to the...
There are three steps to this: 1. Listen for focus events coming from the components that you want to...
Preventing a JInternalFrame from being...
You can subclass the protected class BorderListener that is defined in BasicInternalFrameUI and provide...
Implementing a mouseover effect with...
Add a MouseListener to your JButton and code the desired behaviour in mouseEntered and mouseExited. This...