━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 19. Bidirectional Binding and using Slider

1. Main.java


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
package application;
    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs


2. Main.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
 
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController">
   <children>
      <Slider fx:id="slider" layoutX="27.0" layoutY="31.0" prefHeight="55.0" prefWidth="436.0" />
      <TextField fx:id="field" layoutX="27.0" layoutY="101.0" prefHeight="29.0" prefWidth="436.0" />
   </children>
</AnchorPane>
 
cs


3. MainController.java


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
package application;
 
import java.net.URL;
import java.text.NumberFormat;
import java.util.ResourceBundle;
 
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
 
public class MainController implements Initializable {
 
    @FXML
    private Slider slider;
    
    @FXML
    private TextField field;
    
    private static final double INIT_VALUE = 50;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        slider.setValue(INIT_VALUE);
        field.setText(new Double(INIT_VALUE).toString());
        field.textProperty().bindBidirectional(slider.valueProperty(), NumberFormat.getNumberInstance());
    }
 
}
 
cs


012


'Java > JavaFx' 카테고리의 다른 글

JavaFX 21. CheckBox  (0) 2016.08.25
JavaFX 20. Menu, MenuBar, MenuItem and SeparatorMenuItem  (0) 2016.08.25
JavaFX 18. Binding, ProgressBar and ProgressIndicator  (0) 2016.08.25
JavaFX 17. Properties  (0) 2016.08.25
JavaFX 16. FileChooser  (0) 2016.08.25
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 18. Binding, ProgressBar and ProgressIndicator

Binding 은 두 컨트롤의 properties 를 연결하는 것이다. 바인드 된 속성들은 하나의 속성값이 변화하면 자동으로 다른 속성값도 변화한다. 


단방향 바인드 - bind()

양방향 바인드 - bindBidirectional()

바인드 해제 - unbind()



