1

How can I set the font size of the selected text in a JEditorPane (pane) Using a JComboBox?

previously i used:

toolbar.add(new StyledEditorKit.FontSizeAction("12", 12));

But you cant simply have hundreds of buttons.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Primm
  • 1,347
  • 4
  • 19
  • 32
  • Well...I figured out how to get an int from the JComboBox...Any way to set the font of the selected text using an INT? – Primm Jun 30 '12 at 15:07
  • Fount a good demo: http://stackoverflow.com/questions/939109/increasing-the-font-size-of-a-jtextpane-that-displays-html-text – Primm Jun 30 '12 at 16:51

1 Answers1

3

I don't know the canonical way to do this, but on experimentation, this works:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Action;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;

public class EditorPaneFun extends JPanel {
   private static final Integer[] ITEMS = { 9, 10, 11, 12, 14, 16, 18, 20, 24,
         32 };
   private JEditorPane editorPane = new JEditorPane();
   private JComboBox<Integer> fontBox = new JComboBox<Integer>(ITEMS);
   private StyledDocument doc = new DefaultStyledDocument();
   private StyledEditorKit styledEditorKit = new StyledEditorKit();

   public EditorPaneFun() {
      editorPane.setDocument(doc);
      editorPane.setEditorKit(styledEditorKit);
      JScrollPane scrollpane = new JScrollPane(editorPane);
      scrollpane.setPreferredSize(new Dimension(500, 400));
      JPanel comboPanel = new JPanel();
      comboPanel.add(fontBox);

      setLayout(new BorderLayout());
      add(scrollpane, BorderLayout.CENTER);
      add(comboPanel, BorderLayout.SOUTH);

      Document doc = editorPane.getDocument();
      for (int i = 0; i < 20; i++) {
         int offset = doc.getLength();
         String str = "This is line number: " + i + "\n";
         try {
            doc.insertString(offset, str, null);
         } catch (BadLocationException e) {
            e.printStackTrace();
         }
      }

      fontBox.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            int size = (Integer) fontBox.getSelectedItem();
            Action fontAction = new StyledEditorKit.FontSizeAction(String
                  .valueOf(size), size);
            fontAction.actionPerformed(e);
         }
      });
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("EditorPaneFun");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new EditorPaneFun());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Note that this only works if the Document is a DefaultStyledDocument and if the EditorKit is a StyledEditorKit.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • No, this is not good. Better to use `setCharacterAttributes(...)`... hang on. – Hovercraft Full Of Eels Jun 30 '12 at 15:47
  • The problem with this is the fact you cant select text and do it, so the font size is not changed... – Primm Jun 30 '12 at 16:44
  • 1
    Found an answer HERE: http://stackoverflow.com/questions/939109/increasing-the-font-size-of-a-jtextpane-that-displays-html-text – Primm Jun 30 '12 at 16:52
  • 1
    @user1332495: Setting attributes isn't _wrong_, but `Action` is more flexible. If an instance already exists, you can use it in multiple contexts or _forward_ the event, as shown [here](http://stackoverflow.com/a/10568672/230513). – trashgod Jun 30 '12 at 18:09
  • Ah thanks! I was using a different method. However, my PC crashed and i lost everything, so I took another look at this. This actually works better/simpler. Thanks! – Primm Jun 30 '12 at 19:45