Overview
TheJSqlEditor component is a full-featured SQL editor built on RichTextFX that provides:
- Real-time syntax highlighting for SQL keywords, functions, types, and literals
- Intelligent autocomplete with 100+ SQL keywords, types, and functions
- Live SQL validation using JSqlParser
- Smart editing features (auto-indent, auto-closing pairs, line operations)
- Dark/light theme support
- Line numbers and status bar with error indicators
The SQL editor uses RichTextFX’s
CodeArea for advanced text editing capabilities and includes debounced validation to maintain performance.Basic Usage
Syntax Highlighting
The editor automatically highlights SQL tokens with distinct styles:Token Pattern
The editor recognizes these token types (JSqlEditor.java:80-91):- Block comments:
/* ... */ - Line comments:
-- comment - String literals:
'text' - Quoted identifiers:
"column_name" - Numbers:
123,45.67,1.2e-3 - Keywords/identifiers: SQL keywords and table/column names
- Operators:
=,<>,+,-,*,/ - Punctuation:
(),[],{},;,,,.
Autocomplete
Trigger autocomplete in multiple ways:Autocomplete Sources
The editor provides suggestions from (JSqlEditor.java:32-78):- 46 SQL keywords: SELECT, FROM, WHERE, JOIN, etc.
- 60+ data types: VARCHAR, INTEGER, JSON, TIMESTAMP, etc.
- 78 SQL functions: COUNT, SUM, CONCAT, DATE_TRUNC, etc.
SQL Validation
Live validation with visual error indicators:Error Display
Errors are shown in multiple places (JSqlEditor.java:601-627):- Error lines: Highlighted with red background
- Status bar: Shows error count (
✕ 2 errors) - Click to navigate: Click error count to jump to first error
Smart Editing Features
Keyboard Shortcuts
| Shortcut | Action | Description |
|---|---|---|
Ctrl+Space | Trigger autocomplete | Show all available completions |
Ctrl+Shift+F | Format SQL | Auto-format with proper indentation |
Ctrl+/ | Toggle comment | Comment/uncomment current line |
Ctrl+D | Duplicate line | Duplicate the current line |
Ctrl+Shift+K | Delete line | Delete the current line |
Tab | Indent | Insert 4 spaces (or accept autocomplete) |
Backspace | Smart unindent | Delete 4 spaces if preceded by 4 spaces |
Auto-closing Pairs
The editor automatically closes pairs (JSqlEditor.java:331-336):Smart Enter
Press Enter to maintain indentation (JSqlEditor.java:353-370):Auto-uppercase Keywords
Keywords are automatically uppercased when you type space or semicolon (JSqlEditor.java:420-434):SQL Formatting
Format SQL with proper indentation and style:Format Configuration
The formatter uses these settings (JSqlEditor.java:476-491):- Indent: 4 spaces
- Uppercase: All keywords
- Max column length: 80 characters
- Lines between queries: 1 blank line
- Dialect: Standard SQL
Before and After Formatting
Before and After Formatting
Before:After:
Dark Mode
Toggle between light and dark themes:Light Theme
Clean white background with soft shadows and subtle borders
Dark Theme
Dark slate background (
#0f172a) with adjusted syntax colorsProperties and Methods
Text Content
Read-only Mode
Editor Control
Advanced Access
Access the underlying RichTextFX CodeArea for advanced features:Complete Example
Cleanup
Dispose of subscriptions when done:The editor uses reactive subscriptions for syntax highlighting and validation. Call
dispose() to clean up resources and prevent memory leaks.API Reference
Constructor
Methods
| Method | Return Type | Description |
|---|---|---|
getSql() | String | Get the SQL text |
setSql(String) | void | Set the SQL text |
getText() | String | Get the text content |
setText(String) | void | Set the text content |
clear() | void | Clear all text |
focusEditor() | void | Focus the editor |
selectAll() | void | Select all text |
scrollToTop() | void | Scroll to top |
formatSql() | void | Format the SQL |
toggleDarkMode() | void | Toggle dark/light theme |
copyToClipboard() | void | Copy text to clipboard |
dispose() | void | Clean up resources |
getCodeArea() | CodeArea | Get underlying CodeArea |
Properties
| Property | Type | Description |
|---|---|---|
readOnlyProperty() | BooleanProperty | Read-only mode |
darkModeProperty() | BooleanProperty | Dark theme enabled |
getSqlErrors() | ObservableList<SqlError> | Validation errors |
SqlError Record
Styling
The editor uses these CSS classes:.j-sql-editor- Main container.j-sql-code-area- Code area.j-sql-scroll- Scroll pane.j-sql-status-bar- Status bar.j-sql-status-label- Status labels.j-sql-status-ok- Success state.j-sql-status-error- Error state.j-sql-autocomplete-list- Autocomplete popup
Syntax Highlighting CSS
.sql-keyword- SQL keywords.sql-type- Data types.sql-function- Functions.sql-string- String literals.sql-number- Numeric literals.sql-comment- Comments.sql-operator- Operators.sql-punctuation- Punctuation.sql-error-line- Lines with errors
The editor includes built-in stylesheets for light and dark themes at
/css/sql-editor-light.css and /css/sql-editor-dark.css.