1. Main.java


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
package application;
    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = new FXMLLoader().load(getClass().getResource("/application/Main.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs


2. Main.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.layout.AnchorPane?>
 
<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <Button layoutX="50.0" layoutY="76.0" mnemonicParsing="false" onAction="#BtnClick" text="- Button" />
      <Label fx:id="lblStatus" alignment="CENTER" layoutX="91.0" layoutY="14.0" prefHeight="62.0" prefWidth="121.0" text="Label" />
      <Button layoutX="169.0" layoutY="76.0" mnemonicParsing="false" onAction="#Btn2Click" text="+ Button" />
      <ProgressBar fx:id="pb" layoutX="52.0" layoutY="117.0" prefWidth="200.0" progress="0.5" />
      <ProgressIndicator fx:id="pi" layoutX="90.0" layoutY="165.0" prefHeight="104.0" prefWidth="121.0" progress="0.5" />
   </children>
</AnchorPane>
 
cs


3. MyNumber.java


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
package application;
 
import javafx.beans.property.*;
 
public class MyNumber {
    
    private DoubleProperty number;
 
    public double getNumber() {
        if(number != null){
            return number.get();
        }else{
            return 0;
        }
        
    }
 
    public void setNumber(double number) {
        this.numberProperty().set(number); 
    }
    
    public final DoubleProperty numberProperty(){
        if(number == null){
            number = new SimpleDoubleProperty(0);
        }
        return number;
    }
    
}
 
cs


4. MainController.java


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
50
51
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
 
public class MainController implements Initializable {
 
    final MyNumber myNum = new MyNumber();
    
    @FXML
    private Label lblStatus;
    
    @FXML
    private ProgressBar pb;
    
    @FXML
    private ProgressIndicator pi;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        myNum.setNumber(0);
        myNum.numberProperty().addListener(new ChangeListener<Object>() {
 
            @Override
            public void changed(ObservableValue<extends Object> observable, Object oldValue, Object newValue) {
                lblStatus.setText(new Double(myNum.getNumber()).toString());
                pb.progressProperty().bind(myNum.numberProperty());
                pi.progressProperty().bind(myNum.numberProperty());
            }
        });
        
    }
 
    public void BtnClick(ActionEvent event) {
        myNum.setNumber(myNum.getNumber() - 0.1);
    }
    
    public void Btn2Click(ActionEvent event) {
        myNum.setNumber(myNum.getNumber() + 0.1);
    }
}
 
cs




'Java > JavaFx' 카테고리의 다른 글

JavaFX 20. Menu, MenuBar, MenuItem and SeparatorMenuItem  (0) 2016.08.25
JavaFX 19. Bidirectional Binding and using Slider  (0) 2016.08.25
JavaFX 17. Properties  (0) 2016.08.25
JavaFX 16. FileChooser  (0) 2016.08.25
JavaFX 15. TreeView Events  (0) 2016.08.25
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 17. Properties

1. Main.java


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
package application;
    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = new FXMLLoader().load(getClass().getResource("/application/Main.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs


2. Main.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
 
 
<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController">
   <children>
      <Button layoutX="116.0" layoutY="209.0" mnemonicParsing="false" onAction="#BtnClick" text="Button" />
      <Label fx:id="lblStatus" layoutX="90.0" layoutY="57.0" prefHeight="62.0" prefWidth="121.0" text="Label" />
   </children>
</AnchorPane>
 
cs


3. MyNumber.java


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
package application;
 
import javafx.beans.property.*;
 
public class MyNumber {
    
    private DoubleProperty number;
 
    public double getNumber() {
        if(number != null){
            return number.get();
        }else{
            return 0;
        }
        
    }
 
    public void setNumber(double number) {
        this.numberProperty().set(number); 
    }
    
    public final DoubleProperty numberProperty(){
        if(number == null){
            number = new SimpleDoubleProperty(0);
        }
        return number;
    }
    
}
 
cs


4. MainController.java


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
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
 
public class MainController implements Initializable {
 
    final MyNumber myNum = new MyNumber();
    
    @FXML
    private Label lblStatus;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        myNum.setNumber(0);
        myNum.numberProperty().addListener(new ChangeListener<Object>() {
 
            @Override
            public void changed(ObservableValue<extends Object> observable, Object oldValue, Object newValue) {
                lblStatus.setText(new Double(myNum.getNumber()).toString());
                
            }
        });
        
    }
 
    public void BtnClick(ActionEvent event) {
        myNum.setNumber(myNum.getNumber() + 1);
    }
}
 
cs


properties 는 속성, 성질 등을 뜻한다. JavaFX에서 properties 는 '속성 감시' 라는 표현으로 사용된다. 내가 이해한 바로는 속성 값이 변화하면 즉각적으로 변화된 값을 사용할 수 있도록 해주는 기능이다. 구체적으로 속성값에 변화가 발생하면, changelistener 의 changed() 메소드를 호출하여 그 변화된 속성값을 사용할 수 있도록 해주는 것이다.




'Java > JavaFx' 카테고리의 다른 글

JavaFX 19. Bidirectional Binding and using Slider  (0) 2016.08.25
JavaFX 18. Binding, ProgressBar and ProgressIndicator  (0) 2016.08.25
JavaFX 16. FileChooser  (0) 2016.08.25
JavaFX 15. TreeView Events  (0) 2016.08.25
JavaFx 14. TreeView 사용하기  (0) 2016.08.17
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 16. FileChooser

1. Main.java


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
package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
cs


2. Sample.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
 
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.SampleController">
   <children>
      <Button fx:id="btn1" layoutX="56.0" layoutY="117.0" mnemonicParsing="false" onAction="#Button1Action" text="Select One File" />
      <Button fx:id="btn2" layoutX="53.0" layoutY="173.0" mnemonicParsing="false" onAction="#Button2Action" text="Select Multi File" />
      <ListView fx:id="listView" layoutX="218.0" layoutY="21.0" prefHeight="449.0" prefWidth="258.0" />
   </children>
</AnchorPane>
 
cs


3. SampleController.java


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
package application;
 
import java.io.File;
import java.util.List;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
 
public class SampleController {
    @FXML
    private Button btn1;
    @FXML
    private Button btn2;
    @FXML
    private ListView listView;
    
    public void Button1Action(ActionEvent event){
        FileChooser fc = new FileChooser();
        fc.setInitialDirectory(new File ("C:/Temp"));
        fc.getExtensionFilters().addAll(new ExtensionFilter("png files","*.png"));
        File selectedFile = fc.showOpenDialog(null);
        if(selectedFile != null){
            listView.getItems().add(selectedFile.getAbsolutePath());
        } else {
            System.out.println("File is not valid");
        }
    }
    
    public void Button2Action(ActionEvent event){
        FileChooser fc = new FileChooser();
        fc.setInitialDirectory(new File ("C:/Temp"));
        fc.getExtensionFilters().addAll(new ExtensionFilter("png files","*.png"));
        List<File> selectedFiles = fc.showOpenMultipleDialog(null);
        if(selectedFiles != null){
            for (int i = 0; i < selectedFiles.size(); i++) {
                listView.getItems().add(selectedFiles.get(i).getAbsolutePath());
            }
            
        } else {
            System.out.println("File is not valid");
        }
    }
}
 
cs



'Java > JavaFx' 카테고리의 다른 글

JavaFX 18. Binding, ProgressBar and ProgressIndicator  (0) 2016.08.25
JavaFX 17. Properties  (0) 2016.08.25
JavaFX 15. TreeView Events  (0) 2016.08.25
JavaFx 14. TreeView 사용하기  (0) 2016.08.17
JavaFx 13. ListView 사용하기  (0) 2016.08.17
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 15. TreeView Events

1. Main.java


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
package application;
    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
 
            Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
cs


2. Main.fxml


1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <TreeView fx:id="treeView" layoutX="14.0" layoutY="14.0" onContextMenuRequested="#mouseClick" onMouseClicked="#mouseClick" prefHeight="387.0" prefWidth="469.0" />
   </children>
</AnchorPane>
 
cs


3. MainController.java


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
50
51
52
53
54
55
56
57
58
59
60
61
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
 
public class MainController implements Initializable{
 
    @FXML
    TreeView <String> treeView;
    
    Image icon = new Image(getClass().getResourceAsStream("/img/folder.png"));
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ImageView[] iv = new ImageView[7];
        for (int i = 0; i < 7; i++) {
            iv[i] = new ImageView(icon);
            iv[i].setFitHeight(50);
            iv[i].setFitWidth(50);
        }
        
        
        TreeItem<String> root = new TreeItem<>("Root", iv[0]);
        root.setExpanded(true); // 처음부터 노드가 열려있도록 한다.
        
        
        TreeItem<String> nodeA = new TreeItem<>("node A", iv[1]);
        TreeItem<String> nodeB = new TreeItem<>("node B", iv[2]);
        TreeItem<String> nodeC = new TreeItem<>("node C", iv[3]);
        
        root.getChildren().addAll(nodeA, nodeB, nodeC);
        /*root.getChildren().add(nodeA);
        root.getChildren().add(nodeB);
        root.getChildren().add(nodeC);*/ 
        
        nodeA.setExpanded(true);
        
        TreeItem<String> nodeA1 = new TreeItem<>("node A1", iv[4]);
        TreeItem<String> nodeB1 = new TreeItem<>("node B1", iv[5]);
        TreeItem<String> nodeC1 = new TreeItem<>("node C1", iv[6]);
        
        nodeA.getChildren().addAll(nodeA1, nodeB1, nodeC1);
        
        treeView.setRoot(root);
    }
 
    public void mouseClick(MouseEvent mouseEvent) {
        if (mouseEvent.getClickCount() == 2){
        TreeItem<String> item = treeView.getSelectionModel().getSelectedItem();
        System.out.println(item.getValue());
        }
    }
}
 
cs


'Java > JavaFx' 카테고리의 다른 글

JavaFX 17. Properties  (0) 2016.08.25
JavaFX 16. FileChooser  (0) 2016.08.25
JavaFx 14. TreeView 사용하기  (0) 2016.08.17
JavaFx 13. ListView 사용하기  (0) 2016.08.17
JavaFx 12. ComboBox 사용하기  (0) 2016.08.17
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFx 14. TreeView 사용하기


트리뷰는 위와 같은 모양입니다.

루트 -> 노드 -> 노드 이런식으로 확장 가능한 리스트 입니다.

우리가 흔히 사용하는 탐색창이 트리구조의 대표적인 예입니다.


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
50
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
 
public class MainController implements Initializable{
 
    @FXML
    TreeView <String> treeView;
    
    Image icon = new Image(getClass().getResourceAsStream("/img/folder.png"));
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ImageView[] iv = new ImageView[7];
        for (int i = 0; i < 7; i++) {
            iv[i] = new ImageView(icon);
            iv[i].setFitHeight(50);
            iv[i].setFitWidth(50);
        }
        
        
        TreeItem<String> root = new TreeItem<>("Root", iv[0]);
        
        TreeItem<String> nodeA = new TreeItem<>("node A", iv[1]);
        TreeItem<String> nodeB = new TreeItem<>("node B", iv[2]);
        TreeItem<String> nodeC = new TreeItem<>("node C", iv[3]);
        
        root.getChildren().addAll(nodeA, nodeB, nodeC);
        /*root.getChildren().add(nodeA);
        root.getChildren().add(nodeB);
        root.getChildren().add(nodeC);*/ 
        
        TreeItem<String> nodeA1 = new TreeItem<>("node A1", iv[4]);
        TreeItem<String> nodeB1 = new TreeItem<>("node B1", iv[5]);
        TreeItem<String> nodeC1 = new TreeItem<>("node C1", iv[6]);
        
        nodeA.getChildren().addAll(nodeA1, nodeB1, nodeC1);
        
        treeView.setRoot(root);
    }
 
}
 
