2018년 10월 31일 수요일

[JavaFx] TextField Example


1. TextField Example
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io.File;
import java.util.List;

import javafx.event.EventHandler;
import javafx.scene.control.TextField;
import javafx.scene.input.Clipboard;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class FxTextField implements FxNode {
    
    TextField text;
    
    public FxTextField(Runnable runnable) {
        text = new TextField();
        FxDrop.setDropHandler(text, new FxRunnable() {
            @SuppressWarnings("unchecked")
            @Override public void run(Object... object) {
                if( object[0] instanceof List<?> ) {
                    List<File> files = (List<File>) object[0];
                    text.setText( files.get(0).getPath() );
                }
            }
        });
        if( runnable != null ) {
            text.textProperty().addListener((observable, oldValue, newValue) -> {
                runnable.run();
            });
        }
    }
    
    public FxTextField() {
        this(null);
    }

    @Override
    public TextField get() {
        return text;
    }
    
    public void setOnEnterHandler(Runnable runnable) {
        text.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override public void handle(KeyEvent event) {
                if (event.getCode().equals( KeyCode.ENTER) ) {
                    runnable.run();
                }                
            }
        });
    }
    
    public void clear() {
        text.clear();
    }
    
    public String getText() {
        return text.getText();
    }
    
    public void setText(String value) {
        text.setText(value);
    }
    
    public void setFontSize(int size) {
        if( size > 0 ) {
            text.setStyle("-fx-font-size: " + size + ";"); //1em = 50px
        }
    }
    
    public void clearAllText() {
        text.clear();
    }
    
    public void copyAllText() {
        text.selectAll();
        text.copy();
        text.deselect();
    }
    
    public void pasteAllText() {
        text.selectAll();
        text.paste();
        text.deselect();
    }
    
    public void pasteAllHtml() {
        final Clipboard clipboard = Clipboard.getSystemClipboard();
        String data;
        if( clipboard.hasHtml() ) {
            data = clipboard.getHtml();
        } else {
            data = clipboard.getString();
        }
        text.setText(data);          
    }
}

[JavaFx] TitledPane and Accordion Example


1. TitledPane Example
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
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class FxTitlePane extends Application implements FxNode {

    TitledPane pane;
    
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 400, 250, Color.WHITE);        
        BorderPane borderPane = new BorderPane();
        borderPane.prefHeightProperty().bind(scene.heightProperty());
        borderPane.prefWidthProperty().bind(scene.widthProperty());        
        borderPane.setCenter(pane);
        root.getChildren().add(borderPane);
        stage.setTitle("TitledPane");
        stage.setScene(scene);
        stage.show();
        
        add("TitleB", new Label("TitleBB"), true);
    }
    
    public FxTitlePane() {
        pane = new TitledPane();
    }
    
    public FxTitlePane(String title, Node item, boolean scrollable) {
        this();
        add( title, item, scrollable );
    }
    
    @Override
    public TitledPane get() {
        return pane;
    }

    public void add(String title, Node item, boolean scrollable) {
        pane.setText(title);
        pane.setContent(item);
        pane.setExpanded(true);
        pane.setCollapsible(scrollable);
    }    
}


2. TitledPanes (Accordion) Example
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
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class FxTitlePanes extends Application implements FxNode {

    Accordion pane;
    
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 400, 250, Color.WHITE);        
        BorderPane borderPane = new BorderPane();
        borderPane.prefHeightProperty().bind(scene.heightProperty());
        borderPane.prefWidthProperty().bind(scene.widthProperty());        
        borderPane.setCenter(pane);
        root.getChildren().add(borderPane);
        stage.setTitle("TitledPane");
        stage.setScene(scene);
        stage.show();
        
        add("TitleA", new Label("TitleAA"), true);
        add("TitleB", new Label("TitleBB"), true);
    }
    
    public FxTitlePanes() {
        pane = new Accordion();
    }
    
    @Override
    public Accordion get() {
        return pane;
    }
    
    public void add(String title, Node item, boolean scrollable) {
        TitledPane titledPane = new TitledPane();
        titledPane.setText(title);
        titledPane.setContent(item);
        titledPane.setExpanded(true);
        titledPane.setCollapsible(scrollable);
        pane.getPanes().add(titledPane);
    }

    public void add(String title, Node item) {
        add( title, item, true );
    }    
}



[JavaFx] TabPane Example


