Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I easily edit the style of the selected text in a JTextPane?

How do I easily edit the style of the selected text in a JTextPane? There doesn't seem to be many resources on this. Even if you can direct me to a good resource on this, I'd greatly appreciate it.

Also, how do I get the current style of the selected text? I tried styledDoc.getLogicalStyle(textPane.getSelectionStart()); but it doesn't seem to be working.

like image 756
Evan Fosmark Avatar asked Apr 19 '09 23:04

Evan Fosmark


3 Answers

Here's a code snippet to insert a formatted "Hello World!" string in a JEditorPane:

Document doc = yourEditorPane.getDocument();

StyleContext sc = new StyleContext();
Style style = sc.addStyle("yourStyle", null);

Font font = new Font("Arial", Font.BOLD, 18);

StyleConstants.setForeground(style, Color.RED);
StyleConstants.setFontFamily(style, font.getFamily());
StyleConstants.setBold(style, true);

doc.insertString(doc.getLength(), "Hello World!", style);
like image 80
JRL Avatar answered Sep 21 '22 05:09

JRL


Take a look at the following code in this pastebin:

http://pbin.oogly.co.uk/listings/viewlistingdetail/d6fe483a52c52aa951ca15762ed3d3

The example is from here:

http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample3.htm

It looks like you can change the style using the following in an action listener:

final Style boldStyle = sc.addStyle("MainStyle", defaultStyle);
StyleConstants.setBold(boldStyle, true);   

doc.setCharacterAttributes(0, 10, boldStyle, true);

It sets the style of the text between the given offset and length to a specific style.

See the full pastebin for more details. That should fix your problem though.

like image 2
Jon Avatar answered Sep 22 '22 05:09

Jon


The easiest way to manipulate text panels is using editor kits and their associated actions. You can find a demo of this in the JDK samples (under jdk\demo\jfc\Stylepad).

Sample code that installs a StyledEditorKit and uses a FontSizeAction to manipulate the text:

  public static void main(String[] args) {
    // create a rich text pane
    JTextPane textPane = new JTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane,
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    // install the editor kit
    StyledEditorKit editorKit = new StyledEditorKit();
    textPane.setEditorKit(editorKit);
    // build the menu
    JMenu fontMenu = new JMenu("Font Size");
    for (int i = 48; i >= 8; i -= 10) {
      JMenuItem menuItem = new JMenuItem("" + i);
      // add an action
      menuItem
          .addActionListener(new StyledEditorKit.FontSizeAction(
              "myaction-" + i, i));
      fontMenu.add(menuItem);
    }
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fontMenu);
    // show in a frame
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 400);
    frame.setJMenuBar(menuBar);
    frame.setContentPane(scrollPane);
    frame.setVisible(true);
  }

(Tip: if you want to use a FontFamilyAction, have a look at GraphicsEnvironment.getAvailableFontFamilyNames() and logical font family names.)

like image 2
McDowell Avatar answered Sep 22 '22 05:09

McDowell