cs


'Java > JavaFx' 카테고리의 다른 글

JavaFX 16. FileChooser  (0) 2016.08.25
JavaFX 15. TreeView Events  (0) 2016.08.25
JavaFx 13. ListView 사용하기  (0) 2016.08.17
JavaFx 12. ComboBox 사용하기  (0) 2016.08.17
JavaFx 11. ImageView 사용하기  (0) 2016.08.17
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFx 13. ListView 사용하기

이번에는 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)들을 가져올 수도 있습니다.



'Java > JavaFx' 카테고리의 다른 글

JavaFX 15. TreeView Events  (0) 2016.08.25
JavaFx 14. TreeView 사용하기  (0) 2016.08.17
JavaFx 12. ComboBox 사용하기  (0) 2016.08.17
JavaFx 11. ImageView 사용하기  (0) 2016.08.17
JavaFx 10. 로그인 기능 만들기  (0) 2016.08.17
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFx 12. ComboBox 사용하기

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
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;
 
public class MainController implements Initializable{
 
    @FXML
    public Label  myLabel;
    
    @FXML
    public ComboBox<String> combobox;
    
    ObservableList<String> list = FXCollections.observableArrayList("Mark""Tom""John""Jack");
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        combobox.setItems(list);    
    }
    
    public void comboChange(ActionEvent event) {
        myLabel.setText(combobox.getValue());
    }
    public void buttonAction(ActionEvent event) {
        combobox.getItems().addAll("Ram""Ben""Steve""Ma");
        
    }
}
 
