Java (Netbeans) : Connecting Google Search To JTextField - java

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.

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 append an image into JTextArea

TLDR: Insert an image in a JTextArea that will be generated afterwards.
I'm new on using Java, doing my first real project. I am trying to make an application that creates documents based on input variables that represent the keywords in a "template". Basically I created on the left of the GUI a few JTextFields that I print out in the JTextArea with append and then I generate the document.
What I am trying to do atm is to insert an image in that append, image that will be in the generated document.
The code so far for what I specified below.
JTextArea append, where I would like to insert the image:
private void ADDActionPerformed(java.awt.event.ActionEvent evt) {
txtReceipt.append(
"\t\t CONTRACT DETAILS \n\n" +
"LEASE NUMBER:\t\t\t" + txtContract.getText() +
"insert text template here"
);
}
Between the lines of text I would like to add the image. The txtContract variable is a JTextField and the txtReceipt is the JTextArea.
Generate button:
private void GENERATEActionPerformed(java.awt.event.ActionEvent evt) {
try {
txtReceipt.print();
} catch (PrinterException ex) {
Logger.getLogger(GeneratorContracte.class.getName()).log(Level.SEVERE, null, ex);
}
}
I found another threads on this subject but they were adding images in an already created document or generating a doc. The problem is that I didn't figure out what line of the codes does print the image. Also, I don't know if JTextArea supports images.
I am using NetBeans if that matters.
Sorry for the wall of text, but I tried to specify the issue without posting the whole code. I believe this way makes it easier to find a solution.
Thanks for your time guys!

Calling Java method from HTML link

I'm currently building a Twitter client in Java using the Twitter4J API. To create a Twitter "timeline", I am currently pulling data from Twitter such as profile images, tweets and usernames, then displaying them in a JTextPane, formatted using HTML. Code example below:
StringBuilder out = new StringBuilder();
try {
List<Status> statuses = HandleEvents.instance().twitter.getHomeTimeline();
out.append("<html>");
for (Status status : statuses)
{
out.append("<img src=\"").append(status.getUser().getProfileImageURL())
.append("\" width=30 height=30><b>").append(status.getUser().getName())
.append(":</b> ").append(status.getText())
.append("<br><br>");
}
out.append("</html>");
tweetsTextPane.setText(out.toString());
This displays a timeline of 20 tweets, separated by two line breaks. Under each tweet, I would like to place a simple hyperlink, called "Retweet", which calls one of my Java methods - HandleEvents.instance().twitter.retweetStatus(status.getId())
How would I got about doing this? Can the call be made directly between the tags, or do I have to make the call using JavaScript?
Any help would be appreciated. Many thanks.
You don't really need to have a hyperlink do you? Since it's a Swing app you could just add a JLabel that only looks like a hyperlink (but if you put in a little effort, it could behave like one as well). Add a listener for mouse clicks on that JLabel and you've can hook your current handler there.
On the other hand, if you do want actual HTML links, what you can do is implement your own HyperlinkListener.
Here are a couple of examples:
http://www.java2s.com/Tutorial/Java/0240__Swing/HyperlinkListenerExample.htm
http://www.java2s.com/Code/Java/Event/DemonstratingtheHyperlinkListener.htm
http://www.devx.com/tips/Tip/12997

creating hyperlinks in jlist in 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

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