
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/*
 * 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.
 */

/**
 *
 * @author ta
 */
public class DemoJcomJlist extends JFrame{
    
     JList<String> jlist ;
    Color [] color = {Color.BLUE,Color.BLACK,Color.DARK_GRAY,Color.PINK,Color.darkGray} ;
    String []  colorname =  { "BLUE" , "BLACK" , "DARK_GRAY" , "PINK" , "DARK_GRAY" };

    public DemoJcomJlist(String title) {
        super(title);
        this.setSize(200,250);
       this.getContentPane().setLayout(new FlowLayout());

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
       creatAndShow();
        
    }

    public void creatAndShow() {
        
        
        Container con = this.getContentPane();
        JPanel jpn = new JPanel(new FlowLayout());
        jpn.setBackground(Color.red);
        
        
        Font font = new Font("Font.BOLD",20,20);
        jlist = createJlist();
        // KHong hien la cai dimension
//        jlist.setPreferredSize(new Dimension(100,150));
        
        jpn.add(jlist);
        con.add(jpn);
        
        
        
        
        
        
    }
    
    
    
    public JList createJlist()
    {
       
        jlist = new JList<String>(colorname);
        jlist.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                changebackground();
            }
        });
        
        return jlist;
        
    }
    
    
    
    public void changebackground()
    {
        getContentPane().setBackground(color[jlist.getSelectedIndex()]);
        
    }
    
    public static void main(String[] args) {
        new DemoJcomJlist("Color");
        
    }
    
    
}

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/*
 * 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.
 */

/**
 *
 * @author ta
 */
public class DemoJcomJlist extends JFrame{
    
     JList<String> jlist ;
    Color [] color = {Color.BLUE,Color.BLACK,Color.DARK_GRAY,Color.PINK,Color.darkGray} ;
    String []  colorname =  { "BLUE" , "BLACK" , "DARK_GRAY" , "PINK" , "DARK_GRAY" };

    public DemoJcomJlist(String title) {
        super(title);
        this.setSize(200,250);
       this.getContentPane().setLayout(new FlowLayout());

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
       creatAndShow();
        
    }

    public void creatAndShow() {
        
        
        Container con = this.getContentPane();
        JPanel jpn = new JPanel(new FlowLayout());
        jpn.setBackground(Color.red);
        
        
        Font font = new Font("Font.BOLD",20,20);
        jlist = createJlist();
        // KHong hien la cai dimension
//        jlist.setPreferredSize(new Dimension(100,150));
        
        jpn.add(jlist);
        con.add(jpn);
        
        
        
        
        
        
    }
    
    
    
    public JList createJlist()
    {
       
        jlist = new JList<String>(colorname);
        jlist.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                changebackground();
            }
        });
        
        return jlist;
        
    }
    
    
    
    public void changebackground()
    {
        getContentPane().setBackground(color[jlist.getSelectedIndex()]);
        
    }
    
    public static void main(String[] args) {
        new DemoJcomJlist("Color");
        
    }
    
    
}
