Font Color Change

 

//===========================Step1===

   int color_start,color_end;

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        // TODO add your handling code here:

           

             color_start = textPane1.getSelectionStart();

             color_end = textPane1.getSelectionEnd();

        FontColorDailog.setVisible(true);

    }                                        



//==========OnChange Listiner  initComponant()




    jColorChooser3.getSelectionModel().addChangeListener(e -> {
     StyledDocument doc = textPane1.getStyledDocument();
        // Create and configure style
Style style = textPane1.addStyle("UnderlineColorStyle", null);
//Color c=jColorChooser2.getColor();
//StyleConstants.setUnderline(style, true);
StyleConstants.setForeground(style,jColorChooser3.getColor()); // Change to any color you want

// Apply style
doc.setCharacterAttributes(color_start, color_end - color_start, style, false);

// Reselect and maintain highlight
SwingUtilities.invokeLater(() -> {
    textPane1.setSelectionStart(color_start);
    textPane1.setSelectionEnd(color_end);
    textPane1.requestFocusInWindow();
    textPane1.getCaret().setSelectionVisible(true);
});
        
    });

Comments

Popular posts from this blog

Remove (Clear) and remove Selected element

JSpinner

🛢️ MySQL Connection & CRUD Operations in Java