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

  • SEARCH
  • Home
  • 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 8488863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:40:03+00:00 2026-06-10T21:40:03+00:00

I have a JToolBar and a JTextPane . On the toolbar I have buttons

  • 0

I have a JToolBar and a JTextPane. On the toolbar I have buttons for bolding, underlining, etc. I tried to add a button that, when pressed, would increase the size of the text.

This code appears at the start of my ToolBar class, and is set equal to an int from my Display class where it has a default value of 24. It’s used to set the original font size.

static int size = Display.size;

This code is in my ToolBar() Constructor.

final JButton reduceButton = new JButton(new ImageIcon("res/reduce.png"));
reduceButton.setToolTipText("Reduce Text...");
reduceButton.setFocusable(false);
reduceButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        size -= 4;
        System.out.println("FontSize = " + size);
    }
});
reduceButton.addActionListener(new StyledEditorKit.FontSizeAction("myaction-", size));

For some reason the button doesn’t work, however if I change the code to:

reduceButton.addActionListener(new StyledEditorKit.FontSizeAction("myaction-", 40));

..then it works. Any idea why this is?

  • 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-10T21:40:05+00:00Added an answer on June 10, 2026 at 9:40 pm

    The problem is that the size is fixed by the second invocation of addActionListener – whatever the value of size when that code is initially run is what it will remain.

    If you need to dynamically change the font size, as you do, then you’ll need to do it within your earlier action listener. Try something like

    reduceButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            size -= 4;
            System.out.println("FontSize = " + size);
            // Font has changed, instantiate a new FontSizeAction and execute it immediately.
            new StyledEditorKit.FontSizeAction("myaction-", size).actionPerformed(arg0);
        }
    });
    

    This is a bit strange to create a new action object just to invoke an action; I would probably rewrite this just to modify the font on the editor object directly.

    As an aside, it’s generally a bad idea to have static, mutable variables like this.

    It looks like you can override the font size you specify in constructor via the actionCommand string in the actionEvent; see http://opensourcejavaphp.net/java/harmony/javax/swing/text/StyledEditorKit.java.html

    public void actionPerformed(final ActionEvent event) {
            Object newValue = null;
            if (event != null) {
                try {
                    newValue = new Integer(event.getActionCommand());
                } catch (NumberFormatException e) {
                }
            }
            performAction(event, StyleConstants.FontSize, null, defaultValue,
                          newValue, false);
        }
    

    But what I posted in first place should work. If it doesn’t let me know what the problem is and I’ll take another look.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a JToolBar that have all the controls like add, edit,
Here is the issue I am having. I have a JToolBar that contains 3
I'm using a JToolBar to have a toolbar on my application, but I don't
Good day I have a basic toolbar to which I added ImageIcon buttons. The
I have a bunch of buttons in my JToolBar, and I set some of
Have any one tried to activate fancybox thumbnail gallery using a button or an
I have a horizontal JToolbar with JToggleButtons . For some reason it is placed
Have a look at one of my websites: moskah.com The problem is that it
Have a simple contact us XPage created. Have server side validation in place that
Have a network location that shows paths in the 8.3 short format. I need

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.