Search code examples
javaswing

Setting custom property for Swing Components (JEditorPane)


I am working in a Twitter feed application using Java Swing.

is it possible for me to set custom attribute to the JEditorPane as like below

JEditorPane jep = new JEditorPane();
jep.tweetID = "222";
jep.tweetText = "Good Day...";

so that i can get these attributes directly in events associated with this JEditorPane like below

        public void mouseReleased(MouseEvent e) 
        {
                   String currentTweetID = e.getSource().tweetID;
         }

if this is possible, please suggest the solution.


Solution

  • Every AWT Component, which Swing components are built on, has a setName method and a getName method. You can name your component with any String, like your tweetID. If you need other identification strings, you can concatenate them together and use the setName method to pass them to your action methods.

    You can get the text of your JEditorPane with the getText method.