Hi Thank you very much for your tutorial and it will help me a lot. I am getting image and not able to display text. Please advise changes. package com.mcconsulting; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultComboBoxModel; 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.JOptionPane; import javax.swing.ListCellRenderer; public class ComboImageText extends JFrame { public ComboImageText() { setTitle("Jagadeesh Test"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(new FlowLayout()); // Our Combobox JComboBox comboBox = new JComboBox(); comboBox.setPreferredSize(new Dimension(200, 580)); // Model comboBox.setModel(populate()); comboBox.setRenderer(new ImagesAndTextRenderer()); comboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name=((ImagesNText)comboBox.getSelectedItem()).getName(); JOptionPane.showConfirmDialog(null, name);
} }); getContentPane().add(comboBox); } private DefaultComboBoxModel populate() { DefaultComboBoxModel boxModel = new DefaultComboBoxModel(); boxModel.addElement(new ImagesNText(new ImageIcon("C:\\imagescombo\\1.jpg"), "JAGADEESH")); boxModel.addElement(new ImagesNText(new ImageIcon("C:\\imagescombo\\2.jpg"), "SECOND")); boxModel.addElement(new ImagesNText(new ImageIcon("C:\\imagescombo\\3.jpg"), "THIRD")); boxModel.addElement(new ImagesNText(new ImageIcon("C:\\imagescombo\\4.jpg"), "Forth")); return boxModel; } public static void main(String[] args) { new ComboImageText().setVisible(true);; } } // Render our images and text to combobox class ImagesAndTextRenderer extends JLabel implements ListCellRenderer { @Override public Component getListCellRendererComponent(JList list, Object val, int index, boolean isSelected, boolean cellHasFocus) { // TODO Auto-generated method stub ImagesNText imagesNText = (ImagesNText) val; setIcon(imagesNText.getImg()); setText(imagesNText.getName()); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setFont(list.getFont()); return this; } } // Bean class class ImagesNText { private Icon img; private String name; public ImagesNText(Icon aicon, String aname) { this.img = aicon; this.name = aname;// TODO Auto-generated constructor stub } public Icon getImg() { return img; } public void setImg(Icon img) { this.img = img; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
OMG thanks so much I've searching how to do this for forever!
You are welcome.
Thank u, greetings from 2021 (:
Thank you! you're a superstar!
very thank for your help to me
Thanks for commenting.
thank you a lot
You are welcome.
THANK YOU
You are welcome.
I was wondering, could you could help me troubleshoot my code? I followed the tutorial closely but keep getting errors.
Which errors are you getting?
Hi Thank you very much for your tutorial and it will help me a lot.
I am getting image and not able to display text. Please advise changes.
package com.mcconsulting;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
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.JOptionPane;
import javax.swing.ListCellRenderer;
public class ComboImageText extends JFrame {
public ComboImageText() {
setTitle("Jagadeesh Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
// Our Combobox
JComboBox comboBox = new JComboBox();
comboBox.setPreferredSize(new Dimension(200, 580));
// Model
comboBox.setModel(populate());
comboBox.setRenderer(new ImagesAndTextRenderer());
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name=((ImagesNText)comboBox.getSelectedItem()).getName();
JOptionPane.showConfirmDialog(null, name);
}
});
getContentPane().add(comboBox);
}
private DefaultComboBoxModel populate() {
DefaultComboBoxModel boxModel = new DefaultComboBoxModel();
boxModel.addElement(new ImagesNText(new ImageIcon("C:\\imagescombo\\1.jpg"), "JAGADEESH"));
boxModel.addElement(new ImagesNText(new ImageIcon("C:\\imagescombo\\2.jpg"), "SECOND"));
boxModel.addElement(new ImagesNText(new ImageIcon("C:\\imagescombo\\3.jpg"), "THIRD"));
boxModel.addElement(new ImagesNText(new ImageIcon("C:\\imagescombo\\4.jpg"), "Forth"));
return boxModel;
}
public static void main(String[] args) {
new ComboImageText().setVisible(true);;
}
}
// Render our images and text to combobox
class ImagesAndTextRenderer extends JLabel implements ListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object val, int index, boolean isSelected,
boolean cellHasFocus) {
// TODO Auto-generated method stub
ImagesNText imagesNText = (ImagesNText) val;
setIcon(imagesNText.getImg());
setText(imagesNText.getName());
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setFont(list.getFont());
return this;
}
}
// Bean class
class ImagesNText {
private Icon img;
private String name;
public ImagesNText(Icon aicon, String aname) {
this.img = aicon;
this.name = aname;// TODO Auto-generated constructor stub
}
public Icon getImg() {
return img;
}
public void setImg(Icon img) {
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}