JList는 Java Swing 패키지의 일부입니다. JList는 일련의 객체를 표시하고 사용자가 하나 이상의 항목을 선택할 수 있도록 하는 구성 요소입니다. JList는 JComponent 클래스를 상속합니다. JList는 벡터 배열을 표시하는 쉬운 방법입니다.
JList의 생성자는 다음과 같습니다.
- JList() : 빈 목록을 생성합니다. JList(E [ ] l) : 배열의 요소로 새 목록을 생성합니다. JList(ListModel d) : 지정된 목록 모델을 사용하여 새 목록을 생성합니다. JList(Vector l): 벡터 요소로 새 목록을 생성합니다.
일반적으로 사용되는 방법은 다음과 같습니다.
| 방법 | 설명 |
|---|---|
| getSelectedIndex() | 목록에서 선택한 항목의 인덱스를 반환합니다. |
| getSelectedValue() | 목록 요소의 선택된 값을 반환합니다. |
| setSelectedIndex(int i) | 목록의 선택된 인덱스를 i로 설정합니다. |
| setSelectionBackground(색상 c) | 목록의 배경색을 설정합니다. |
| setSelectionForeground(색상 c) | 목록의 전경색을 변경합니다. |
| setListData(E [ ] l) | 목록의 요소를 l의 요소로 변경합니다. |
| setVisibleRowCount(int v) | visibleRowCount 속성을 변경합니다. |
| setSelectedValue(객체 a, 부울 s) | 목록에서 지정된 개체를 선택합니다. |
| setSelectedIndices(int[] i) | 주어진 배열에 의해 지정된 인덱스 세트로 선택 항목을 변경합니다. |
| setListData(벡터 l) | 지정된 Vector에서 읽기 전용 ListModel을 생성합니다. |
| setLayoutOrientation(int l) | 목록의 방향을 정의합니다 |
| setFixedCellWidth(int w) | 목록의 셀 너비를 매개변수로 전달된 값으로 변경합니다. |
| setFixedCellHeight(int h) | 목록의 셀 높이를 매개변수로 전달된 값으로 변경합니다. |
| isSelectedIndex(int i) | 지정된 인덱스가 선택되면 true를 반환하고, 그렇지 않으면 false를 반환합니다. |
| indexToLocation(int i) | 목록의 좌표계에서 지정된 항목의 원점을 반환합니다. |
| getToolTipText(MouseEvent e) | 해당 이벤트에 사용할 도구 설명 텍스트를 반환합니다. |
| getSelectedValuesList() | 선택한 모든 항목의 목록을 반환합니다. |
| getSelectedIndices() | 선택한 모든 인덱스의 배열을 오름차순으로 반환합니다. |
| getMinSelectionIndex() | 선택된 가장 작은 셀 인덱스를 반환하거나, 선택 항목이 비어 있으면 -1을 반환합니다. |
| getMaxSelectionIndex() | 선택된 가장 큰 셀 인덱스를 반환하거나, 선택 항목이 비어 있으면 -1을 반환합니다. |
| getListSelectionListeners() | 목록의 리스너를 반환합니다. |
| getLastVisibleIndex() | 현재 표시되는 가장 큰 목록 인덱스를 반환합니다. |
| getDragEnabled() | 자동 드래그 처리가 활성화되어 있는지 여부를 반환합니다. |
| addListSelectionListener(ListSelectionListener l) | 목록에 listSelectionlistener를 추가합니다. |
다음 프로그램은 JList의 사용을 보여줍니다.
1. 간단한 JList를 생성하는 프로그램
자바
이진 트리의 후위 순회
ml에서 온스로
// java Program to create a simple JList> import> java.awt.event.*;> import> java.awt.*;> import> javax.swing.*;> class> solve>extends> JFrame> {> > >//frame> >static> JFrame f;> > >//lists> >static> JList b;> > >//main class> >public> static> void> main(String[] args)> >{> >//create a new frame> >f =>new> JFrame(>'frame'>);> > >//create a object> >solve s=>new> solve();> > >//create a panel> >JPanel p =>new> JPanel();> > >//create a new label> >JLabel l=>new> JLabel(>'select the day of the week'>);> >//String array to store weekdays> >String week[]= {>'Monday'>,>'Tuesday'>,>'Wednesday'>,> >'Thursday'>,>'Friday'>,>'Saturday'>,>'Sunday'>};> > >//create list> >b=>new> JList(week);> > >//set a selected index> >b.setSelectedIndex(>2>);> > >//add list to panel> >p.add(b);> > >f.add(p);> > >//set the size of frame> >f.setSize(>400>,>400>);> > >f.show();> >}> > > }> |
>
>
출력 :

자바 문자열 값
2. 목록을 생성하고 itemListener를 추가하는 프로그램(목록을 사용하여 생일을 선택하는 프로그램).
자바
화창한 나이
// java Program to create a list and add itemListener to it> // (program to select your birthday using lists) .> import> javax.swing.event.*;> import> java.awt.*;> import> javax.swing.*;> class> solve>extends> JFrame>implements> ListSelectionListener> {> > >//frame> >static> JFrame f;> > >//lists> >static> JList b,b1,b2;> > >//label> >static> JLabel l1;> > >//main class> >public> static> void> main(String[] args)> >{> >//create a new frame> >f =>new> JFrame(>'frame'>);> > >//create a object> >solve s=>new> solve();> > >//create a panel> >JPanel p =>new> JPanel();> > >//create a new label> >JLabel l=>new> JLabel(>'select your birthday'>);> >l1=>new> JLabel();> >//String array to store weekdays> >String month[]= {>'January'>,>'February'>,>'March'>,> >'April'>,>'May'>,>'June'>,>'July'>,>'August'>,> >'September'>,>'October'>,>'November'>,>'December'>};> > >//create a array for months and year> >String date[]=>new> String[>31>],year[]=>new> String[>31>];> > >//add month number and year to list> >for>(>int> i=>0>;i<>31>;i++)> >{> >date[i]=>''>+(>int>)(i+>1>);> >year[i]=>''>+(>int>)(>2018>-i);> >}> > >//create lists> >b=>new> JList(date);> >b1=>new> JList(month);> >b2=>new> JList(year);> > >//set a selected index> >b.setSelectedIndex(>2>);> >b1.setSelectedIndex(>1>);> >b2.setSelectedIndex(>2>);> > >l1.setText(b.getSelectedValue()+>' '>+b1.getSelectedValue()> >+>' '>+b2.getSelectedValue());> > >//add item listener> >b.addListSelectionListener(s);> >b1.addListSelectionListener(s);> >b2.addListSelectionListener(s);> > >//add list to panel> >p.add(l);> >p.add(b);> >p.add(b1);> >p.add(b2);> >p.add(l1);> > >f.add(p);> > >//set the size of frame> >f.setSize(>500>,>600>);> > >f.show();> >}> >public> void> valueChanged(ListSelectionEvent e)> >{> >//set the text of the label to the selected value of lists> >l1.setText(b.getSelectedValue()+>' '>+b1.getSelectedValue()> >+>' '>+b2.getSelectedValue());> > >}> > > }> |
>
자바 문자열 조인
>
출력 :

참고: 위 프로그램은 온라인 컴파일러에서 실행되지 않을 수 있습니다. 오프라인 IDE를 사용하십시오.