简体   繁体   中英

ChangeListener doesn't change font in JTextArea

I'm using Java(ver1.42) awt for making chatting program

I made bold & Italic JCheckBox to change font in JTextArea .

Here is the listener for 2 CheckBoxes.

class CheckBoxListener implements ChangeListener {
        public void stateChanged(ChangeEvent ce) {
            String fontName = inputTextArea.getFont().getFontName();
            int fontSize = inputTextArea.getFont().getSize();
            int fontStyle = 0;

            if(boldCheckBox.isSelected())
                fontStyle += Font.BOLD;

            if(italicCheckBox.isSelected())
                fontStyle += Font.ITALIC;

            inputTextArea.setFont(new Font(fontName, fontStyle, fontSize));
        }           
    }
}

Everything works well If I check "boldCheckBox", font in inputTextArea changes into BOLD.
If I check "italicCheckBox", font in inputTextArea changes into ITALIC.
AND
If I un-check "italicCheckBox", font changes into normal form.

HOWEVER

font never comes back even though I un-checked "boldCheckBox"

could you find what's wrong?

First, indeed you have to use the bitwise | operator to get Bold and Italic together in the same font, not the + operator.

It could also be the case that the system, once you have switched to a bold font, is using a related font that includes the bold attribute. For example, in some operating systems, You have "Arial" and "ArialBD". Since you create your new font based on the name of the old font rather than using deriveFont , it may be that it stays "ArialBD".

So try this:

class CheckBoxListener implements ChangeListener {
        public void stateChanged(ChangeEvent ce) {

            int fontStyle = Font.PLAIN;

            if(boldCheckBox.isSelected())
                fontStyle |= Font.BOLD;

            if(italicCheckBox.isSelected())
                fontStyle |= Font.ITALIC;

            inputTextArea.setFont(inputTextArea.getFont().deriveFont(fontStyle));
        }           
    }
}

The problem was with the initialization of fontName every time inside the listener. Move the fontName initialization code outside the listener just once.

Try below code:

    final String fontName = inputTextArea.getFont().getFontName();
    final int fontSize = inputTextArea.getFont().getSize();
    class CheckBoxListener implements ChangeListener {
        public void stateChanged(ChangeEvent ce) {
            int fontStyle = 0;
            if (boldCheckBox.isSelected() && italicCheckBox.isSelected())
                fontStyle = Font.BOLD | Font.ITALIC;
            else if (boldCheckBox.isSelected())
                fontStyle = Font.BOLD;
            else if (italicCheckBox.isSelected())
                fontStyle = Font.ITALIC;
            else
                fontStyle = Font.PLAIN;
            inputTextArea.setFont(new Font(fontName, fontStyle, fontSize));
        }
    }

Thanks guys, but I found answer.

The reason was FontName

if I turn it into BOLD style

FontName is changed into "dialog.bold" from "dialog"

so, even though I remove Bold style, font remains as Bold style because FontName is "dialog.bold"

here is my answer

class CheckBoxListener implements ChangeListener {
        public void stateChanged(ChangeEvent ce) {
            int fontSize = inputTextArea.getFont().getSize();
            int fontStyle = 0;

            if(boldCheckBox.isSelected())
                fontStyle += Font.BOLD;

            if(italicCheckBox.isSelected())
                fontStyle += Font.ITALIC;

            inputTextArea.setFont(new Font("dialog", fontStyle, fontSize));
        }           
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM