- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExampleTabPane.java
More file actions
Latest commit
144 lines (127 loc) · 4.93 KB
/
Copy pathExampleTabPane.java
File metadata and controls
144 lines (127 loc) · 4.93 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (C) 2025 Raghul-tech
* https://github.com/raghul-tech
* This file is part of JavaFX Markdown Preview.
*
* JavaFX Markdown Preview is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JavaFX Markdown Preview is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JavaFX Markdown Preview. If not, see <https://www.gnu.org/licenses/>.
*/
importjavafx.application.Application;
importjavafx.scene.Scene;
importjavafx.scene.control.*;
importjavafx.scene.layout.BorderPane;
importjavafx.stage.FileChooser;
importjavafx.stage.Stage;
importjava.io.File;
importio.github.raghultech.markdown.javafx.preview.MarkdownTab;
/**
* ExampleTabPane demonstrates how to integrate MarkdownTab into a JavaFX TabPane.
* It shows how to open Markdown files dynamically and preview them in separate tabs.
*/
publicclassExampleTabPaneextendsApplication {
/**
* Application entry point when launched from the command line.
*
* @param args command-line arguments
*/
publicstaticvoidmain(String[] args) {
launch(args);
}
/**
* Called by JavaFX when the application starts.
*
* @param primaryStage the main window
*/
@Override
publicvoidstart(StageprimaryStage) {
// Create the main TabPane that holds all tabs
TabPanetabPane = newTabPane();
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.ALL_TABS);
// Add some initial tabs containing text editors
tabPane.getTabs().addAll(
createEditorTab("Editor 1"),
createEditorTab("Editor 2")
);
// Create the root BorderPane and embed the tab pane
BorderPaneroot = newBorderPane(tabPane);
Scenescene = newScene(root, 1200, 900);
// Add a MenuBar with a File -> Open Markdown option
MenuBarmenuBar = newMenuBar();
MenufileMenu = newMenu("File");
MenuItemopenItem = newMenuItem("Open Markdown");
openItem.setOnAction(e -> openMarkdownFile(tabPane));
fileMenu.getItems().add(openItem);
menuBar.getMenus().add(fileMenu);
root.setTop(menuBar);
// Set up the main window
primaryStage.setTitle("Markdown Preview Tabs (JavaFX)");
primaryStage.setScene(scene);
primaryStage.show();
// Example: Automatically open a markdown file on startup
FilemarkdownFile = newFile("README.md");
if (markdownFile.exists()) {
createMarkdownPreviewTab(tabPane, markdownFile);
}
}
/**
* Creates a Tab containing a TextArea editor.
*
* @param title the title of the tab
* @return the Tab with an editable TextArea
*/
privateTabcreateEditorTab(Stringtitle) {
Tabtab = newTab(title);
TextAreatextArea = newTextArea();
textArea.setWrapText(true);
tab.setContent(newScrollPane(textArea));
returntab;
}
/**
* Opens a file chooser to let the user select a Markdown file,
* then creates a preview tab for it.
*
* @param tabPane the TabPane to which the new tab will be added
*/
privatevoidopenMarkdownFile(TabPanetabPane) {
FileChooserfileChooser = newFileChooser();
fileChooser.setTitle("Open Markdown File");
fileChooser.getExtensionFilters().addAll(
newFileChooser.ExtensionFilter("Markdown Files", "*.md", "*.markdown"),
newFileChooser.ExtensionFilter("Text Files", "*.txt"),
newFileChooser.ExtensionFilter("All Files", "*.*")
);
Filefile = fileChooser.showOpenDialog(tabPane.getScene().getWindow());
if (file != null) {
createMarkdownPreviewTab(tabPane, file);
}
}
/**
* Creates a new tab with a Markdown preview using MarkdownTab.
*
* @param tabPane the TabPane to which the preview tab is added
* @param file the Markdown file to render
*/
privatevoidcreateMarkdownPreviewTab(TabPanetabPane, Filefile) {
// Create a new Tab to hold the preview
TabpreviewTab = newTab("Preview: " + file.getName());
tabPane.getTabs().add(previewTab);
// Initialize MarkdownTab
MarkdownTabpreview = newMarkdownTab(previewTab, file);
preview.setHostServices(getHostServices());
preview.setTabName("Preview: " + file.getName());
// preview.setDarkMode(true); // Enable dark theme
preview.launchTab();
// Select the preview tab
tabPane.getSelectionModel().select(previewTab);
}
}