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

Answers


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
edtText.setFont(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() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField field;

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

    }

}

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