Skip to main content

Overview

JSqlEditor is a professional SQL editor component built on RichTextFX with intelligent features including syntax highlighting, autocomplete, real-time validation, and code formatting.

Constructor

JSqlEditor editor = new JSqlEditor();
Creates a new SQL editor with default configuration.

Properties

readOnly
BooleanProperty
Controls whether the editor is read-only
  • Default: false
  • Editable when false, read-only when true
darkMode
BooleanProperty
Controls the editor theme
  • Default: false (light mode)
  • Automatically switches stylesheets and colors
errors
ObservableList<SqlError>
List of SQL validation errors
  • Updated automatically after 400ms debounce
  • Each error contains line, column, and message

Core Methods

Text Management

getSql
String
Returns the current SQL text
String sql = editor.getSql();
setSql
void
Sets the SQL text
editor.setSql("SELECT * FROM users WHERE active = true");
sql
String
SQL text to set in the editor
getText
String
Alias for getSql()
setText
void
Alias for setSql(String)
clear
void
Clears all text from the editor
editor.clear();

Formatting & Validation

formatSql
void
Formats the SQL text using SqlFormatter
editor.formatSql();
  • Applies standard SQL formatting
  • Uppercase keywords
  • 4-space indentation
  • 80-character max column length
  • Keyboard shortcut: Ctrl+Shift+F
getSqlErrors
ObservableList<SqlError>
Returns observable list of validation errors
ObservableList&lt;SqlError&gt; errors = editor.getSqlErrors();
errors.addListener((ListChangeListener.Change<? extends SqlError> c) -> {
    System.out.println("Errors: " + c.getList().size());
});

Focus & Navigation

focusEditor
void
Requests focus on the code editor
editor.focusEditor();
selectAll
void
Selects all text in the editor
editor.selectAll();
scrollToTop
void
Scrolls the editor to the top-left position
editor.scrollToTop();

Theme

toggleDarkMode
void
Toggles between light and dark theme
editor.toggleDarkMode();
  • Switches color scheme instantly
  • Updates status bar indicator
setDarkMode
void
Sets the dark mode state
editor.setDarkMode(true);
dark
boolean
true for dark mode, false for light mode
isDarkMode
boolean
Returns current dark mode state
boolean isDark = editor.isDarkMode();

Clipboard

copyToClipboard
void
Copies the entire SQL text to system clipboard
editor.copyToClipboard();

Advanced Access

getCodeArea
CodeArea
Returns the underlying RichTextFX CodeArea for advanced operations
CodeArea codeArea = editor.getCodeArea();
codeArea.setStyle("-fx-font-size: 14px;");
dispose
void
Cleans up subscriptions and resources
editor.dispose();
  • Call when removing the editor from the scene
  • Unsubscribes from reactive streams

Keyboard Shortcuts

ShortcutAction
TabInsert 4 spaces or accept autocomplete
BackspaceSmart delete 4 spaces if preceded by indent
EnterSmart indent based on previous line
Ctrl+SpaceTrigger autocomplete
Ctrl+/Toggle line comment
Ctrl+DDuplicate current line
Ctrl+Shift+KDelete current line
Ctrl+Shift+FFormat SQL

Auto-Complete

  • Triggers automatically after typing 2+ characters
  • Includes SQL keywords, data types, and functions
  • Navigate with arrow keys, accept with Enter or Tab
  • Force trigger with Ctrl+Space

Syntax Highlighting

Supported token types:
  • Keywords: SELECT, FROM, WHERE, JOIN, etc.
  • Data Types: VARCHAR, INTEGER, TIMESTAMP, etc.
  • Functions: COUNT, SUM, AVG, COALESCE, etc.
  • Strings: Single-quoted literals
  • Comments: Line (--) and block (/* */)
  • Numbers: Integers, decimals, scientific notation
  • Operators: =, <>, +, -, etc.

Real-Time Validation

  • Validates SQL syntax using JSqlParser
  • 400ms debounce to avoid performance impact
  • Highlights error lines in red
  • Shows error count in status bar
  • Click error indicator to navigate to first error

SqlError Record

public record SqlError(int line, int startCol, int endCol, String message) {}
line
int
Line number (1-based)
startCol
int
Starting column (0-based)
endCol
int
Ending column (0-based)
message
String
Error description in Spanish

Example Usage

JSqlEditor editor = new JSqlEditor();
editor.setPrefSize(800, 600);

// Set initial SQL
editor.setSql("""
    SELECT u.id, u.name, COUNT(o.id) as order_count
    FROM users u
    LEFT JOIN orders o ON o.user_id = u.id
    WHERE u.active = true
    GROUP BY u.id, u.name
    ORDER BY order_count DESC
    """);

// Listen for errors
editor.getSqlErrors().addListener((Change<? extends SqlError> c) -> {
    if (editor.getSqlErrors().isEmpty()) {
        System.out.println("SQL is valid!");
    } else {
        editor.getSqlErrors().forEach(err -> 
            System.out.println("Error at line " + err.line() + ": " + err.message())
        );
    }
});

// Format on button click
JButton formatBtn = new JButton("Format", JIcon.CODE);
formatBtn.setOnAction(e -> editor.formatSql());

// Enable dark mode
editor.setDarkMode(true);

Styling

The component uses separate stylesheets for light and dark themes:
  • /css/sql-editor-light.css
  • /css/sql-editor-dark.css
Style classes:
  • .j-sql-editor - Main container
  • .j-sql-code-area - Code editing area
  • .j-sql-status-bar - Bottom status bar
  • .sql-keyword, .sql-type, .sql-function - Syntax tokens
  • .sql-error-line - Error highlighted lines

Notes

  • Built on RichTextFX for high-performance text rendering
  • Uses JSqlParser for SQL validation
  • Supports PostgreSQL, MySQL, Oracle, SQL Server dialects
  • Line numbers are always visible
  • Status bar shows cursor position and error count

Build docs developers (and LLMs) love