How to add attributed string to JTextPane

Hi all,
Could you tell me how to add an attributed string into a Textpane.
Textpane only takes a styledDocument. Is there any way to convert attributed string into a styled document.
Are there any components , which we can include attributed string , instead of simple string. Becoz i want my text to have variable fonts.

Hello,
check this short course about [url http://developer.java.sun.com/developer/onlineTraining/GUI/Swing2/shortcourse.html#JFCStyled]JTextPane and StyledDocument.
Heres a small example:import javax.swing.*;
import javax.swing.text.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
public class AttributedStringTest extends JFrame
     public static void main(String[] args)
          new AttributedStringTest();
     private JTextPane textPane = null;
     private StyledDocument doc = null;
     AttributedStringTest()
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          initMenuBar();
          initTextPane();
          setSize(500, 300);
          setLocationRelativeTo(null);
          setVisible(true);
     private void initMenuBar()
          JMenuBar mbar = new JMenuBar();
          JMenu menu = new JMenu("StyleConstants");
          JMenuItem item = new JMenuItem("Color");
          item.addActionListener(new ActionListener()
               public void actionPerformed(ActionEvent e)
                    Color color =
                         JColorChooser.showDialog(
                              AttributedStringTest.this,
                              "Color Chooser",
                              Color.cyan);
                    if (color != null)
                         MutableAttributeSet attr = new SimpleAttributeSet();
                         StyleConstants.setForeground(attr, color);
                         textPane.setCharacterAttributes(attr, false);
          menu.add(item);
          JMenu font = new JMenu("Font");
          font.add(item = new JMenuItem("12"));
          item.addActionListener(new StyledEditorKit.FontSizeAction("font-size-12", 12));
          font.add(item = new JMenuItem("24"));
          item.addActionListener(new StyledEditorKit.FontSizeAction("font-size-24", 24));
          font.add(item = new JMenuItem("36"));
          item.addActionListener(new StyledEditorKit.FontSizeAction("font-size-36", 36));
          font.addSeparator();
          font.add(item = new JMenuItem("Serif"));
          item.setFont(new Font("Serif", Font.PLAIN, 12));
          item.addActionListener(
               new StyledEditorKit.FontFamilyAction("font-family-Serif", "Serif"));
          font.add(item = new JMenuItem("SansSerif"));
          item.setFont(new Font("SansSerif", Font.PLAIN, 12));
          item.addActionListener(
               new StyledEditorKit.FontFamilyAction("font-family-SansSerif", "SansSerif"));
          font.add(item = new JMenuItem("Monospaced"));
          item.setFont(new Font("Monospaced", Font.PLAIN, 12));
          item.addActionListener(
               new StyledEditorKit.FontFamilyAction("font-family-Monospaced", "Monospaced"));
          font.addSeparator();
          menu.add(font);
          mbar.add(menu);
          setJMenuBar(mbar);
     private void initTextPane()
          doc = new DefaultStyledDocument();
          textPane = new JTextPane(doc);
          JScrollPane scroll = new JScrollPane(textPane);
          getContentPane().add(scroll);
          final JTextField text = new JTextField();
          text.setToolTipText("Please enter something");
          text.addActionListener(new ActionListener()
               public void actionPerformed(ActionEvent e)
                    try
                         doc.insertString(doc.getLength(), text.getText(), textPane.getInputAttributes());
                    } catch (BadLocationException bad)
          getContentPane().add(text, BorderLayout.SOUTH);
//not formatted version:import javax.swing.*;
import javax.swing.text.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
public class AttributedStringTest extends JFrame
     public static void main(String[] args)
          new AttributedStringTest();
     private JTextPane textPane = null;
     private StyledDocument doc = null;
     AttributedStringTest()
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          initMenuBar();
          initTextPane();
          setSize(500, 300);
          setLocationRelativeTo(null);
          setVisible(true);
     private void initMenuBar()
          JMenuBar mbar = new JMenuBar();
          JMenu menu = new JMenu("StyleConstants");
          JMenuItem item = new JMenuItem("Color");
          item.addActionListener(new ActionListener()
               public void actionPerformed(ActionEvent e)
                    Color color =
                         JColorChooser.showDialog(
                              AttributedStringTest.this,
                              "Color Chooser",
                              Color.cyan);
                    if (color != null)
                         MutableAttributeSet attr = new SimpleAttributeSet();
                         StyleConstants.setForeground(attr, color);
                         textPane.setCharacterAttributes(attr, false);
          menu.add(item);
          JMenu font = new JMenu("Font");
          font.add(item = new JMenuItem("12"));
          item.addActionListener(new StyledEditorKit.FontSizeAction("font-size-12", 12));
          font.add(item = new JMenuItem("24"));
          item.addActionListener(new StyledEditorKit.FontSizeAction("font-size-24", 24));
          font.add(item = new JMenuItem("36"));
          item.addActionListener(new StyledEditorKit.FontSizeAction("font-size-36", 36));
          font.addSeparator();
          font.add(item = new JMenuItem("Serif"));
          item.setFont(new Font("Serif", Font.PLAIN, 12));
          item.addActionListener(
               new StyledEditorKit.FontFamilyAction("font-family-Serif", "Serif"));
          font.add(item = new JMenuItem("SansSerif"));
          item.setFont(new Font("SansSerif", Font.PLAIN, 12));
          item.addActionListener(
               new StyledEditorKit.FontFamilyAction("font-family-SansSerif", "SansSerif"));
          font.add(item = new JMenuItem("Monospaced"));
          item.setFont(new Font("Monospaced", Font.PLAIN, 12));
          item.addActionListener(
               new StyledEditorKit.FontFamilyAction("font-family-Monospaced", "Monospaced"));
          font.addSeparator();
          menu.add(font);
          mbar.add(menu);
          setJMenuBar(mbar);
     private void initTextPane()
          doc = new DefaultStyledDocument();
          textPane = new JTextPane(doc);
          JScrollPane scroll = new JScrollPane(textPane);
          getContentPane().add(scroll);
          final JTextField text = new JTextField();
          text.setToolTipText("Please enter something");
          text.addActionListener(new ActionListener()
               public void actionPerformed(ActionEvent e)
                    try
                         doc.insertString(doc.getLength(), text.getText(), textPane.getInputAttributes());
                    } catch (BadLocationException bad)
          getContentPane().add(text, BorderLayout.SOUTH);
regards
Tim

Similar Messages

  • RE: how to add attributes in graphical modulator in segment builder .

    Hi Gurus,
    can you please help me how to add attributes in graphical modulator in segment builder in sap CRM 7.0 WEB-UI.
    thanks and regards
    satyanarayana
    Edited by: koradasatya on Oct 27, 2011 8:14 PM

    Hi Gurus,
    thanks for your reply my requirement from client is given below if you could answer this it will be a great help for me.
    WKSC needs to be able to make selections (in segment builder) with the possibilities to use stored data on organizations. The information we are missing in Segment builder (SB) (and Account search for industrycode) are Industrycode (we want to see values not codes) and responsible salesrep (values not codes).
    These variables together with regions and other marketing attributes is crucial for us. Without it we cannot use the functionality built.
    We also need it so that we can extract smaller parts of our customers and prospects and update them with information from the thirdparty or adding new persons to them.
    Everyone using the SB should be able to use the fields for selections. i.e. Marketers, marketing managers and power users.
    Example
    We would like to make selections that could look like this.
    Region: A and B
    Organization in Industrycode C
    Sales rep: D, E, and F
    Turnover: above 50000
    CP on selected orgs
    CP has title G or H
    CP has Email not blank
    thanks and regards
    satya

  • How to add Attributes to an asset? [URGENT]

    Hi,
    We are developing an application that need to add attributes to an asset depending on Product associated. Someone knows how to add the attributes automaticlly?
    FYI. We have not licensed the Configurator module.
    Please need an answer ASAP.
    Thanks

    Hi,
    We are developing an application that need to add attributes to an asset depending on Product associated. Someone knows how to add the attributes automaticlly?
    FYI. We have not licensed the Configurator module.
    Please need an answer ASAP.
    Thanks

  • How to add attribute to Element with SAX

    Hi,
    I'm parsing XML document with SAX using DefaultHandler.
    How can I add attribute to start tag?

    Is this right????????????Yes, it's right. Everything everybody except you has said in this thread has been right.

  • How to add attribute to Group in opensso

    I want to add a new attribute in opensso group as UsageControl. Presently while we create a group, we only get option to give id of the group. How to add an entry in that page? I want to give the value of Usagecontrol while creating a group, also I want that value to be present as an attribute.Please help.

    Hi,
    Can you please share the exact url for adding new fact group into OBIA load plan, altogether a new offering and  functional area.
    Regards,
    Priya

  • How to add attribute to already existing tag?

    Hello!
    Could you help me, please. I need to add attribute maxlegth to <h:inputTextarea>, but I don't know how to do it. Maybe, you know?

    <textarea> don't support maxlength by spec. You can use JavaScript instead.
    Or if you really want to create a custom component with this attribute added, check following tuts:
    http://www.jsftutorials.net/components/index.html
    http://www-128.ibm.com/developerworks/java/library/j-jsf4/
    http://java.sun.com/javaee/javaserverfaces/customcomponents.html

  • How to add attribute to Item ?

    Hi,
    I have a problem concerning OCS -> Content Service. I try to find out how scaleable the tool is. Do you know is it possible to add attribute to the Item? For example how to add some link to Document in repository?
    Best regards

    Content Services support adding attributes (we
    support Categories which are named collections of
    attributes) to documents.
    We also have support for document and folder links.so, there is no problem with adding an attribute to a document? Attribute could consist of a collection of links to any Document (Document, Folder etc.)?
    Regards

  • How to add attribute to characteristic already in production system

    HI Guru's ,
                     How to add a attribute to characteristic which already exist in production system.
    thanks in advance

    Hi Venkat,
    We don't make any design changes in production box. Usually you make changes in Development box then transport that object with request to Quality box. You test the changed object there, if no inconsistancies found you will transport the object to Production box with same request.
    In development box, if you want to add the new attribute to the characterstic, you need to delete the complete data in the characterstic.
    Thanks
    Sreekanth

  • How to add a string at the End of TLFTextField without loosing current text format!

    I have a TLFTextField that loads text from external text file.
    Then a function changes the format of some parts.
    Then as soon as I add a string at the end of my TLFTextField, All previous formatting is lost!
    I use this code to add string:
    myTLF.text += "." ;
    or
    myTLFText.text = myTLFText.text.concat(".");
    Should I use another method??

    pass the text through your formatting function again, or try appendText:
    myTLF.appendText(".") ;

  • How to add attributes in Cost Objects via Data Bridge?

    Hi everyone,
    I need your help. I want to know if is possible to add attributes via Data Bridge in Cost Objects tables like the country or address of a customer?
    Thanks for all.
    Sincerely,
    Pedro Sousa

    Hi,
    Jef is right you need to use the different values in the "Attribute Flag" field for creating attribute hierarchy using data bridge.
    For attribute Hierarchy you need to use option 12 & 15 depending on your requirement.
    12  - Attribute Assignment  - Used for assigning attribute to a member of the main hierarchy
    15  - Attribute Hierarchy only - used for creating hierarchy within attribute hierarchy.
    As mentioned by Jef for details you can refer the Data bridge user guide section 4.3.
    Regards,
    Niraj

  • How to add attributes to a DATS type characteristic

    I need to add some attiributes to a 0CALDAY type of characteristic.  I need to be able to do automatic time conversion in my transformations (e.g. from Cal Day to Cal Month or Cal Year).  I also need to have intelligent variable offsets in my queries too (e.g. the month of January-01 with a negative offset of 1 would be 12, not 00). 
    However I need to add some attributes to this Calendar Day characteristic such as "day of the week", "Week Number", etc...
    I need this as attributes and not as seperate InfoObjects such as 0CALDAY, 0CALMON, AND 0CALYEAR.  I need to be able to use the same date throughout my entire report, but then using the nav attributes, restrict based on what they selected in the variable screen.

    Hi Adam,
    It is not possible to define your own time characteristics or add attributes to any time characteristic.
    Time characteristics form the time reference frame for many data analyses and evaluations. They are delivered with Business Content.
    Examples:
    -->Timecharacteristic with the largest granularity: Calendar Day
    (0CALDAY)
    -->Time characteristic with the smallest granularity: Calendar Year
    (0CALYEAR) or Fiscal Year (0FISCYEAR).
    -Vikram

  • How to add attributes in the xml tag

    hi,
    i have two tables employee,department
    employee table
    empno number,
    empname varchar2(100),
    deptno number,
    department table
    deptno number,
    deptname varchar2(100)
    i want xml file in the above mentioned two tables.The xml file is as follows,
    <?xml version="1.0" encoding="UTF-8" ?>
    <EmployeeInfo>
    <Data type="employee">
    <empno>1</empno>
    <empname>siva</empname>
    <deptno>10</deptno>
    </data>
    <Data type="department">
    <deptno>10</empno>
    <deptname>IT</empname>
    </data>
    </EmployeeInfo>
    can anybody please give plsql code for this requirement.i need to add attributes in the xml tag.Its urgent
    By
    Siva

    A general example (based on the OE sample data/tables)
    SQL>SELECT XMLElement("Emp",
      2                     XMLAttributes(employee_id AS "id"
      3                                  , '10' "dept"),
      4                     first_name||' '||last_name) as result
      5 FROM   employees e
      6 WHERE  department_id = 10;
    RESULT
    <Emp id="200" dept="10">Jennifer Whalen</Emp>

  • How to add attribute to UME and how to retrive the attribute value

    Hi Gurus,
    I have developped an application that retriving the data from back end for that we need to give input values .
    I have got the Logged user from the Iuser But Sales Organization of User [from user profile] and Brand value [from user profile] I want these input put values need to pass as a input .
    For this how can get the sales org and Brand value.
    Give me suggestions for this or how to pass attributes to UME and how to capture it.
    Advance thanks.
    thanks
    Lohi.

    Check if there are any available function modules for this functionality. One of the FM that comes to my mind is:
    a) BAPI_USER_GET_DETAIL
    Check with your SD guys , if there exists any such function module.
    Regards,
    Subramanian V.

  • How to add attribute to groups.

    When I add a groups, Identity askme name of group and member for suscrib. I need add attribute to groups, attribute that not exits in groupsofuniquenames.
    Thanks

    Hi Venkat,
    We don't make any design changes in production box. Usually you make changes in Development box then transport that object with request to Quality box. You test the changed object there, if no inconsistancies found you will transport the object to Production box with same request.
    In development box, if you want to add the new attribute to the characterstic, you need to delete the complete data in the characterstic.
    Thanks
    Sreekanth

  • How to Add Horizontal scrollbar to JTextPane

    when i try to add Horizontal scrollbar to JTextPane it is not apperaing..plase help me..
    nagesh

    I tried in all ways
    1)HORIZONTAL_SCROLLBAR_ALWAYS and VERTICAL_SCROLLBAR_ALWAYS
    2)JTextPane textPane = new JTextPane();
    JScollPane scroller = new JScrollPane(textPane);
    or :
    3)JScollPane scroller = new JScrollPane(textPane, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
    4)JScrollPane.setHorizontalScrollBarPolicy(
                   ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                   JScrollPane.setVerticalScrollBarPolicy(
                   ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    and also we tried to overwrite the setbounds also, but nothing is diplaying..

Maybe you are looking for

  • Invisible boxes displayed when exporting from PowerPoint

    I am using Acrobat 10.1.4 with PowerPoint 2010 on Windows 7 Home Premium.  I have a presentation that contains some opaque white boxes that I remove through animation to make visible text underneath.  I structure my presentation this way because I ma

  • Adobe LiveCycle Designer and send XML

    Dear all, I have developped a PDF with Adobe LiveCycle Designer  that send an XML file to BW using a button Submit. But this PDF works when executed from the Adobe LiveCycle Designer software (tabpstrip PDF preview) but does not work when it is execu

  • How to Show Inline Message in Oracle ADF 11g Release 1.

    Can you please tell me how to show inline error Message on particular field.

  • How to check if the content in txt file is empty

    hi, i need my program to check if the content in txt file is empty. can anyone demonstrate an example with if-else statement ?

  • Serial number on multiple platforms

    So if I purchased an Adobe software that runs on Mac, and then I also installed a trial version of the software on Windows. If they are the same product, say photoshop CS5, does the same serial number applies (if it's intended for multiple users)? Re