2

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.

balanv
  • 10,686
  • 27
  • 91
  • 137

1 Answers1

3

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.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Cool.. actually i tried to set and use Tooltip.. get/set name is good option though.. – balanv Mar 21 '13 at 13:01
  • 7
    Also consider `putClientProperty()` & `getClientProperty()` in `JComponent`. – trashgod Mar 21 '13 at 16:14
  • @trashgod: I never noticed those methods. How do you pass the keys around so the listener knows which property to get? – Gilbert Le Blanc Mar 21 '13 at 16:29
  • @GilbertLeBlanc: That's a perennial problem; some are [buried in the source](http://stackoverflow.com/q/377924/230513); some are [vendor-specific](http://developer.apple.com/library/mac/#technotes/tn2007/tn2196.html); for local use, a package-access constant may do. – trashgod Mar 21 '13 at 16:35