views:

780

answers:

5

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.

A: 

I'd recommend taking a look at Sun's Java Tutorial about editor panes.

htw
+1  A: 

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.

Jon
I just visited the java2s link and wow, lots of examples.
extraneon
A: 

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);
JRL
I don't know why this was down-voted because it is the most correct answer here!
Steve McLeod
A: 

Ok, wow. Hard question. So I have not found a way to get the style of a given character. You can, however, get the MutableAttributeSet for a given character and then test to see if the style is in that attribute set.

   Style s; //your style
   Element run = styledDocument.getCharacterElement( 
       textPane.getSelectionStart() );
   MutableAttributeSet curAttr =
       ( MutableAttributeSet )run.getAttributes();
   boolean containsIt = curAttr.containsAttributes( s );

One problem with getting the Style for a range of characters is that there may be more than one style applied to that range (example: you may select text where some is bold and some is not).

To update the selected text you can:

  Style s; //your style
  JTextPane textPane; //your textpane
  textPane.setCharacterAttributes( s, false );

Oh, and it appears that the function getLogicalStyle doesn't work because it's returning the default style (or maybe just the style) for the paragraph that contains p, rather than the the style of the character at p.

Joseph Gordon
+1  A: 

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.)

McDowell