Java/JavaFx

JavaFx 13. ListView 사용하기

AristataIT 2016. 8. 17. 16:16

이번에는 ListView 사용하는 법을 보여드리겠습니다.


사실 이쯤 하면 JavaFX 를 사용하는 방법은 대게 유사합니다. 


Scene Bulider 로 Controls 를 배치하고 이벤트 설정하고...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
 
public class MainController implements Initializable{
 
    @FXML
    public Label  myLabel;
    
    @FXML
    public ComboBox<String> combobox;
    
    @FXML
    public ListView<String> listView;
    
    ObservableList<String> list = FXCollections.observableArrayList("Mark""Tom""John""Jack");
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //combobox.setItems(list);
        //listView.setItems(list);
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    }
    
    public void comboChange(ActionEvent event) {
        myLabel.setText(combobox.getValue());
    }
    public void buttonAction(ActionEvent event) {
        //combobox.getItems().addAll("Ram", "Ben", "Steve", "Ma");
        //listView.getItems().addAll("Ram", "Ben", "Steve", "Ma");
        ObservableList<String> names;
        names = listView.getSelectionModel().getSelectedItems();
        for (String name : names) {
            System.out.println(name);
        }
    }
}
 
cs


리스트뷰는 콤보박스와 사용방법이 매우 유사합니다. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.collections.*?>
 
<AnchorPane prefHeight="300.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <ComboBox fx:id="combobox" layoutX="14.0" layoutY="78.0" onAction="#comboChange" prefHeight="52.0" prefWidth="153.0" promptText="Select Name">
          <!-- <items>
            <FXCollections fx:factory="observableArrayList"  >
                <String fx:value="Item 1" />
                <String fx:value="Item 2" />
                <String fx:value="Item 3" />
                <String fx:value="Item 4" />
            </FXCollections>        
          </items> -->
         </ComboBox>
      
      <Label fx:id="myLabel" alignment="CENTER" layoutX="14.0" layoutY="14.0" prefHeight="64.0" prefWidth="153.0" text="Label" />
      <Button layoutX="14.0" layoutY="137.0" mnemonicParsing="false" onAction="#buttonAction" prefHeight="52.0" prefWidth="153.0" text="이름 추가" />
      <ListView fx:id="listView" layoutX="181.0" layoutY="14.0" prefHeight="274.0" prefWidth="307.0" >
          <items>
            <FXCollections fx:factory="observableArrayList"  >
                <String fx:value="Item 1" />
                <String fx:value="Item 2" />
                <String fx:value="Item 3" />
                <String fx:value="Item 4" />
            </FXCollections>        
          </items>
      </ListView>
   </children>
</AnchorPane>
 
cs



기본적으로 하나만 선택되는데, 컨트롤러에서 33번 라인과 같이 mutiple 선택모드를 설정하면 여러개를 선택할 수도 있습니다.
listView.getSelectionModel().getSelectedItems() 메소드를 사용하면 서택한 아이템의 값(value)들을 가져올 수도 있습니다.