The objective is to have images appear within the ComobBox.
When using ImageIcon and the basic calls all work except the icons are very large. Not sure if the combobox uses the icon to set the size and if that can be altered.
When using a custom class that extends ImageIcon, no icons appear which is the main objective to address.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JComboBoxWithIcons extends JFrame {
private static final long serialVersionUID = 1L;
private final ImageIcon ICON1a = new ImageIcon("c:\\Downloads\\Images\\Image1.jpg");
private final ImageIcon ICON2a = new ImageIcon("c:\\Downloads\\Images\\Image2.jpg");
private final ImageIcon ICON3a = new ImageIcon("c:\\Downloads\\Images\\Image3.jpg");
private final MyImageIconObject ICON1b = new MyImageIconObject("c:\\Downloads\\Images\\Image1.jpg");
private final MyImageIconObject ICON2b = new MyImageIconObject("c:\\Downloads\\Images\\Image2.jpg");
private final MyImageIconObject ICON3b = new MyImageIconObject("c:\\Downloads\\Images\\Image3.jpg");
private JComboBox comboBox1;
private JComboBox comboBox2;
private JPanel topPanel;
public JComboBoxWithIcons () {}
public void createGUI(){
setMinimumSize(new Dimension(400,400));
setTitle("Demo");
setLocation(200, 200);
topPanel = new JPanel();
getContentPane().add(topPanel, BorderLayout.CENTER);
ArrayList<ImageIcon> iconsA = new ArrayList<ImageIcon>();
iconsA.add(ICON1a);
iconsA.add(ICON2a);
iconsA.add(ICON3a);
ArrayList<MyImageIconObject> iconsB = new ArrayList<MyImageIconObject>();
iconsB.add(ICON1b);
iconsB.add(ICON2b);
iconsB.add(ICON3b);
comboBox1 = new JComboBox();
comboBox2 = new JComboBox();
for(ImageIcon val : iconsA){
comboBox1.addItem(val);
}
for(MyImageIconObject val : iconsB){
comboBox2.addItem(val);
}
topPanel.add(comboBox1);
topPanel.add(comboBox2);
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
public static void main(String[] args)
{
JComboBoxWithIcons T = new JComboBoxWithIcons();
T.createGUI();
T.setVisible(true);
}
class MyImageIconObject extends ImageIcon
{
float x;
ImageIcon ic;
public MyImageIconObject(String iconLocation)
{
this.ic = new ImageIcon(iconLocation);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
super.paintIcon(c, g, x, y);
// Graphics2D g2d = (Graphics2D) g;
// ic.paintIcon(c, g2d, x, y);
System.out.println("Painting 2");
}
}
}
When the above code is executed, it produces the following :
The example creates 2 ComboBoxes with the same icons.
ComboBox1 - displays the icons, but they are large.
ComboBox2 - uses the custom class and overrides the paintIcon() as I plan to do additional modifications to the icons but first need them to appear.
Question 1 (more of a curiosity question) - to resize the icons will a renderer be required?
Question 2 (main objective) - for ComboBox 2, why are the icons not appearing?