本文整理汇总了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);
}
}
}