Java Custom JComboBox - Add Both IMAGES and TEXT dynamically

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ธ.ค. 2024

ความคิดเห็น • 13

  • @rickcanty
    @rickcanty 6 ปีที่แล้ว +2

    OMG thanks so much I've searching how to do this for forever!

  • @localnep
    @localnep 3 ปีที่แล้ว

    Thank u, greetings from 2021 (:

  • @jasonlodder8591
    @jasonlodder8591 4 ปีที่แล้ว

    Thank you! you're a superstar!

  • @sohrabbehnam6767
    @sohrabbehnam6767 6 ปีที่แล้ว

    very thank for your help to me

  • @hmdmok
    @hmdmok 5 ปีที่แล้ว +1

    thank you a lot

  • @JoyJulianGomes
    @JoyJulianGomes 8 ปีที่แล้ว +1

    THANK YOU

  • @mcagar-gamer7470
    @mcagar-gamer7470 4 ปีที่แล้ว

    I was wondering, could you could help me troubleshoot my code? I followed the tutorial closely but keep getting errors.

  • @jagadeeshgk
    @jagadeeshgk 7 ปีที่แล้ว

    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;
    }
    }