本文整理匯總了Java中javax.swing.JPopupMenu.getComponent方法的典型用法代碼示例。如果您正苦於以下問題:Java JPopupMenu.getComponent方法的具體用法?Java JPopupMenu.getComponent怎麽用?Java JPopupMenu.getComponent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.JPopupMenu
的用法示例。
在下文中一共展示了JPopupMenu.getComponent方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getPopupMenuItem
import javax.swing.JPopupMenu; //導入方法依賴的package包/類
/**
* Returns the index of an item in a popup menu.
*
* @param menu the menu.
* @param text the label.
*
* @return The item index.
*/
private int getPopupMenuItem(JPopupMenu menu, String text) {
int index = -1;
for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
Component comp = menu.getComponent(i);
if (comp instanceof JMenuItem) {
JMenuItem item = (JMenuItem) comp;
if (text.equals(item.getText())) {
index = i;
}
}
}
return index;
}
示例2: actionPerformed
import javax.swing.JPopupMenu; //導入方法依賴的package包/類
@Override
public void actionPerformed(ActionEvent evt){
if(annotationEditor == null) return;
//this action either creates a new annotation or starts editing an
//existing one. In either case we need first to make sure that the current
//annotation is finished editing.
if(!annotationEditor.editingFinished()) return;
if(textLocation == -1) return;
JPopupMenu popup = new JPopupMenu();
//check for selection hovering
if(textPane.getSelectedText() != null
&& textPane.getSelectionStart() <= textLocation
&& textPane.getSelectionEnd() >= textLocation){
//add 'New annotation' to the popup menu
popup.add(new NewAnnotationAction(textPane.getSelectedText()));
popup.addSeparator();
}
//check for annotations at location
for(SetHandler setHandler : setHandlers) {
for(Annotation ann : setHandler.set.get(
Math.max(0l, textLocation-1),
Math.min(document.getContent().size(), textLocation+1))) {
if(setHandler.getTypeHandler(ann.getType()).isSelected()) {
AnnotationDataImpl annotAtPoint =
new AnnotationDataImpl(setHandler.set, ann);
//add annotations to edit to the popup menu
popup.add(new HighlightMenuItem(
new EditAnnotationAction(annotAtPoint),
annotAtPoint.getAnnotation().getStartNode().getOffset().intValue(),
annotAtPoint.getAnnotation().getEndNode().getOffset().intValue(),
popup));
}
}
}
if (popup.getComponentCount() == 0) {
// nothing to do
} else if(popup.getComponentCount() == 1
|| (popup.getComponentCount() == 2
&& popup.getComponent(1) instanceof JSeparator)) {
//only one annotation, start the editing directly
//or only one selection, add new annotation
((JMenuItem)popup.getComponent(0)).getAction().actionPerformed(evt);
} else { //mouse hover a selection AND annotation(s)
try{
Rectangle rect = textPane.modelToView(textLocation);
//display the popup
popup.show(textPane, rect.x + 10, rect.y);
}catch(BadLocationException ble){
throw new GateRuntimeException(ble);
}
}
}