전체 글 (74)

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

JavaFX 27. Event handler for a Pie Chart


Pie Chart 의 node(각 영역)를 클릭하였을 때 label 에 퍼센테이지를 표시하는 이벤트 핸들러를 작성해 보았습니다.


1. 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
package application;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
 
public class MainController {
 
    @FXML PieChart pieChart;
    
    @FXML Label status;
    
    public void btn(ActionEvent event){
        ObservableList<Data> list = FXCollections.observableArrayList(
            new PieChart.Data("Java"50),    
            new PieChart.Data("c++"20),
            new PieChart.Data("python"30),
            new PieChart.Data("c#"10),
            new PieChart.Data("c"15)
            );
        pieChart.setData(list);
        
        for (final PieChart.Data data : pieChart.getData()) {
            data.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
 
                @Override
                public void handle(MouseEvent event) {
                    status.setText(String.valueOf(data.getPieValue()/(125.00/100)) + "%");
                    
                }
            });
        }
    }
    
}
 
cs


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

JavaFX 29. Multiple Line Chart  (0) 2016.08.29
JavaFX 28. Line Chart  (0) 2016.08.29
JavaFX 26. Pie Chart  (0) 2016.08.29
JavaFX 25. WebView  (0) 2016.08.25
JavaFX 24. DatePicker  (0) 2016.08.25
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 26. Pie Chart

 


Pie Chart 는 주어진 데이터를 원형 그래프로 그려주는 기능 입니다.


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.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("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.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?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>
      <PieChart fx:id="pieChart" title="PIE CHART" />
      <Button layoutX="200.0" layoutY="433.0" mnemonicParsing="false" onAction="#btn" text="Load Chart" />
   </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
package application;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
 
public class MainController {
 
    @FXML PieChart pieChart;
    
    public void btn(ActionEvent event){
        ObservableList<Data> list = FXCollections.observableArrayList(
            new PieChart.Data("Java"50),    
            new PieChart.Data("c++"20),
            new PieChart.Data("python"30),
            new PieChart.Data("c#"10),
            new PieChart.Data("c"15)
            );
        pieChart.setData(list);
    }
    
}
 
cs



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

JavaFX 28. Line Chart  (0) 2016.08.29
JavaFX 27. Event handler for a Pie Chart  (0) 2016.08.29
JavaFX 25. WebView  (0) 2016.08.25
JavaFX 24. DatePicker  (0) 2016.08.25
JavaFX 23. TableView  (1) 2016.08.25
COMMENT
━━━━ ◇ ━━━━
Java/Java 프로젝트

Server and PieChart

1. Server


1.1 Server.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
// input your code herepackage server;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
 
public class Server extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/server/Server.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
 
}
 
cs

1.2 Server.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="server.ServerController">
   <children>
      <Label alignment="CENTER" layoutX="26.0" layoutY="28.0" prefHeight="35.0" prefWidth="452.0" text="Mobile OS 점유율">
         <font>
            <Font name="D2Coding" size="30.0" />
         </font>
      </Label>
      <Label layoutX="21.0" layoutY="128.0" text="Android">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Label layoutX="41.0" layoutY="217.0" text="IOS">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Label layoutX="31.0" layoutY="300.0" text="Linux">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <TextField fx:id="tf1" layoutX="132.0" layoutY="124.0" prefHeight="29.0" prefWidth="241.0" />
      <TextField fx:id="tf2" layoutX="132.0" layoutY="213.0" prefHeight="29.0" prefWidth="241.0" />
      <TextField fx:id="tf3" layoutX="132.0" layoutY="296.0" prefHeight="29.0" prefWidth="241.0" />
      <Button layoutX="259.0" layoutY="399.0" mnemonicParsing="false" onAction="#send" prefHeight="32.0" prefWidth="219.0" text="전송">
         <font>
            <Font name="D2Coding" size="18.0" />
         </font>
      </Button>
      <Button layoutX="21.0" layoutY="399.0" mnemonicParsing="false" onAction="#startServer" prefHeight="32.0" prefWidth="205.0" text="서버 시작">
         <font>
            <Font size="18.0" />
         </font>
      </Button>
   </children>
</AnchorPane>
 
cs

1.3 OSData.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
package server;
 
import java.io.Serializable;
 
public class OSData implements Serializable{
    int android;
    int ios;
    int linux;
    public OSData(int android, int ios, int linux) {
        super();
        this.android = android;
        this.ios = ios;
        this.linux = linux;
    }
    public int getAndroid() {
        return android;
    }
    public void setAndroid(int android) {
        this.android = android;
    }
    public int getIos() {
        return ios;
    }
    public void setIos(int ios) {
        this.ios = ios;
    }
    public int getLinux() {
        return linux;
    }
    public void setLinux(int linux) {
        this.linux = linux;
    }
    
    
}
 
