Increase / decrease font size inside textArea using JButton

I am creating a note taking application using Java.

What I want to do: I want to increase the size of the text inside textAreaeach time I click on the size of the increase. I will know how to do the opposite.

Short code:

        JButton incButton = new JButton("+");
        fontFrame.add(incButton);
        incButton.addActionListener(new fontIncAction());
        JButton DecButton = new JButton("-");
        fontFrame.add(DecButton);

        //textArea.setFont( Font("Serif", Font.PLAIN, fz));
    }
}

private class fontIncAction implements ActionListener{
    public void actionPerformed(ActionEvent e){

        textArea.setFont(new Font("Serif",Font.PLAIN,20));
    }
}
+3
source share
1 answer

To make the code more general, you can do something like the following in your ActionListener:

Font font = textArea.getFont();
float size = font.getSize() + 1.0f;
textArea.setFont( font.deriveFont(size) );
+8
source

All Articles