cs


콤보 박스를 사용하는 방법은 

1. Initializable 을 구현한다.

2. ObservalbeList 를 구성한다.

3. 아이템을 콤보박스에 붙인다.


먼저 Initializable 를 구현 해야 합니다. 왜냐하면 스테이지가 초기화 될때 콤보박스에 아이템이 설정되어야 하기 때문입니다.

22번 라인을 보면, String 타입의 리스트를 만들고 있습니다.

그리고 26번 라인에서 그 리스트를 콤보박스에 붙이는 것이죠.


콤보 박스를 사용하는 또 다른 방법은 fxml 에서 바로 코딩을 하는 방법입니다.

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
<?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="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <ComboBox fx:id="combobox" layoutX="74.0" layoutY="106.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" layoutX="74.0" layoutY="29.0" prefHeight="64.0" prefWidth="153.0" text="Label" />
      <Button layoutX="74.0" layoutY="196.0" mnemonicParsing="false" onAction="#buttonAction" prefHeight="52.0" prefWidth="153.0" text="Button" />
   </children>
</AnchorPane>
 
cs


첫번째 방법으로 아이템을 구성하면 위 소스에서 12~ 19 라인이 없습니다.

12~19 라인은 수동으로 아이템을 입력하는 방법입니다.

이 방법을 사용하실때 주의하실 점은 7번 라인을 보시면 collections 를 import  한것을 알 수 있습니다. 

