creating hyperlinks in jlist in java - java

I have search this alot and found little guidance. Can someone guide me towards the best path to creating workable hyperlinks in a Jlist in a java swing application?

Here is a no frills way of doing it.
jList1.addListSelectionListener(this);
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
String url = (String)jList1.getSelectedValue();
//Assume browser is supported on Desktop API
Desktop.getDesktop().browse(new URI(url));
}
I'm using the default model here and the value displayed is the URL. Runs on JDK 1.6

Related

Opening an URL from default browser crashes the application

I have a GUI application written in JavaFX 14 (not the XML one). I currently have three lists that are connected each to a different custom class, in whom exists an ObservableList which updates the ListViews. I have three. Upon doubleclicking on an element of the first ListView, which also contains URLs, I want to open said URL inside the default browser, no matter the OS (I'm programming on Ubuntu 20.04, and this app will be used on W10 too).
listSquads.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(event.getClickCount()==2) {
try {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI(bot.getSquadHandler().extractURL(listSquads.getSelectionModel().getSelectedItem())));
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
});
This is the code. The bot is a custom TwitchBot object, based on pIRCbot libraries. The squadHandler mentioned in the getter method is said custom class. Extract URL is a method that, as the name would suggest, extracts the exact URL given a string as input - this part works, as it's called upon in other parts of the code and works correctly, and I've also tested it.
However, upon double clicking on any given element of the ViewList, the app freezes to then crash a few seconds later. Why could that be? Is there a way to open the browser no matter the OS?
Thanks in advance! :)

Java (Netbeans) : Connecting Google Search To JTextField

Is there any way to connect google search to jtextfield, i want to create a software that can search directly through a jtextfield and the results would show up on a different JPanel. i found a code that will make it search on google directly but you need an existing browser to do it, i just want the results to appear on a different panel, for example when you search something on the textfield the results will popup on another panel.
this is the code that i found but it needs an existing browser i want the software to be standalone thats all thank you:
try {
String search = "#q="+jTextField.getText().toString().trim();
search = search.replaceAll(" ","+");
String url = "http:////www.google.com//"+search;
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
If you're not interested in writing your own browser you could use this.

remote FileChooser using OtrosVFSBrowser

I'm trying to do a remote filechooser. I read a lot of information but I don't get it.
The best site I've seen is (https://code.google.com/p/otrosvfsbrowser/) because is exactly what I want but I don't fine more information. Are Someone of you using this tool? where are any example code?
I already use JSCH library to transfer via SFTP, but I want a tool to choose the remote path.
I'm starting in Java. sorry for my English.
You can check example code on Usage page
You can just create dialog and register action on approve:
JOtrosVfsBrowserDialog jOtrosVfsBrowserDialog = new JOtrosVfsBrowserDialog();
Action a = new AbstractAction("Select file") {
#Override
public void actionPerformed(ActionEvent actionEvent) {
if (JOtrosVfsBrowserDialog.ReturnValue.Approve.equals(jOtrosVfsBrowserDialog.showOpenDialog(TestDialog.this,"title"))){
String friendlyUrl = jOtrosVfsBrowserDialog.getSelectedFile().getName().getFriendlyURI();
}
}
};

Open Editor at Start in an EclipseRCP application

I'm currently programming on an eclipse RCP application in Java for an university project.
My problem is that I want an editor loaded at application start, but I don't know which method is the right one to start with. In the perspective I can only add views and set my editor space, but I can't set any editors.
I tried overwrite the WorkbenchWindowAdvisor.postWindowOpen() method, but this only got me an exception...
You say you got an exception.. what was it? How did you overwrite postWindowOpen(), can you post your code? I could help you more if I knew these things.
Anyway, the following code opens the editor at application startup:
#Override
public void postWindowOpen() {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
page.openEditor(editorInput, editorId);
} catch (PartInitException e) {
// Handle the exception here
}
}
where "editorInput" is the input of your editor and "editorId" it's ID.
Also, I highly recommend reading Lars Vogel's tutorial on editors:
http://www.vogella.de/articles/EclipseEditors/article.html

JEditorPane can't take Google search queries, why?

I am creating a very basic web browser using JEditorPane just to teach myself Swing and GUIs in Java but am having trouble implementing a Firefox-like Google Search bar.
I'm not if it's due to a limitation of JEditorPane or my lack of understanding but if I try and take the string typed into the "Google Search" bar and use the setPage() method of JEditorPane, it doesn't work.
Here is my code for the ActionListener of the "Google Search" button:
public void actionPerformed(ActionEvent arg0)
{
try
{
content.setPage("http://www.google.com/search?q=" + searchBar.getText());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(frame, "Error searching for: " + searchBar.getText());
}
}
Even when I try and just do content.setPage("http://www.google.com/search?p=test"); it doesnt work, so is it something to do with setPage()'s way of taking the URL string? As in it doesn't like the "?" or "=" characters or is there another way of doing it all together?
Thanks for your time,
InfinitiFizz
Add something to print the exception you are catching and you'll see that you're receiving a 403 Forbidden from Google.
There are a lot of Java bots out there and sites have started blocking requests with "java" in the User-agent field. Google will let you get their home page, but won't let you search unless you override the User-agent field.
Start your jvm with -Dhttp.agent=myappname/1.0 where myappname is the name of your application.
JEditorPane is a poor choice to implement even the simplest browser. It works to display simple HTML pages but it stops there.
Try The Flying Saucer Project, it works pretty well(it's not a full browser, but close enough).They have an example which simulates actually a web browser.
Like adrian.tarau said, JEditorPane is very poor at displaying modern web pages.
It doesn't even support HTML 4 or Javascript. I believe Google uses Javascript to make the Search button work.
Another suggestion would be to use the Lobo Browser/Cobra engine.
If you need a full browser in Java check out Lobo:
http://lobobrowser.org/java-browser.jsp

Categories