Short Code Class

 //=========================


/*

 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template

 */

package mdi;


import javax.swing.JTextPane;

import javax.swing.JToggleButton;

import javax.swing.SwingUtilities;

import javax.swing.text.AttributeSet;

import javax.swing.text.Element;

import javax.swing.text.Style;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyledDocument;



public class StylesCollection {


public void Font(JTextPane textPane, String styleName, JToggleButton toggleButton) {

    StyledDocument doc = textPane.getStyledDocument();

    int start = textPane.getSelectionStart();

    int end = textPane.getSelectionEnd();


    if (start == end) return; // No text selected


    // Get the first character's style in the selected text

    Element element = doc.getCharacterElement(start);

    AttributeSet oldAttr = element.getAttributes();


    // Create a new style based on the existing one

    Style style = textPane.addStyle("TempStyle", null);

    style.addAttributes(oldAttr);


    // Logic to toggle based on current selected text style

    switch (styleName) {

        case "Bold":

            boolean isBold = StyleConstants.isBold(oldAttr);

            StyleConstants.setBold(style, !isBold); // Toggle

            toggleButton.setSelected(!isBold);      // Update button state

            break;

        case "Italic":

            boolean isItalic = StyleConstants.isItalic(oldAttr);

            StyleConstants.setItalic(style, !isItalic); // Toggle

            toggleButton.setSelected(!isItalic);        // Update button state

            break;

        case "Underline":

            boolean isUnderline = StyleConstants.isUnderline(oldAttr);

            StyleConstants.setUnderline(style, !isUnderline); // Toggle

            toggleButton.setSelected(!isUnderline);           // Update button state

            break;

    }


    doc.setCharacterAttributes(start, end - start, style, false);


    // Reapply selection and focus

    SwingUtilities.invokeLater(() -> {

        textPane.setSelectionStart(start);

        textPane.setSelectionEnd(end);

        textPane.requestFocusInWindow();

        textPane.getCaret().setSelectionVisible(true);

    });

}



public void UpdateToggleStatus(JTextPane textPane, String styleName, JToggleButton toggleButton){

    StyledDocument doc = textPane.getStyledDocument();

    int start = textPane.getSelectionStart();

    int end = textPane.getSelectionEnd();


    if (start == end) return; // No text selected


    // Get the first character's style in the selected text

    Element element = doc.getCharacterElement(start);

    AttributeSet oldAttr = element.getAttributes();


    // Create a new style based on the existing one

    Style style = textPane.addStyle("TempStyle", null);

    style.addAttributes(oldAttr);


    // Logic to toggle based on current selected text style

    switch (styleName) {

        case "Bold":

            boolean isBold = StyleConstants.isBold(oldAttr);

            toggleButton.setSelected(isBold);      // Update button state

            break;

        case "Italic":

            boolean isItalic = StyleConstants.isItalic(oldAttr);

            toggleButton.setSelected(isItalic);        // Update button state

            break;

        case "Underline":

            boolean isUnderline = StyleConstants.isUnderline(oldAttr);

            toggleButton.setSelected(isUnderline);           // Update button state

            break;

    }


   

}

    

}

//==========================Update Toggle Status==========
    private void textPaneCaretUpdate(javax.swing.event.CaretEvent evt) {                                     
        // TODO add your handling code here:
     stylesCollection.UpdateToggleStatus(textPane, "Bold", jToggleButton1);
     stylesCollection.UpdateToggleStatus(textPane, "Italic", jToggleButton2);
     stylesCollection.UpdateToggleStatus(textPane, "Underline", jToggleButton3);
     
    }                                    

//============================
  stylesCollection.Font(textPane, "Bold", jToggleButton1);

Comments

Popular posts from this blog

Remove (Clear) and remove Selected element

JSpinner

🛢️ MySQL Connection & CRUD Operations in Java