0

I have been programming this small app that the user can select one of the a font colors, blue , red and black, so i used JradioButton for them. also the user can choose a style to the font whether bold or italic or both at the same time, so i used Jcheckbox. the problem is that i tried to let the user choose both italic and bold but nothing changed, is there a way to do that?

private void jRadioBlackActionPerformed(java.awt.event.ActionEvent evt) {                                            
        if (jRadioBlack.isSelected()) {
            jTextField1.setForeground(Color.black);
        }
        if (jCheckBoxBold.isSelected() && jCheckBoxItalic.isSelected()) {
            jTextField1.setFont(new Font("Times New Roman", Font.BOLD + Font.ITALIC, 14));
        }
    }                                           

    private void jRadioRedActionPerformed(java.awt.event.ActionEvent evt) {                                          
        if (jRadioRed.isSelected())
            jTextField1.setForeground(Color.red);
    }                                         

    private void jRadioBlueActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if (jRadioBlue.isSelected())
            jTextField1.setForeground(Color.blue);
    }                                          

    private void jCheckBoxBoldItemStateChanged(java.awt.event.ItemEvent evt) {                                               
        if (jCheckBoxBold.isSelected()) {
            jTextField1.setFont(new Font("Times New Roman", Font.BOLD, 14));
        } else {
            jTextField1.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        }
    }                                              

    private void jCheckBoxItalicStateChanged(javax.swing.event.ChangeEvent evt) {                                             

    }                                            

    private void jCheckBoxItalicItemStateChanged(java.awt.event.ItemEvent evt) {                                                 
        if (jCheckBoxItalic.isSelected()) {
            jTextField1.setFont(new Font("Times New Roman", Font.ITALIC, 14));
        } else {
            jTextField1.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        }
    }                                                

1 Answers1

1

Example:

import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import javax.swing.*;

public class Test {

    public void run() {
        final JTextField textField = new JTextField(20);
        
        JCheckBox bold = new JCheckBox("Bold");
        JCheckBox italic = new JCheckBox("Italic");
        JPanel stylePanel = new JPanel();
        stylePanel.add(bold);
        stylePanel.add(italic);
        
        bold.addItemListener(e -> changeStyle(textField, Font.BOLD, e.getStateChange()));
        italic.addItemListener(e -> changeStyle(textField, Font.ITALIC, e.getStateChange()));

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(textField, BorderLayout.NORTH);
        frame.add(stylePanel, BorderLayout.SOUTH);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
    
    private void changeStyle(JTextField textField, int style, int stateChange) {
        if (stateChange == ItemEvent.DESELECTED) {
            removeStyle(textField, style);
        } else {
            addStyle(textField, style);
        }
    }
    
    private void addStyle(JTextField textField, int style) {
        Font current = textField.getFont();
        int newStyle = current.getStyle() | style;        
        textField.setFont(current.deriveFont(newStyle));
    }
    
    private void removeStyle(JTextField textField, int style) {
        Font current = textField.getFont();
        int newStyle = current.getStyle() & ~style;        
        textField.setFont(current.deriveFont(newStyle));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Test().run());
    }
}
Mihe
  • 2,270
  • 2
  • 4
  • 14