I am designing a web application testing tool with integrated webbrowser in Java, to implement the browser i used the HTMLEditorKit of JeditorPane which provides bare minimum functionality which is ok for me.
Now I wanna detect all the html elements such as buttons, lists, dropdownList, textfields etc. and record the behaviour of these elements when user interacts with them.(e.g. User types in textboxes of webpage)
I tried using actionListeners on the JeditorPane to detect events of adding components with no positive result.All the eventsListeners for JeditorPane will not work on the html elements such as textbox(webpage form) displayed in it.
Is there any way i can detect and listen to action of the html elememts(especially web form) rendered in JeditorPane which is dynamically loaded when i supply url.
To render such elements (let's name them controls) EditorKit uses ComponentView and extensions. On paint() ComponentView creates a container - inner class Invalidator. The Invalidator actually contains control from Element attributes.
So after paint() is done you can get list of children of your JEditorPane. I think the list will contains the Invalidators only. And ask each Invalidator about inner component. Thus you'll have list of actual controls created in the JEditorPane.
Then you can add any listeners depending on the component type to register any activity.
On each document change event (insert/delete/update) you should recheck changhes in the list after the next paint() and update your data accordingly.
Related
I´m building a simple word-editor in java. Currently, everything´s working fine. Now I want to create "Pages", like in word. The JTextPane representing a Page is supposed to check if it´s full and then create a new JTextpane under it. With a scrollbar I would be able to scroll between them. So far this wouldn't be problematic. However, all the pages should belong to a single document, and if I were to delete a line on let´s say page 2, every line on every other page will be moved up. (For example) Is there an easy way to do this, or will I have to create DocumentListeners for each JTextPane, changing everything on each change? Also, is there a way to extend selections over multiple pages?
Personally I havn't tried anything as of yet, since I want some tips before writing myself into a corner. I thought that I could make the pages uneditable, and instead use a caretListener to check the position the user clicks on, to edit an invisible infinite JTextPane containing the actual document, which would write it´s content to the visible pages.
Lots of unknowns, but maybe the following will give you something to think about.
all the pages should belong to a single document,
Agreed.
The JTextPane representing a Page is supposed to check if it´s full and then create a new JTextpane under it
Maybe add each text pane to a JScrollPane, but don't display the scrollbars or Border of the scroll pane.
if I were to delete a line on let´s say page 2, every line on every other page will be moved up
You would need to manually control the viewport of each scroll pane. The first page would position the viewport at offset 0. The next page would position the viewport at the offset that represents your page height.
Then any changes to the Document should be automatically reflected in all the text panes.
You would also probably need to use setAutoScrolls(false) to prevent the viewport from scrolling as you drag the mouse.
is there a way to extend selections over multiple pages?
Selection is a property of the text pane, not the Document.
Not sure what will happen as you try to drag the mouse from one text pane to another.
I'm guessing you might need some special logic. Maybe using mouseEntered/Exited events to trigger this type of processing.
I am using a JEditorPane as a component to show code. The JEditorPane resides in my custom PropertyEditorSupport for my Netbeans Platform app and is show in an OutlineView and the Properties window.
I've already limited the JEditorPane to be one line only, using a DocumentFilter. However I am not able to rebuild the funcionality a JTextField has, to finish input into the component by hitting the ENTER key.
I have already thought about adding a KeyListener event to my JEditorPane but am not sure what to do in case of the event. Is there a method to be called to "finish input of text"?
I am using a JEditorPane as a component to show code.
I've already limited the JEditorPane to be one line only
Those two statements seems to contradict one another. Usually codes consists of multiple lines, not one line.
In any case if you are simply display text then I would use a JTextPane as it supports text attributes. A JEditorPane is used to display HTML.
I have already thought about adding a KeyListener event
Don't use a KeyListener. Swing was designed to be used with Key Bindings. Basically you create an Action (in your case you would extend TextAction) and you bind the Action to a KeyStroke. Check out Key Bindings which contains more information as well as a link to the Swing tutorial on How to Use Key Bindings.
I am building my site for the first time with Wicket and I want to create a portal, where the username would be displayed on multiple places (user menu, header, maybe somewhere else on the page..). Wicket apparently doesn't like labels with the same wicket:id, because I get:
The component [Component id = userName] was rendered already. You can render it only once during a render phase.
Is there some other way to display the same dynamic text on different places without creating a new label for each and every one?
No, there isn't. Best you can do is to subclass a Label that will retrieve the username and use this label in multiple places with different wicket:id's. Like this:
public class UserNameLabel extends Label{
public UserNameLabel(String id) {
super(id, getTheUsernameSomewhereFrom());
}
}
Wicket builds the page to render as a hierarchy parallel to the Dom (HTML) tree. Everything inside Wicket is a component with its own id, mapping to an insertion point within the Dom tree. Rendering would not be deterministic, if components with the same id would be reusable (this would result in a math. graph obscuring the hierarchy and, as a graph, would result in cycles).
The simple answer is that you cannot reuse components.
I would not recommend to fiddle with the ids. Just use the OOP way and create a base component that adds/provides the labels.
I'm building a program in Java, using Swing, that will act as an interactive presentation.
I have paragraphs I need to display (presumably in JLabels) , and within each paragraph are certain words and phrases that need to be formatted differently (have a different color), and I need them to call a method that will display something else when clicked or hovered over.
I know there must be a way to accomplish this...
If you want to apply some style, you can use HTML in your JLabel's content.
If you need some custom behavior and you want to handle it in a different way for some parts of your JLabel, you need your component to be split in a more detailed way.
Create a container(JPanel) and arrange you components within it. From now, your smaller components will be able to listen for events like mouseEntered and mouseClicked and handle them separately, not confusing the whole JLabel component.
In this way, every smart part of text will be a standalone component.
Here's the sit:
I have a JSF component which is basically a list of 'documents'
I have any number of document viewer components on the same page.
None of these components "know" about each other. In other words, they cannot be configured at design time to link to each other or anything like that.
When the user clicks a document link I wish each one of the document viewer components to be notified.
Basically the idea would be to have the document viewers publish the fact that they listen for a certain type of event ("DocumentSelectedEvent" say) which the doc list component would fire.
I can think of ways of doing this that are not JSF specific, but I'm wondering if the JSF event model can handle that sort of thing.
Anyone have any ideas?
I don't think there's a way of doing that with the standard JSF event model.
Is there any way you can bind the components to a backing bean? That way when an event happens you can just go through your list of components and notify each of them.
You need to just bind the components with a backing bean and use a ValueChangeListener to notify the backing bean. The listener method could change the state of the other components which are tied to respective UI components.
Are you trying to do it in a "Ajax" way without page being explicitly submitted?
ValueChangeEvent
I do not know how you implemented your document list but if it were say a dropdown or any other multi item list component you can do an Value Change Event and force a submit on change for the component. Then in the page code backing bean you can call the methods for your viewers to load whatever you like.
In your jsf you just specify the value change handler you wrote in the backing bean.
/**
* Handle document click value change.
*
* #param valueChangedEvent the value changed event
*/
public void handleDocumentSelect(ValueChangeEvent valueChangedEvent) {
String selectedDocument = valueChangedEvent.getNewValue();
doDocViewer1DisplayMethod(selectedDocument);
doDocViewe2DisplayMethod(selectedDocument);
}
Modify your jsf tag to use your new value change event handler and force the submit.
<f:componentTag
attr=xxx
attr=xxx
valueChangeListener="#{pc_BackingBean.handleDocumentSelect}"
onChange=submit();>