fungho
asked on
How to add pop-up menu for JEditorPane?
In Swing, I know that I can use JEditorPane to display a html file. At the same time, some words in the file are allowed to be right-clicked, so that a pop-up menu is displayed for the user to choose.
Now, I decide to add some custom tag to the html file to acheive the above result, so that I can use ElementIterator to get the tag. However, I don't know how to add listener to the words in the html file so that the menu can be displayed.
Moreover, I don't know my above idea is correct or not.
I also want to ask whether there is web site or sample code of this example?
Stephen
Now, I decide to add some custom tag to the html file to acheive the above result, so that I can use ElementIterator to get the tag. However, I don't know how to add listener to the words in the html file so that the menu can be displayed.
Moreover, I don't know my above idea is correct or not.
I also want to ask whether there is web site or sample code of this example?
Stephen
ASKER
Thanks! I do not have time to look at these 2 classes, I will look at them tomorrow. Moreover, I want to know that if I add listener to JEditorPane, could I add different listener to different words?
Thanks!
Thanks!
Hi,
I believe you can't add listener to different words. You need to locate word basing on click position, then show appropriate menu. Check out JTextComponent.viewToModel (). StyledDocument.getParagrap hElement, StyledDocument.getCharacte rElement() might also help.
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
I believe you can't add listener to different words. You need to locate word basing on click position, then show appropriate menu. Check out JTextComponent.viewToModel
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
ASKER
Is it useful if I use HTMLEditorKit?
Maybe, if you can turn your sensitive words into hyperlinks. HTMLEditorKit.LinkControll er class looks promising. Maybe you will need to subclass event delivered to HyperlinkListener. Source digging is necessary to provide more details. I will try to do that tomorrow.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks! It works!
I want to ask whether I can add scroll bar to the textpane?
I want to ask whether I can add scroll bar to the textpane?
ASKER
I met a serious problem when I try to add the listener to the pop up menu. It said that Popup.LinkController cannot use addMouseListener or addPopupMenuListener... how to solve this?
Then how can I add listener to different element in the pop up menu?
Then how can I add listener to different element in the pop up menu?
> I want to ask whether I can add scroll bar to the textpane?
Just put it into JScrollPane.
BTW, what java version did you use? My code has rather big painting problem on 1.3.1/Win98.
Regards,
Igor Bazarny.
P.S. BTW, would you mind awarding points?
Just put it into JScrollPane.
BTW, what java version did you use? My code has rather big painting problem on 1.3.1/Win98.
Regards,
Igor Bazarny.
P.S. BTW, would you mind awarding points?
Well, in place where you create menu you can add listeners to items
JPopupMenu popup = new JPopupMenu();
JMenuItem item = new JMenuItem("Item 1");
item.addActionListener(new ActionListener(){
public void actionPerformed(ActionEven t event){
System.out.println("Popup menu item selected!!!");
}
});
popup.add(item);
popup.add("Item 2");
popup.show(reason.getCompo nent(),rea son.getX() ,reason.ge tY());
JPopupMenu popup = new JPopupMenu();
JMenuItem item = new JMenuItem("Item 1");
item.addActionListener(new
public void actionPerformed(ActionEven
System.out.println("Popup menu item selected!!!");
}
});
popup.add(item);
popup.add("Item 2");
popup.show(reason.getCompo
ASKER
bazarny, don't worry, i must give it to you! thanks for ur answer. I will try to test it later and ensure it is ok! Thanks!
ASKER
Thanks for the code and the answer! I still have some questions in fact, hope you can help me later... I increase the point to 200 to thanks you!
Stephen
Stephen
I am unable to understand how will u be able to add listener to text(listeners can only be added to components),so i think u will be adding listener to JEditorPane.
also have a look at class :
java.awt Class MenuComponent.AccessibleAW
and class :
javax.accessibility.Access
hope this will give u some help if not all
Kamal