Web Browser in Java FX - java

I am just a beginner and i wanted to create a web browser in java using swing. now here are the three things that i m not able to do:
Dont know how to load a web page in a frame. here is my code for that:
AddressField.getText();
try {
URI uri=new URI(AddressField.getText());
URL url=uri.toURL();
InputStream in=url.openStream();
} catch (URISyntaxException ex) {
Logger.getLogger(MyBrowser.class.getName()).log(Level.SEVERE, null, ex);
} catch (MalformedURLException ex) {
Logger.getLogger(MyBrowser.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MyBrowser.class.getName()).log(Level.SEVERE, null, ex);
}
I want to put a backward button to get back to previous page. I tried to do that but i doesnt worked well. i need a good code to get back button in function. here is the code for its action listener:
ActionListener ab = new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
int i= store.size();
loadURL(store.get(i-2).toString());//store is object of ArrayList
}
};
I also want to put an option to open a new tab and also show record of history of pages visited.
hoping for positive responses. every help will be appreciated. thank you

a) From this example it looks like you can just get the webEngine from the WebView instance and load the URL.
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
// that should do it...
webEngine.load(url.toExternalForm());
b) There seems to be history support built-in.
You'll just need to navigate between items...
c) As stated previously, Web History support seems to be built-in. For tabs, you'll likely need to have a TabPane (each tab with its one WebView component).

Related

Create Java Desktop Notification

The requirement is to create a desktop notification which can register a click-event. I cannot use web-sockets or any browser notifications.
I am unable to use Tray-Icons and SystemTray because they cannot register Click-Events on DISPLAY MESSAGE. They can have click-events on the trayicon but not on the display message. The closest example - "When we register a click on a Skype message, it opens Skype for us"
Screenshot
On clicking the above Notification Skype chat opens-up. The same functionality is not supported with Tray-Icons. Either a work around it or a new approach will be do.
Hope I am clear thanks.
I used the following repository from github DorkBox.
Simply add maven dependency as instructed on the github link. However, I was unable to check how to change the UI for the notifications.
Notify.create()
.title(text)
.text(title)
.position(Pos.TOP_RIGHT)
.onAction( new ActionHandler<Notify>() {
#Override
public void handle(Notify value) {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
try {
Desktop.getDesktop().browse(new URI(targetUrl));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
})
.hideAfter(5000)
.shake(250, 5)
.darkStyle() // There are two default themes darkStyle() and default.
.showConfirm(); // You can use warnings and error as well.
Add the following code in your main block and you are good to go.

GIF from external resource in page is not animating in apache wicket web application

I am using external images in my webaplication, everything wass fine until I wanted to add animated gif there, the gif loads, but it doesn't animate.
Java code:
File sourceimage = new File("loading_img.gif");
try {
final BufferedDynamicImageResource r = new BufferedDynamicImageResource("GIF");
r.setImage(ImageIO.read(sourceimage));
add(new Image("gif", r));
} catch (Exception e) {
e.printStackTrace();
}
HTML:
<img wicket:id="gif"/>
EDIT:
Tried martin-g suggestion, gif still doesn't animate
try {
final BufferedDynamicImageResource r = new BufferedDynamicImageResource("GIF"){
#Override
protected void setResponseHeaders(AbstractResource.ResourceResponse data,
IResource.Attributes attributes){
super.setResponseHeaders(data, attributes);
data.setContentType("image/gif");
}
};
r.setImage(ImageIO.read(sourceimage));
add(new Image("gif", r));
} catch (Exception e) {
e.printStackTrace();
}
The problem is that the content type is not automatically set.
You will need to override org.apache.wicket.request.resource.AbstractResource#setResponseHeaders() and set with via resourceResponse.setContentType(String).
Maybe this should be done automatically by Wicket in org.apache.wicket.request.resource.DynamicImageResource, since it knows the format ("png", or "gif" as in your case). Please file a ticket in Wicket's JIRA for this improvement! Thanks!

change color of link when clicked in Java

Okay, I know the title of the post is very familiar. I saw a lot of posts on this subject, but non of them covered the code in Java. I have this HyperlinkListener class :
private void editorpaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
// TODO add your handling code here:
if (evt.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED) {
try {
try {
java.awt.Desktop.getDesktop().browse(evt.getURL().toURI());
} catch (IOException ex) {
Logger.getLogger(MyAssistantGUI.class.getName()).log(Level.SEVERE, null, ex);
}
catch (URISyntaxException ex) {
Logger.getLogger(MyAssistantGUI.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Open browser: " + evt.getURL());
I tried to add Style-sheet to my JEditorPane and then add a:active {color: #f00;}, but it does not work. Then i saw in the API that java does not support a:active/hoover..etc.
Can you give me suggestions, on how to change the color when the user clicks the link?
In the listener you can get position in the text. Then cast your JEditorPane's Document to StyledDocument and use getCharacterElement() passing the offset you have. For the Element you can change attributes e.g background or foreground using setCharacterAttributes() method.

showDocument() does not display new window in IE8 with Java 7/Java 6u27

I have a Java Applet that interacts with the Java Plugin to show a document (just a URL) in a named browser window:
public class TestApplet extends Applet {
#Override
public void init() {
super.init();
final JButton showButton = new JButton("Show Google!");
showButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
getAppletContext().showDocument(new URL("http://google.com"), "Some Window Title");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
});
add(showButton);
}
}
This has worked historically but starting with Java 7 and Java 6u27, the window fails to open in Internet Explorer (tested in IE 8). If I use _blank as the window title (target) instead of Google, the window opens correctly (albeit in a new window each time).
I've tracked down this bug that was fixed for 6u27:
Vista/IE7 further showDocument focus issue with named targeted windows
Has anybody else experienced the same behaviour? Have you found a workaround (other than using "_blank")?
Edit
Updated the example. I wasn't actually using "Google" as the target, I was using "Some Window Title" (sorry!). It seems like this problem is unique to targets with spaces in the name.
It seems like this problem is unique to targets with spaces in the name.
Two possible solutions:
Replace the " " with "%20"
Don't use a space in the name of the target! (Though I thought that would be a 'no brainer'.)
Try this code, it should work.
Desktop desktop = Desktop.getDesktop();
desktop.browse(new URI(info));

J2me, how to create a link in the screen?

I am developing in J2ME, I need to show a text, and then an underlined link / button that people can press.
I cannot use Canvas.
As a temporal solution, I am using the typical command button but I would like to show this option on screen.
(I don't want to use any framework that implies to change everything so that it has a particular look, only an underlined link)
I found it, uff!!!
Command prospectoCommand = new Command("Prospecto", Command.EXIT, 1);
StringItem messageItem2 = new StringItem("", "", Item.HYPERLINK);
messageItem2.setText("push to go to the URL");
ItemCommandListener listener = new ItemCommandListener() {
public void commandAction(Command cmnd, Item item) {
if(cmnd==prospectoCommand)
{
try {
midlet.platformRequest(URL);
} catch (ConnectionNotFoundException ex) {
ex.printStackTrace();
}
}
}
};
messageItem2.setDefaultCommand(prospectoCommand);

Categories