-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextFileCopier.java
96 lines (78 loc) · 2.88 KB
/
TextFileCopier.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class TextFileCopier extends Application {
private String[] lines;
private int currentIndex = 0;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Easy Paste Developed By AD178");
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text Files", "*.txt"));
Button chooseFileButton = new Button("Choose File");
chooseFileButton.setOnAction(e -> chooseFile(fileChooser, primaryStage));
TextArea textDisplay = new TextArea();
textDisplay.setEditable(false);
textDisplay.setPrefSize(400, 200);
VBox vbox = new VBox(10);
vbox.getChildren().addAll(chooseFileButton, textDisplay);
Scene scene = new Scene(vbox, 400, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
private void chooseFile(FileChooser fileChooser, Stage primaryStage) {
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
currentIndex = 0;
readLinesFromFile(file);
copyLineToClipboard();
}
}
private void readLinesFromFile(File file) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
StringBuilder content = new StringBuilder();
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
lines = content.toString().split("\n");
} catch (IOException e) {
e.printStackTrace();
}
}
private void copyLineToClipboard() {
if (lines != null && currentIndex >= 0 && currentIndex < lines.length) {
String line = lines[currentIndex].trim();
javafx.scene.input.Clipboard clipboard = javafx.scene.input.Clipboard.getSystemClipboard();
javafx.scene.input.ClipboardContent content = new javafx.scene.input.ClipboardContent();
content.putString(line);
clipboard.setContent(content);
}
}
public void incrementIndex() {
if (lines != null && currentIndex < lines.length - 1) {
currentIndex++;
copyLineToClipboard();
}
}
public void decrementIndex() {
if (lines != null && currentIndex > 0) {
currentIndex--;
copyLineToClipboard();
}
}
public void copyLine() {
copyLineToClipboard();
}
}