cs

1.4 ServerController.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package server;
 
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
 
public class ServerController {
 
    @FXML private TextField tf1, tf2, tf3;
    
    ServerSocket server;
    Socket socket;
    
    
    class ServerThread implements Runnable{
 
        Socket socket;
        
        public ServerThread(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            
            int android = Integer.parseInt(tf1.getText());
            int ios = Integer.parseInt(tf2.getText());
            int linux = Integer.parseInt(tf3.getText());
            
            OSData osd = new OSData(android, ios, linux);
            
            try {
                OutputStream os = socket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(os);
                
                oos.writeObject(osd);
                oos.flush();
                
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
    }
    
    public void startServer(ActionEvent event){
        try {
            server = new ServerSocket(8888);
            System.out.println("서버가 시작되었습니다.");
            System.out.println("클라이언트 접속 대기중 입니다.");
            socket = server.accept();
            System.out.println("클라이언트("+socket.getInetAddress()+")가 접속 하였습니다.");
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
    public void send(ActionEvent event){
        Thread thread = new Thread (new ServerThread(socket));
        thread.start();
    }
 
}
 
 
cs

2. Client


2.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("/application/Main.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs

2.2 Main.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
27
28
29
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
 
<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>
      <Button layoutX="334.0" layoutY="441.0" mnemonicParsing="false" onAction="#btnClicked" text="결과 보기">
         <font>
            <Font size="18.0" />
         </font>
      </Button>
      <PieChart fx:id="pieChart" layoutX="6.0" layoutY="7.0" prefHeight="358.0" prefWidth="486.0" />
      <Label fx:id="status" alignment="CENTER" layoutX="19.0" layoutY="386.0" prefHeight="33.0" prefWidth="460.0" textFill="RED">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Button layoutX="95.0" layoutY="441.0" mnemonicParsing="false" onAction="#acceptServer" text="서버 접속">
         <font>
            <Font size="18.0" />
         </font>
      </Button>
   </children>
</AnchorPane>
 
cs

2.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package application;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.net.UnknownHostException;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import server.OSData;
 
public class MainController {
    
    @FXML PieChart pieChart;
    @FXML Label status;
    
    Socket socket;
    int android;
    int ios;
    int linux;
    
    public void acceptServer(ActionEvent event){
        try {
            socket = new Socket("192.168.0.20"8888);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("서버에 접속합니다.");
    }
    
    public void btnClicked(ActionEvent event){
        
        try {
            
            
            InputStream is = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            OSData osd = (OSData) ois.readObject();
            
            android = osd.getAndroid();
            ios = osd.getIos();
            linux = osd.getLinux();
            
            ObservableList<Data> list = FXCollections.observableArrayList(
                new PieChart.Data("Android", android),    
                new PieChart.Data("IOS", ios),    
                new PieChart.Data("Linux", linux)    
                );
            pieChart.setData(list);
            
            for (final PieChart.Data data : pieChart.getData()) {
                data.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
 
                    @Override
                    public void handle(MouseEvent event) {
                        status.setText(String.valueOf(data.getPieValue()) + "%");
                        
                    }
                });
            }
            
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
    }
}
 
cs


'Java > Java 프로젝트' 카테고리의 다른 글

Java 임금 계산기  (0) 2016.08.11
java swing 으로 music player 만들기  (0) 2016.08.10
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 25. WebView

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("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
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.web.WebView?>
 
 
<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController">
   <children>
      <WebView fx:id="webView" layoutX="14.0" layoutY="14.0" prefHeight="528.0" prefWidth="766.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="1.0" />
      <Button layoutX="14.0" layoutY="557.0" mnemonicParsing="false" onAction="#btn1" text="Load" />
      <Button layoutX="117.0" layoutY="557.0" mnemonicParsing="false" onAction="#btn2" text="Javascript" />
      <Button layoutX="254.0" layoutY="557.0" mnemonicParsing="false" onAction="#btn3" text="Html" />
      <Button layoutX="353.0" layoutY="557.0" mnemonicParsing="false" onAction="#btn4" text="Reload" />
   </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
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
 
public class MainController implements Initializable{
 
    @FXML private WebView webView;
    
    private WebEngine engine;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        engine = webView.getEngine();
        
    }
    
    public void btn1(ActionEvent event){
        engine.load("https://www.google.com");
    }
    
    public void btn2(ActionEvent event){
        engine.executeScript("window.location = \"http://easyonlineconverter.com//\";");
    }
 
    public void btn3(ActionEvent event){
        engine.loadContent("<html><body><h1>Hello World</h1></body></html>");
    }
 
    public void btn4(ActionEvent event){
        engine.reload();
    }
 
 
}
 
cs



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

JavaFX 27. Event handler for a Pie Chart  (0) 2016.08.29
JavaFX 26. Pie Chart  (0) 2016.08.29
JavaFX 24. DatePicker  (0) 2016.08.25
JavaFX 23. TableView  (1) 2016.08.25
JavaFX 22. RadioButton  (0) 2016.08.25
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 24. DatePicker

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.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("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.DatePicker?>
<?import javafx.scene.control.Label?>
<?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>
      <DatePicker fx:id="dp" editable="false" layoutX="56.0" layoutY="56.0" onAction="#showDate" prefHeight="64.0" prefWidth="387.0" showWeekNumbers="true" />
      <Label fx:id="showDateLbl" alignment="CENTER" layoutX="56.0" layoutY="191.0" prefHeight="64.0" prefWidth="387.0" text="Label" />
   </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
package application;
 
import java.time.LocalDate;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
 
public class MainController {
 
    @FXML private DatePicker dp;
    @FXML private Label showDateLbl;
    
    public void showDate(ActionEvent event){
        LocalDate ld = dp.getValue();
        showDateLbl.setText(ld.toString());
    }
}
 
cs



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

JavaFX 26. Pie Chart  (0) 2016.08.29
JavaFX 25. WebView  (0) 2016.08.25
JavaFX 23. TableView  (1) 2016.08.25
JavaFX 22. RadioButton  (0) 2016.08.25
JavaFX 21. CheckBox  (0) 2016.08.25
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 23. TableView

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.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("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
19
20
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?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>
      <TableView fx:id="table" layoutX="14.0" layoutY="14.0" prefHeight="500.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columns>
          <TableColumn fx:id="id" prefWidth="99.0" text="Id" />
          <TableColumn fx:id="name" prefWidth="133.0" text="Name" />
            <TableColumn fx:id="surname" prefWidth="147.0" text="Surname" />
            <TableColumn fx:id="age" prefWidth="120.0" text="Age" />
        </columns>
      </TableView>
   </children>
</AnchorPane>
 
cs


3. Student.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
package application;
 
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
 
public class Student {
 
    private final SimpleIntegerProperty id;
    private final SimpleStringProperty name;
    private final SimpleStringProperty surname;
    private final SimpleIntegerProperty age;
    public Student(Integer id, String name, String surname, Integer age) {
        super();
        this.id = new SimpleIntegerProperty(id);
        this.name = new SimpleStringProperty (name);
        this.surname = new SimpleStringProperty (surname);
        this.age = new SimpleIntegerProperty(age);
    }
    public Integer getId() {
        return id.get();
    }
    public String getName() {
        return name.get();
    }
    public String getSurname() {
        return surname.get();
    }
    public Integer getAge() {
        return age.get();
    }
 
    
}
 
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
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
 
public class MainController implements Initializable{
 
    @FXML private TableView<Student> table;
    @FXML private TableColumn<Student, Integer> id;
    @FXML private TableColumn<Student, String> name;
    @FXML private TableColumn<Student, String> surname;
    @FXML private TableColumn<Student, Integer> age;
    
    public ObservableList<Student> list = FXCollections.observableArrayList(
                new Student(1"Mark""surname1"22),
                new Student(2"Tom""surname2"30),
                new Student(3"Ben""surname3"15),
                new Student(4"John""surname4"65),
                new Student(5"Tomy""surname5"44),
                new Student(6"Jack""surname6"22)
            );
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        id.setCellValueFactory(new PropertyValueFactory<Student, Integer>("id"));
        name.setCellValueFactory(new PropertyValueFactory<Student, String>("name"));
        surname.setCellValueFactory(new PropertyValueFactory<Student, String>("surname"));
        age.setCellValueFactory(new PropertyValueFactory<Student, Integer>("age"));
        table.setItems(list);
        
    }
    
}
 
cs



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

JavaFX 25. WebView  (0) 2016.08.25
JavaFX 24. DatePicker  (0) 2016.08.25
JavaFX 22. RadioButton  (0) 2016.08.25
JavaFX 21. CheckBox  (0) 2016.08.25
JavaFX 20. Menu, MenuBar, MenuItem and SeparatorMenuItem  (0) 2016.08.25
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 22. RadioButton

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.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("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
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.ToggleGroup?>
<?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>
      <RadioButton fx:id="rb1" layoutX="70.0" layoutY="75.0" mnemonicParsing="false" onAction="#radioSelect" selected="true" text="Male">
         <toggleGroup>
            <ToggleGroup fx:id="gender" />
         </toggleGroup>
      </RadioButton>
      <RadioButton fx:id="rb2" layoutX="70.0" layoutY="157.0" mnemonicParsing="false" onAction="#radioSelect" text="Female" toggleGroup="$gender" />
      <Label fx:id="lbl" layoutX="109.0" layoutY="263.0" prefHeight="19.0" prefWidth="294.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
package application;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
 
public class MainController {
    
    @FXML private RadioButton rb1;
    @FXML private RadioButton rb2;
    @FXML private Label lbl;
    
    public void radioSelect(ActionEvent event){
        String message ="";
        if(rb1.isSelected()){
            message += rb1.getText() + "\n"
        }
        if(rb2.isSelected()){
            message += rb2.getText() + "\n"
        }
        lbl.setText(message);
    }
}
 
cs



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

JavaFX 24. DatePicker  (0) 2016.08.25
JavaFX 23. TableView  (1) 2016.08.25
JavaFX 21. CheckBox  (0) 2016.08.25
JavaFX 20. Menu, MenuBar, MenuItem and SeparatorMenuItem  (0) 2016.08.25
JavaFX 19. Bidirectional Binding and using Slider  (0) 2016.08.25
COMMENT
━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 21. CheckBox

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.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("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
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?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>
      <CheckBox fx:id="cb1" layoutX="80.0" layoutY="50.0" mnemonicParsing="false" onAction="#checkEvent" text="Dog" />
      <CheckBox fx:id="cb2" layoutX="80.0" layoutY="100.0" mnemonicParsing="false" onAction="#checkEvent" text="Cat" />
      <CheckBox fx:id="cb3" layoutX="80.0" layoutY="150.0" mnemonicParsing="false" onAction="#checkEvent" text="Cow" />
      <CheckBox fx:id="cb4" layoutX="80.0" layoutY="200.0" mnemonicParsing="false" onAction="#checkEvent" text="Rat" />
      <Label fx:id="lblCount" layoutX="106.0" layoutY="296.0" prefHeight="50.0" prefWidth="333.0" />
      <Label fx:id="lblList" layoutX="250.0" layoutY="51.0" prefHeight="173.0" prefWidth="191.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
package application;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
 
public class MainController {
    @FXML
    private CheckBox cb1;
    
    @FXML
    private CheckBox cb2;
    
    @FXML
    private CheckBox cb3;
    
    @FXML
    private CheckBox cb4;
    
    @FXML
    private Label lblList;
    
    @FXML
    private Label lblCount;
    
    public void checkEvent(ActionEvent event){
        int count = 0;
        String message = "";
        if(cb1.isSelected()){
            count++;
            message += cb1.getText() + "\n";
        }
        if(cb2.isSelected()){
            count++;
            message += cb2.getText() + "\n";
        }
        if(cb3.isSelected()){
            count++;
            message += cb3.getText() + "\n";
        }
        if(cb4.isSelected()){
            count++;
            message += cb4.getText() + "\n";
        }
        lblCount.setText("Items Selected : " + count);
        lblList.setText(message);
    }
}
 
cs



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

JavaFX 20. Menu, MenuBar, MenuItem and SeparatorMenuItem

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.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("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
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
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.layout.BorderPane?>
 
 
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
                  <Menu mnemonicParsing="false" text="Import">
                    <items>
                      <MenuItem mnemonicParsing="false" text="FXML" />
                        <MenuItem mnemonicParsing="false" text="Media" />
                    </items>
                  </Menu>
                  <MenuItem mnemonicParsing="false" text="Open" />
                  <MenuItem mnemonicParsing="false" text="Save" />
                  <SeparatorMenuItem mnemonicParsing="false" />
              <MenuItem mnemonicParsing="false" onAction="#closeApp" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>
 
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
package application;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
 
public class MainController implements Initializable {
 
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        
    }
    
    public void closeApp(ActionEvent event){
        Platform.exit();
        System.exit(0);
    }
}
 
cs





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

JavaFX 22. RadioButton  (0) 2016.08.25
JavaFX 21. CheckBox  (0) 2016.08.25
JavaFX 19. Bidirectional Binding and using Slider  (0) 2016.08.25
JavaFX 18. Binding, ProgressBar and ProgressIndicator  (0) 2016.08.25
JavaFX 17. Properties  (0) 2016.08.25
COMMENT
1 2 3 4 5 6 7 ··· 9