简体   繁体   English

从JTextPane获取特定样式的所有文本

[英]Get all text of specific style from JTextPane

I have a JtextPane that has style1 , style2 etc. I want to retrieve all text that is in the textPane that has been inserted with style1 . 我有一个JstylePane,它具有style1style2等。我想检索已用style1插入的textPane中的所有文本。

Is there a way to do this? 有没有办法做到这一点?

EDIT What I'm trying to accomplish: 编辑我要完成的工作:

I have a client that receives different MessageTypes from the server. 我有一个从服务器接收不同MessageTypes的客户端。 I have a "debugging" window that logs each message to the JTextPane. 我有一个“调试”窗口,它将每个消息记录到JTextPane。 The window has a set of JRadioButtons corresponding to each type of message. 该窗口具有一组与每种消息类型相对应的JRadioButton。 The user can check off the buttons to see specific types of messages that were sent from the server. 用户可以选中按钮以查看从服务器发送的特定消息类型。

You can define your own attribute and place it with all the other ones. 您可以定义自己的属性,并将其与所有其他属性一起放置。 Just use SimpleAttributeSet and place there the new attribute (let's name it "MessageTypeAttribute") the value could be string constants for each required type. 只需使用SimpleAttributeSet并在其中放置新属性(我们将其命名为“ MessageTypeAttribute”),该值可以是每种必需类型的字符串常量。

To iterate the Document use getCharacterElement() method. 要迭代Document使用getCharacterElement()方法。 Start from 0 and then move to the end offset of the element to retrieve the next one. 从0开始,然后移到元素的结束偏移量以检索下一个。

Looking at the documentation , you can call getStyle( String stylename ) to get the style of a JTextPane. 查看文档 ,您可以调用getStyle(String stylename)以获取JTextPane的样式。

You may want to build a method like this to select the text you want 您可能需要构建这样的方法来选择所需的文本

public ArrayList<String> getAllOfOneStyle( String inStyle ){
   ArrayList<String> strAL = new ArrayList<String>(); 
   if( jtextpane_01.getStyle(inStyle) != null ){
      strAL.add( jtextpane_01.getText() );
   }

   // do this for each JTextPane

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM