Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class Main {
    JComboBox combo = new JComboBox();

    public Main() {
        combo.setRenderer(new ComboToolTipRenderer(combo));
        combo.addItemListener(e -> System.out.println(combo.getSelectedItem().toString()));
        combo.addItem("");
        combo.addItem("Long text 4");
        combo.addItem("Long text 3");
        combo.addItem("Long text 2");
        combo.addItem("Long text 1");
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(combo);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
    }
}

class ComboToolTipRenderer extends BasicComboBoxRenderer {
    static final long serialVersionUID = 1L;
    JComboBox combo;
    JList comboList;

    ComboToolTipRenderer(JComboBox combo) {
        this.combo = combo;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (comboList == null) {
            comboList = list;
            KeyAdapter listener = new KeyAdapter() {

                @Override
                public void keyReleased(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_UP) {
                        int x = 5;
                        int y = comboList.indexToLocation(comboList.getSelectedIndex()).y;
                        System.out.println(comboList.getSelectedIndex());
                    }
                }
            };
            combo.addKeyListener(listener);
            combo.getEditor().getEditorComponent().addKeyListener(listener);
        }
        if (isSelected) {
            System.out.println(value.toString());
        }
        return this;
    }
}