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?

Peter Mauldin
  • 55
  • 1
  • 6

1 Answers1

2

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.

I82Much
  • 26,901
  • 13
  • 88
  • 119
  • Actually, now another problem arose. It's not a big enough deal to make another post, so let me ask here: if I select a line of text, and reduce it to size 8, and then select another line of text and reduce it, instead of size starting back at whatever size the selected text is, it'll start at 8. I know it's because size is a global variable, but I can't think of a way around that. – Peter Mauldin Sep 03 '12 at 07:15
  • This should be a different question about how to aggregate `StyledEditorKit` actions; the actions apply to the selection, not globally. See also this [example](http://stackoverflow.com/a/8534162/230513). – trashgod Sep 03 '12 at 15:55
  • This is another reason this global size is a bad idea. But yeah open another question – I82Much Sep 03 '12 at 16:56