이게 자동완성으로 되지 않았습니다. 그래서 수동으로 임포트 시켰습니다.



콤보박스는 이런식으로 보여집니다.


'Java > JavaFx' 카테고리의 다른 글

JavaFx 14. TreeView 사용하기  (0) 2016.08.17
JavaFx 13. ListView 사용하기  (0) 2016.08.17
JavaFx 11. ImageView 사용하기  (0) 2016.08.17
JavaFx 10. 로그인 기능 만들기  (0) 2016.08.17
JavaFx 08. 계산기 만들기  (2) 2016.08.17
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFx 11. ImageView 사용하기




직전 포스트인 로그인 프로젝트를 그대로 사용하여 만들었습니다. 그래서 대부분의 코딩은 똑같습니다. 단지 차이점은 Login.fxml 파일에 ImageView 가 추가된 부분 입니다.


ImageView를 사용하는 방법은,

scene builder 에서 ImageView 를 드래그&드롭으로 적당한 위치에 배치하고, 

properties -> specific -> image -> ... 을 눌려서 이미지를 선택하면 됩니다. 


아니면 저처럼 직접 입력하셔도 됩니다.

이미지를 소스폴더 안에 copy 해 놓고, 경로를 패키지명/파일명 으로 입력하면 됩니다.


ImageView 를 사용하는 또 다른 방법은 소스코드를 직접 코딩하는 것입니다.


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
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.image.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.image.Image?>
 
<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>
      <Button layoutX="23.0" layoutY="198.0" mnemonicParsing="false" onAction="#Login" text="Login">
         <font>
            <Font size="18.0" />
         </font>
         <graphic>
             <ImageView fitHeight="50.0" fitWidth="75.0"
                  <image>
                      <Image url="http://easyonlineconverter.com/img/logo1.png" />
                  </image>
                </ImageView>
            </graphic>
      </Button>
      <TextField fx:id="txtUserName" layoutX="23.0" layoutY="107.0" promptText="UserName">
         <font>
            <Font size="18.0" />
         </font>
      </TextField>
      <PasswordField fx:id="txtPassword" layoutX="23.0" layoutY="150.0" promptText="Password">
         <font>
            <Font size="18.0" />
         </font>
      </PasswordField>
      <Label fx:id="lblStatus" alignment="CENTER" layoutY="35.0" prefHeight="21.0" prefWidth="229.0" text="Status" textFill="#d31c1c">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <ImageView fitHeight="238.0" fitWidth="229.0" layoutX="260.0" layoutY="32.0" pickOnBounds="true" preserveRatio="true">
          <image>
              <Image url="img/melon.jpg" />
          </image>
      </ImageView>
   </children>
</AnchorPane>
 
cs


17 ~ 23번 라인, 40 ~ 44번 라인이 ImageView를 삽입하는 코드 입니다. 20번 라인을 보시면, 이미지를 인터넷에서 바로 가져와서 사용하는 방법도 있습니다.

'Java > JavaFx' 카테고리의 다른 글

JavaFx 13. ListView 사용하기  (0) 2016.08.17
JavaFx 12. ComboBox 사용하기  (0) 2016.08.17
JavaFx 10. 로그인 기능 만들기  (0) 2016.08.17
JavaFx 08. 계산기 만들기  (2) 2016.08.17
JavaFx 07. CSS 스타일 하기  (0) 2016.08.12
COMMENT
1 2 3 4 5 6 7 8 9