1. TabPane Example
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
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class FxTabPane extends Application implements FxNode {

    TabPane pane;
    
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 400, 250, Color.WHITE);        
        BorderPane borderPane = new BorderPane();
        borderPane.prefHeightProperty().bind(scene.heightProperty());
        borderPane.prefWidthProperty().bind(scene.widthProperty());        
        borderPane.setCenter(pane);
        root.getChildren().add(borderPane);
        stage.setTitle("Tabs");
        stage.setScene(scene);
        stage.show();
        
        add("TabA", new Label("TabAA"));
        add("TabB", new Label("TabBB"));
        add("TabC", new Label("TabCC"));
    }
    
    public FxTabPane() {
        pane = new TabPane();
    }
    
    @Override
    public TabPane get() {
        return pane;
    }
    
    public void add(String title, Node item) {
        Tab tab = new Tab();
        tab.setText(title);
        tab.setContent(item);
        pane.getTabs().add(tab);
    }
    
    public void add(String title, FxNode item) {
        add(title, item.get());
    }
}

[JavaFx] SplitPane Example


1. SplitPane Example
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
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.SplitPane;

public class FxSplitPane implements FxNode {

    SplitPane pane;
    
    public FxSplitPane() {
        pane = new SplitPane();
        pane.setOrientation(Orientation.HORIZONTAL);
        pane.setDividerPositions(0.5, 0.5);
    }
    
    @Override
    public SplitPane get() {
        return pane;
    }
    
    public void setVertical() {
        pane.setOrientation(Orientation.VERTICAL);
    }
    
    public void add(Node item) {
        pane.getItems().add(item);
    }
    
    public void add(FxNode item) {
        pane.getItems().add(item.get());
    }
}

[JavaFx] HBox Example


1. HBox Example

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
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Separator;
import javafx.scene.layout.HBox;

public class FxHBox implements FxNode {
    
    HBox box;
    
    public FxHBox() {
        box = new HBox(0);
        box.setAlignment(Pos.CENTER);
        box.setPadding(new Insets(0, 0, 0, 0)); //T,R,B,L
    }

    public FxHBox(int gap) {
        box = new HBox(gap);
        box.setAlignment(Pos.CENTER);
        box.setSpacing(gap);
        box.setPadding(new Insets(0, 0, 0, 0)); //T,R,B,L
    }
    
    @Override
    public HBox get() {
        return box;
    }

    public void add(FxNode item) {
        box.getChildren().add(item.get());
    }

    public void add(Node item) {
        box.getChildren().add(item);
    }

    public void addSeparator() {
        Separator sep = new Separator();
        sep.setOrientation(Orientation.VERTICAL);
        box.getChildren().add(sep);
    }

}


2. VBox Example
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
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class FxVBox implements FxNode {
    
    final int gap = 0;
    VBox box;

    public FxVBox() {
        box = new VBox(gap);
        box.setAlignment(Pos.CENTER);
        box.setSpacing(gap);
        box.setPadding(new Insets(0, 0, 0, 0)); //T,R,B,L
    }
    
    @Override
    public VBox get() {
        return box;
    }
    
    public void add(Node item) {
        box.getChildren().add(item);
    }
    
    public void add(FxNode item) {
        box.getChildren().add(item.get());
    }
}





[JavaFx] FlowPane Example


1. FlowPane Example
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
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class FxFlowPane extends Application implements FxNode {

    FlowPane pane;
    
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 400, 250, Color.WHITE);        
        BorderPane borderPane = new BorderPane();
        borderPane.prefHeightProperty().bind(scene.heightProperty());
        borderPane.prefWidthProperty().bind(scene.widthProperty());        
        borderPane.setCenter(pane);
        root.getChildren().add(borderPane);
        stage.setTitle("TitledPane");
        stage.setScene(scene);
        stage.show();
        
        vertical();
        for( int i = 0; i < 100; i++ ) {
            add( new Button("Button" + i ) );
        }
    }
    
    public FxFlowPane() {
        pane = new FlowPane();
        pane.setPadding(new Insets(0, 0, 0, 0));
        pane.setVgap(8);
        pane.setHgap(4);
    }
    
    @Override
    public FlowPane get() {
        return pane;
    }

    public void add(Node item) {
        pane.getChildren().add(item);
    }
    
    public void add(FxNode item) {
        add(item.get());
    }    
    
    public void vertical() {
        pane.setOrientation(Orientation.VERTICAL);
    }
}

[JavaFx] BorderPane Example


1. BorderPane
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
public class FxBorderPane implements FxNode {
    
    BorderPane pane;
    
    public FxBorderPane() {
        pane = new BorderPane();
        pane.setPadding(new Insets(0, 0, 0, 0));    
    }
    
    @Override
    public BorderPane get() {
        return pane;
    }
    
    public void setTop(Node item) {
        pane.setTop(item);
    }

    public void setBottom(Node item) {
        pane.setBottom(item);
    }
    
    public void setLeft(Node item) {
        pane.setLeft(item);
    }

    public void setRight(Node item) {
        pane.setRight(item);
    }
    
