/Swing-Markdown-Preview

πŸ“Swing Markdown Preview is a Java library to render GitHub-flavored Markdown in Swing apps. Supports live preview, themes, emoji, tables, and Flexmark integration out of the box.

Primary LanguageJavaMIT LicenseMIT

πŸ“ Swing Markdown Preview

Render beautiful GitHub-style Markdown in your Java Swing apps.

⚑ Lightweight. πŸ’Ό Production Ready. 🧩 Fully Customizable.

Maven Central Build Status CodeQL Security Javadoc Latest Release Buy Me A Coffee


✨ What is Swing Markdown Preview?

Swing Markdown Preview is a modern Java library to display Markdown content in Swing applications.
With GitHub-like styling, theme switching, and live updating, you can turn your text into beautifully rendered HTML without complex setup.


🎯 Who Is It For?

  • 🧱 Java Swing Developers needing Markdown rendering

  • πŸ“– Note-taking or documentation tool makers

  • πŸ§‘β€πŸ« Students or teachers building markdown-based learning tools

  • βš™οΈ IDE or editor plugin developers needing a quick Markdown viewer


βš–οΈ Minimal vs All-In-One JAR

Variant Includes Flexmark? Use Case
swing-markdown-preview ❌ No Maven users managing dependencies manually
swing-markdown-preview-all βœ… Yes (flexmark-all) Anyone who wants it to "just work" out of the box

πŸš€ Installation

✨ All-In-One (Recommended for most users)

Maven

<dependency>
  <groupId>io.github.raghul-tech</groupId>
  <artifactId>swing-markdown-preview-all</artifactId>
  <version>1.0.0</version>
</dependency>

πŸͺΆ Minimal (No Flexmark bundled)

Maven

<dependency>
  <groupId>io.github.raghul-tech</groupId>
  <artifactId>swing-markdown-preview</artifactId>
  <version>1.0.0</version>
</dependency>

<dependency>
  <groupId>com.vladsch.flexmark</groupId>
  <artifactId>flexmark-all</artifactId>
  <version>0.64.8</version>
</dependency>

πŸ’‘ Features

βœ… GitHub-style Markdown rendering βœ… Live updates βœ… Theme switching βœ… Optional Flexmark bundling βœ… Fat jar available βœ… Java 8+ compatible


🧩 Usage Examples by Class

  • Below are examples of how to use each main class:

🎯 1. SwingMarkdownTabbedPreview

  • Use this when you want to add a Markdown preview tab to your own JTabbedPane.

  • βœ… Perfect for apps with multiple tabs (like editors).

✨ Example:

import java.awt.BorderLayout;
import java.io.File;
import javax.swing.*;
import io.github.raghultech.markdown.swing.preview.SwingMarkdownTabbedPreview;

public class ExampleSwingTab {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Markdown Preview Tabs");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1200, 900);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new BorderLayout());

        // Create your tabbed pane
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Editor 1", new JScrollPane(new JTextArea()));
        tabbedPane.addTab("Editor 2", new JScrollPane(new JTextArea()));

        frame.add(tabbedPane, BorderLayout.CENTER);

        // Create preview and attach it
        File markdownFile = new File("README.md");
        SwingMarkdownTabbedPreview preview = new SwingMarkdownTabbedPreview(tabbedPane, markdownFile);
        preview.launchPreviewTab();

        frame.setVisible(true);
    }
}

🎯 2. SwingMarkdownScrollPanePreview

  • Use this when you want a JScrollPane with preview content you can embed in any layout.

  • βœ… Great if you want scrolling built in.

✨ Example:

import java.awt.Desktop;
import java.io.File;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import io.github.raghultech.markdown.swing.preview.SwingMarkdownScrollPanePreview;

public class ExampleSwingScrollPane {
    public static void main(String[] args) {
        File markdownFile = new File("README.md");

        // Optionally use your own JEditorPane for custom behavior
        /*
         * if u want u can add a JEditorPane or leave it will take care
         */ 
        JEditorPane myPane = new JEditorPane();
        myPane.setContentType("text/html");
        myPane.setEditable(false);
        myPane.addHyperlinkListener(e -> {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                try {
                    Desktop.getDesktop().browse(e.getURL().toURI());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        SwingMarkdownScrollPanePreview preview = new SwingMarkdownScrollPanePreview(markdownFile);
        preview.setEditorPane(myPane);

        JScrollPane previewPane = preview.createPreview();

        JFrame frame = new JFrame("Markdown ScrollPane Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.add(previewPane);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

🎯 3. SwingMarkdownPanelPreview

  • Use this if you need a JPanel you can put anywhere.

  • βœ… Ideal for embedding in custom UIs.

✨ Example:

import java.io.File;
import javax.swing.*;
import io.github.raghultech.markdown.swing.preview.SwingMarkdownPanelPreview;

public class ExampleSwingPanel {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Markdown Panel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);

        File file = new File("README.md");
        SwingMarkdownPanelPreview previewPanel = new SwingMarkdownPanelPreview(file);

        frame.getContentPane().add(previewPanel);
        frame.setVisible(true);
    }
}

🎯 4. SwingMarkdownWindowPreview

  • Use this when you want a standalone window that displays your Markdown.

  • βœ… Great for β€œPreview” buttons.

✨ Example:

import java.io.File;
import javax.swing.JFrame;
import io.github.raghultech.markdown.swing.preview.SwingMarkdownWindowPreview;

public class ExampleSwingWindow {
    public static void main(String[] args) {
        File file = new File("README.md");

        SwingMarkdownWindowPreview preview = new SwingMarkdownWindowPreview(file);

        preview.setWindowTitle("My Markdown Viewer");
        preview.setWindowSize(900, 700);
        preview.launchPreview();
    }
}

πŸ—οΈ Example Projects

βœ… To run an example:

  1. Download or clone this repository.

  2. Navigate to examples/.

  3. Compile and run the desired file.


🎨 MarkdownTheme

  • Simple enum to toggle light or dark theme.

βœ… Available values:

  • πŸ”Ή Example:
preview.isDarkMode(true);

You can call isDarkMode() any timeβ€”your preview updates immediately.


πŸ“‚ How to Use the JAR

Compile:

javac -cp swing-markdown-preview-all-1.0.0.jar MyPreviewApp.java

Run:

Windows:

java -cp .;swing-markdown-preview-all-1.0.0.jar MyPreviewApp

macOS/Linux:

java -cp .:swing-markdown-preview-all-1.0.0.jar MyPreviewApp

πŸ” Documentation


πŸ†• Changelog


🀝 Contributing

  • We welcome all contributions!

    • πŸ› Bug fixes

    • ✨ Features

    • πŸ“ Documentation improvements

    • πŸ§ͺ Example enhancements

πŸ‘‰ Contributing Guide


🐞 Report a Bug


πŸ“„ License


β˜• Support