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

落爺英雄遲暮 提交于 2019-12-19 07:45:07

问题


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.


回答1:


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.




回答2:


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



回答3:


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




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/766396/how-do-i-easily-edit-the-style-of-the-selected-text-in-a-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!