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;
}
}
}
Comments
Post a Comment