0

In my text editor, I give the option to change text through a JComboBox and a StyledEditorKit. The only problem is that once I select a size in the combo box (and it changes the selected text), and then select a new selection of text, the value in the combo box stays the same as the previous selection, regardless of the actual size.

I saw this post, but I don't really understand the given answer.

Here's the code I use to change the font

JComboBox<?> fontSizeCombo = new JComboBox<Object>(sizes.toArray());
fontSizeCombo.setFocusable(false);
fontSizeCombo.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          new StyledEditorKit.FontSizeAction("myaction-", (int) ((JComboBox<?>) e.getSource()).getSelectedItem()).actionPerformed(e);
      }
});

"sizes" just contains ints ranging from 8 to 112 by increments of 4.

Any help is greatly appreciated.

Community
  • 1
  • 1
Peter Mauldin
  • 55
  • 1
  • 6

2 Answers2

3

The mentioned post means you should add a CaretListener to your editor. When sselection is changed caret position updates and the listener is invoked. Then InputAttributes of the kit is updated. You can access the attributes and get font size for current caret position (selection).

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Okay, when I do that it returns a string that says "size = 24" or whatever the size is. I can't use that because it'll set the ComboBox value to "size = 24", not 24. How can I get around that? – Peter Mauldin Sep 04 '12 at 03:25
1

Add caret listener to your text pane that invokes update for the combo box.

lebryant
  • 351
  • 2
  • 18