Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8454289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:59:12+00:00 2026-06-10T11:59:12+00:00

Every time I change the font, it goes back to the default size, which

  • 0

Every time I change the font, it goes back to the default size, which is 12, even if I change it before with the “Tamano” menu, it only goes back to 12 every time, my guess would be the way I change the size with deriveFont(), but don’t I now any other way to change it.

public static class cambiar extends JFrame {

    public cambiar() {
        final Font aryal = new Font("Comic Sans MS", Font.PLAIN, 12);
        JFrame ventana = new JFrame("Cambios en el Texto!");
        JPanel adentro = new JPanel();
        final JLabel texto = new JLabel("Texto a Cambiar!");
        texto.setFont(aryal);
        JMenuBar menu = new JMenuBar();

        JMenu fuentes = new JMenu("Fuentes");
        /* Elementos de Fuentes */
        JMenuItem arial = new JMenuItem("Arial");
        arial.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Font arrrial = new Font("Arial", Font.PLAIN, 12);
                float tam = (float) texto.getFont().getSize();
                String hola = String.valueOf(tam);
                texto.setFont(arrrial);
                texto.setFont(texto.getFont().deriveFont(tam));
            }
        });



        fuentes.add(arial);
        /* FIN Fuentes */


        JMenu tamano = new JMenu("Tamano");

        /* Elementos de Tamano */
        JMenuItem font13 = new JMenuItem("13");
        font13.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                texto.setFont(texto.getFont().deriveFont(23.0f));
            }
        });

        JMenuItem font14 = new JMenuItem("14");
        arial.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                texto.setFont(aryal);
            }
        });

        JMenuItem font15 = new JMenuItem("15");
        arial.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                texto.setFont(aryal);
            }
        });

        JMenuItem font16 = new JMenuItem("16");
        arial.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                texto.setFont(aryal);
            }
        });

        JMenuItem font17 = new JMenuItem("17");
        arial.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                texto.setFont(aryal);
            }
        });

        JMenuItem font18 = new JMenuItem("18");
        arial.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                texto.setFont(aryal);
            }
        });

        JMenuItem font19 = new JMenuItem("19");
        arial.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                texto.setFont(aryal);
            }
        });

        JMenuItem font20 = new JMenuItem("20");
        arial.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                texto.setFont(aryal);
            }
        });

        tamano.add(font13);
        /* FIN tanano */

        JMenu tipo = new JMenu("Tipo");

        /* Elementos de tipo */

        /* FIN tipo */


        /* Elementos del JMENU */
        menu.add(fuentes);
        menu.add(tamano);
        menu.add(tipo);
        /* FIN JMENU */

        /* Elementos del JPanel */
        adentro.add(menu);
        adentro.add(texto);
        /* FIN JPanel */

        /* Elementos del JFRAME */
        ventana.add(adentro);
        ventana.setVisible(true);
        ventana.setSize(250, 250);
        /* FIN JFRAME */        
    }
}

Thanks in Advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:59:14+00:00Added an answer on June 10, 2026 at 11:59 am

    First off, you really just need one action listener:

    private class MenuListener implements ActionListener {
        @Override public void actionPerformed(ActionEvent e) {
            Object caller = e.getSource();
            if (caller != null && caller.instanceOf JMenuItem) {
                JMenuItem src = (JMenuItem)caller;
                String size = src.getText();
    
                if (size != null) {
                    float fontSize = Float.parseFloat(size);
                    texto.setFont(aryal.deriveFont(fontSize));
                }
            }
        }
    }
    

    Then when you create your JMenuItems:

    MenuListener listener = new MenuListener();
    JMenuItem font18 = new JMenuItem("18");
    font18.setActionListener(listener);
    

    The deriveFont method returns the font set to the specified size, but it doesn’t actually change the font itself. Therefore you need to call setFont with the newly sized font.

    In general to change the font size:

    Font font; // some font you already have instantiated
    float size = 20f; // the target font size
    Font newFont = font.deriveFont(size); // the newly sized font
    

    EDIT

    In response to the question, the above MenuListener code either needs to go in its own class called MenuListener.java (but you have to make it public) or you’d put it in the class cambiar:

    public class cambiar extends JFrame { 
        ... your existing code here ...
    
        private class MenuListener implements ActionListener {
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every time I change a .coffee file in the development mode, Rails creates a
In my activity there is a toggle button. I want every time you change
Publishing my entire application to a server in the U.S. every time I change
I think beestings change the html every time. this means html is not able
I'd like to change the font in textview which is in dialog: dialog =
I need to change a text field every time values in an array is
I am now to Django and i find that every time i change my
Every time I change a package name or move some source code folder, I
HI!!!!! every time I change the name of a control there is this dialog
I'm using a web service and every time I change something on the dataset,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.