JAVA JList / JComboBox Demo

JListComboBox.java


  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class JListComboBox extends JFrame implements ItemListener {
  5.     protected JList fruits;
  6.     protected JComboBox colors;
  7.     protected String item[]={"apple","banana","orange","pear"};
  8.     public JListComboBox(){
  9.         super("JList/ComboBox Demo");
  10.         getContentPane().setLayout(new FlowLayout());
  11.        
  12.         fruits=new JList(item);
  13.         fruits.addMouseListener(new MouseAdapter(){
  14.             public void mouseClicked(MouseEvent e){
  15.                 //마우스가 클릭된 위치를 이용해 선택된 아이템의 인덱스를 얻는다.
  16.                 int index = fruits.locationToIndex(e.getPoint());
  17.                 if(e.getClickCount() ==2){
  18.                     System.out.println("["+item[index]+"]");   
  19.                 }else if(e.getClickCount() ==1){
  20.                     System.out.println(item[index]);
  21.                 }
  22.             }
  23.         });
  24.         //한번에 보여지는 내용물의 개수
  25.         fruits.setVisibleRowCount(3);
  26.         //List옆에 스크롤바를 붙인다.
  27.         JScrollPane sp = new JScrollPane(fruits);
  28.        
  29.         colors = new JComboBox();
  30.         colors.addItem("White");
  31.         colors.addItem("red");
  32.         colors.addItem("green");
  33.         colors.addItemListener((ItemListener) this);
  34.        
  35.         getContentPane().add(colors);
  36.         getContentPane().add(sp);
  37.        
  38.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.         setSize(300,200);
  40.         setVisible(true);
  41.        
  42.     }
  43.     public void itemStateChanged(ItemEvent e){
  44.         Object o = e.getSource();
  45.         if(o==colors){
  46.             if(e.getStateChange()==ItemEvent.SELECTED){
  47.                 Object data = colors.getSelectedItem();
  48.                 System.out.println(data.toString());
  49.             }
  50.         }
  51.     }
  52.     public static void main(String args[]){
  53.         JListComboBox jlc = new JListComboBox();
  54.     }
  55. }

    JComboBox 와  JList의 내용물을 모두 출력하는 실습.
    1. import java.awt.*;
    2. import java.awt.event.*;
    3. import java.util.Vector;
    4.  
    5. import javax.swing.*;
    6.  
    7. public class JListComboBox extends JFrame implements ItemListener {
    8.     protected JList fruits;
    9.     protected JComboBox colors;
    10.     protected String item[]={"apple","banana","orange","pear"};
    11.    
    12.     public JListComboBox(){
    13.         super("JList/ComboBox Demo");
    14.         getContentPane().setLayout(new FlowLayout());
    15.        
    16.         fruits=new JList(item);
    17.         fruits.addMouseListener(new MouseAdapter(){
    18.             public void mouseClicked(MouseEvent e){
    19.                 //마우스가 클릭된 위치를 이용해 선택된 아이템의 인덱스를 얻는다.
    20.                 int index = fruits.locationToIndex(e.getPoint());
    21.                 if(e.getClickCount() ==2){
    22.                     System.out.println("["+item[index]+"]");   
    23.                 }else if(e.getClickCount() ==1){
    24.                     System.out.println(item[index]);
    25.                 }
    26.             }
    27.         });
    28.         //한번에 보여지는 내용물의 개수
    29.         fruits.setVisibleRowCount(3);
    30.         //List옆에 스크롤바를 붙인다.
    31.         JScrollPane sp = new JScrollPane(fruits);
    32.        
    33.         colors = new JComboBox();
    34.         colors.addItem("White");
    35.         colors.addItem("red");
    36.         colors.addItem("green");
    37.         colors.addItemListener((ItemListener) this);
    38.        
    39.         getContentPane().add(colors);
    40.         getContentPane().add(sp);
    41.        
    42.  
    43.        
    44.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    45.         setSize(300,200);
    46.         setVisible(true);
    47.        
    48.     }
    49.  
    50.     public void itemStateChanged(ItemEvent e){
    51.         Object o = e.getSource();
    52.         if(o==colors){
    53.             if(e.getStateChange()==ItemEvent.SELECTED){
    54.                 Object data = colors.getSelectedItem();
    55.                 System.out.println(data.toString());
    56.             }
    57.         }
    58.     }
    59.     //JComboBox와 JList의 내용물을 모두 출력하는 메소드
    60.     public void printAllElement(){
    61.        
    62.         for(int i=0; i<colors.getItemCount();i++){
    63.             System.out.println(colors.getItemAt(i).toString());
    64.         }
    65.         //JList의 경우 ListModel을 이용해서 얻어야한다.
    66.         ListModel dm=(ListModel)fruits.getModel();
    67.         for(int i=0; i<dm.getSize();i++){
    68.             System.out.println(dm.getElementAt(i).toString());
    69.         }
    70.     }
    71.     public static void main(String args[]){
    72.         JListComboBox jlc = new JListComboBox();
    73.         jlc.printAllElement();
    74.     }
    75. }

댓글

이 블로그의 인기 게시물

[Win32 API] WINAPI - 함수호출규약

JAVA Frame Icon setting

JAVA Spinner