Changing a font size in Java, using a textbox

okay, so i need a little helping hand, i'm using a set of varibles to declare my fonts etc, i have made the bold and italics function work by using the deriveFont method - creating a new font and applying the new style etc.

This one is more tricky because im getting the value out of the text box and applying it to the fontSize variable and then applying that to the currentFont i have there...

HELP?! :(

A snippet of my code is below...

public class Practice01 extends javax.swing.JFrame {

private int fontStyle;
private int fontSize = 12;

private void changeSize(){

    Font currentFont = edtText.getFont(); //getting the current font
    fontSize = Integer.parseInt(txtSize.getText()); //getting the number input from the text box and putting it to the fontSize variable
    txtSize.setText(Integer.toString(fontSize)); //setting the txtSize entry to the fontSize variable?
    currentFont.deriveFont(fontSize); //deriving a new font
    edtText.setFont(currentFont.deriveFont(fontSize)); //setting the new font and size to the text box.


and here's my font variable bits...

Font serifFont;
Font monoFont;
Font sansserifFont;

public Practice01() {
    serifFont = new Font ("Serif", fontStyle, fontSize);
    monoFont = new Font ("Monospaced", fontStyle, fontSize);
    sansserifFont = new Font ("SansSerif", fontStyle, fontSize);


Beware, Font#derive(int) changes the font style, not it's size, you could try using Font#deriveFont(float) which is used to change the size of the font...

Also, deriveFont creates a new instance of the Font based on the values you provide, so you will need to maintain a reference to it, for example...

Font font = currentFont.deriveFont((float)fontSize); //deriving a new font

Update with example

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FontTest {

    public static void main(String[] args) {
        new FontTest();

    public FontTest() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

                JFrame frame = new JFrame("Testing");
                frame.add(new TestPane());

    public class TestPane extends JPanel {

        private JTextField field;

        public TestPane() {
            setLayout(new GridBagLayout());
            field = new JTextField(5);
            field.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String text = field.getText();
                    try {
                        float size = Float.parseFloat(text);
                        Font font = field.getFont();
                        font = font.deriveFont(size);
                    } catch (Exception exp) {



Need Your Help

Employing a proxy in Remoting Concept

c# remoting

I am a newbie in remoting concepts(C# Remoting).Actually i done some projects using

how to set toggle button text value from db on jtable

java swing jtable tablecelleditor jtogglebutton

how to set toggle button text value (depend on database) on jtable