dcsimg
To make a copy/paste action as general as possible...
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Chantal_Ackermann
Posted On:   Monday, November 6, 2000 09:04 AM

To make a copy/paste action as general as possible in an application - what would be the most effective way to get hold of the current selection without knowing which component owns the focus/the selected area?

Re: To make a copy/paste action as general as possible...

Posted By:   Sandip_Chitale  
Posted On:   Saturday, March 24, 2001 08:57 AM

Yes. That is how all the Actions work in the JTextComponent. The methods are in javax.swing.text.TextAction. Here is the code snippet -


/**
* Determines the component to use for the action.
* This if fetched from the source of the ActionEvent
* if it's not null and can be narrowed. Otherwise,
* the last focused component is used.
*
* @param e the ActionEvent
* @return the component
*/
protected final JTextComponent getTextComponent(ActionEvent e) {
if (e != null) {
Object o = e.getSource();
if (o instanceof JTextComponent) {
return (JTextComponent) o;
}
}
return getFocusedComponent();
}

/**
* Fetches the text component that currently has focus.
* This allows actions to be shared across text components
* which is useful for key-bindings where a large set of
* actions are defined, but generally used the same way
* across many different components.
*
* @return the component
*/
protected final JTextComponent getFocusedComponent() {
return JTextComponent.getFocusedComponent();
}

About | Sitemap | Contact