Sobiaholic
Sobiaholic

Reputation: 2967

Increasing/Decreasing Font Size inside textArea using JButton

I'm creating a sticky note application using Java.

What I want to do: I want to increase the size of the texts inside textArea each time I click on the increase size. I will know how to do the opposite obviously.

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));
    }
}

Upvotes: 4

Views: 19841

Answers (1)

camickr
camickr

Reputation: 324207

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) );

Upvotes: 15

Related Questions