    public void setCenter(Node item) {
        pane.setCenter(item);
    }    
    
    public void setTop(FxNode item) {
        pane.setTop(item.get());
    }

    public void setBottom(FxNode item) {
        pane.setBottom(item.get());
    }
    
    public void setLeft(FxNode item) {
        pane.setLeft(item.get());
    }

    public void setRight(FxNode item) {
        pane.setRight(item.get());
    }
    
    public void setCenter(FxNode item) {
        pane.setCenter(item.get());
    }    
}


[Java] Zip, Unzip Example (zip4j)




1. Zip Example
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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

public class ZipUtil {

    ZipFile zipFile = null;
    ZipParameters parameters;

    public ZipUtil(String zipFileName) throws IOException, ZipException {
        zipFile = new ZipFile(zipFileName);
        parameters = new ZipParameters();
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);
    }
    
    public void addFolder(String folder, boolean includeRootFolder) throws ZipException {
        parameters.setSourceExternalStream(false);
        parameters.setFileNameInZip(null);        
        parameters.setIncludeRootFolder(includeRootFolder);
        zipFile.addFolder(folder, parameters);
    }

    public void addFile(String file, String target) throws FileNotFoundException, ZipException {
        parameters.setSourceExternalStream(true);
        parameters.setFileNameInZip(target);
        zipFile.addStream(new FileInputStream(file), parameters);
    }
    
    public void setRootFolderInZip(String folder) {
        parameters.setRootFolderInZip(folder);
    }
    
    public void setIncludeRootFolder(boolean bool) {
        parameters.setIncludeRootFolder(bool);
    }

    public void setFileNameInZip(String fileName) {
        parameters.setFileNameInZip(fileName);
    }
    /*
    public static void main(String[] args) throws IOException, ZipException {
        ZipUtil zip = new ZipUtil("D:/zip4j4.zip");
        zip.addFile("D:/Temp2/a.png", "test5/xxx.png");
        zip.setIncludeRootFolder(false);
        zip.setRootFolderInZip("test2");
        zip.addFolder("D:/Temp1/b/e/FxLibs", true);
        zip.setRootFolderInZip("test2");
    }
    */
}


2. Unzip Example
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
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;

public class UnzipUtil {

    ZipFile zipFile = null;

    public UnzipUtil() {

    }
    
    public UnzipUtil(String zipFileName) throws IOException, ZipException {
        zipFile = new ZipFile(zipFileName);
    }
    
    public void load(String zipFileName) throws ZipException {
        zipFile = new ZipFile(zipFileName);
    }

    public void extractAll(String target) throws ZipException {
        zipFile.extractAll(target);
    }
    
    public void extractFile(int index, String target) throws ZipException {
        zipFile.extractFile((FileHeader) zipFile.getFileHeaders().get(index), target);
    }
    
    public void extractFile(String name, String target) throws ZipException {
        zipFile.extractFile(zipFile.getFileHeader(name), target);
    }
    
    public int size() {
        try {
            return zipFile.getFileHeaders().size();
        } catch (ZipException e) {
            e.printStackTrace();
            return 0;
        }
    }
    
    public String name(int index) throws ZipException {
        return ((FileHeader) zipFile.getFileHeaders().get(index)).getFileName();
    }

    public byte[] unzipEntry(FileHeader zipEntry) throws IOException, ZipException {
        if( zipEntry == null ) {
            return null;
        }
        if( zipEntry.isDirectory() ) {
            return null;
        }
        return copyStreamToByteArray( zipFile.getInputStream(zipEntry) );
    }
    
    public byte[] unzipEntry(String name) throws IOException, ZipException {
        return unzipEntry( zipFile.getFileHeader(name));
    }
    
    public byte[] unzipEntry(int index) throws IOException, ZipException {
        return unzipEntry( (FileHeader) zipFile.getFileHeaders().get(index));
    }

    public byte[] copyStreamToByteArray(InputStream input ) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int n = 0;
        while( (n = input.read(buffer)) > 0 ) {
          output.write(buffer, 0, n);
        }
        output.close();
        return output.toByteArray();
    }
    /*
    public static void main(String[] args) throws IOException, ZipException {
        UnzipUtil unzip = new UnzipUtil("D:/a.zip");
        unzip.extractAll("D:/Temp9");
        UnzipUtil uz = new UnzipUtil("D:/a.zip");
        for( int i = 0; i < uz.size(); i++ ) {
            System.out.println(uz.name(i));
            byte[] fileData = uz.unzipEntry(i);
            System.out.println("----------------------------------------");
            System.out.println(uz.name(i));
            System.out.println("----------------------------------------");
            System.out.println(new String(fileData));
        }
    }
    */
}