/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package swingdemo;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author hi
 */
public class JComboboxJListUI extends JFrame {
    JComboBox cbo;
    JList  jlist;
    JButton jbt;
    
    public JComboboxJListUI() throws HeadlessException {
        this.setSize(400,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        creatAndShow();
        addEvent();
    }

    private void creatAndShow() {
        JPanel jpn = new JPanel();
        jpn.setLayout(new BoxLayout(jpn,BoxLayout.Y_AXIS));
        
        String arr[] = {"Xuất sắc","Giỏi","Khá","Trung bình"};
         cbo = new JComboBox(arr);
         jpn.add(cbo);
         
         
         JPanel jpnlist = new JPanel();
         jpnlist.setLayout(new FlowLayout());
         JLabel jlb = new JLabel("Chọn số : ");
         jlb.setFont(new Font("Font.BOLD",20,20));
         JList list = jlist();
         JScrollPane sc = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
         
         jpnlist.add(jlb);
         jpnlist.add(sc);
         
         JPanel jpnbt = new JPanel();
         jbt = new JButton("OK");
         jbt.setFont(new Font("Font.BOLD",20,20));
         jpnbt.add(jbt);
         
         
        jpn.add(jpnlist);
        jpn.add(jpnbt);
        this.getContentPane().add(jpn);
        
        
    }
    
    public JList jlist()
    {
        Random rd = new Random();
        Vector<Integer> vec = new Vector<>();
        for(int i=0;i<100;i++)
        {
            
                    int x = rd.nextInt(300);
                    vec.add(x);
                  }
        
      jlist = new JList(vec);
        return jlist;
    }
    
    public void addEvent()
    {
        cbo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int vt = cbo.getSelectedIndex();
                if(vt!=-1)
                {
                    JOptionPane.showMessageDialog(rootPane,"Vị trí được chọn : "+(vt+1)+" "+"Phần tử được chọn là :"+cbo.getSelectedItem());
                    
                }
                
                else 
                {
                    JOptionPane.showMessageDialog(rootPane,"Không có phần tử nào được chọn");
                }
                
            }
        });
        
        jbt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object obj = jlist.getSelectedValue();
                int vt = jlist.getSelectedIndex();
                JOptionPane.showMessageDialog(rootPane, "Phần tử được chọn là " + obj + " "+ "Vị trí được chọn là : "+vt );
                
            }
        });
    }

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