Java Desktop app to pop up websites captcha - java

Hoping someone can point me in the right direction.
I'm quite new to Java and wanting to build a small app to practice etc.
Anyway I want to fill out a form on a site and grab the data. I have had a look at HtmlUnit and HtmlComponents and think I have most of the design covered.
Something I can't figure out is the site has a single captcha and I want my desktop app to popup a window where I would enter the captcha (manually, I'm not creating a bot).
Is this possible in Java?

It's possible and in fact JDownloader is doing it. It's open source, so you can take a look to its code.

As mentioned before JDownloader does this technique quite well. You mentioned the source is rather difficult to understand, so I'll explain the basics of what one would do.
First you would want to create your code that will fill in the form, once the form hits the captcha have it open up a frame that has a spot for an image (which will be the captcha image), a textfield, and a submit button. The submit button should have an action that will send the text from the textfield to the captcha text field in the form.
Summary:
1: Create you class that will fill in the form
2: Once it gets to the capthca make it open your 'captcha solving frame'
3: Fill in the text field and create an actionPerformed on the button that will send your string to the textfield in the form
4: Throw in some sort of catch in case you improperly fill in the captcha
On a side note, this would be easier and considerably less verbose if done with Groovy (or a scripting language) rather than Java.

Related

Prompt user to select a radio button

I'm currently making a Java Applet program that heavily depends on the radio buttons and a text field for it to compute the needed information. I want to know if there is a way to remind the user that he or she has forgotten to select a radio button like how catching does to an empty textfield.
Thanks in advance.
P.s. I am still a senior high student. Our professor and curriculum requires us to study Java Applet before moving on to a more advanced type of programming.
Java Applet actually defines a Server-side code, which coincidently creates an HTML page that will run on the user's browser. But it's important to remember that none of your Java code will run in the browser.
This means that when a client presses submit on your page, all the information will be sent to the server side (as is). At this point you can check for example, if specific value was not set, and present a page with error message to the client. In conclusion: cumbersome, and not pretty.
A much better solution, and what is usually done is: Create a Javascript function that will test if a radio button is selected, if not, it will show an error message (without leaving the page) and will prevent the submit code from running until it was fixed.
Here's an example of how it's done: Check if input radio ID is selected before form is submitted

Calling a Java method using javascript from within a JPanel

I have a Java application which displays results on a JPanel. The results are displayed using HTML by using a JLabel.
Now I want to let the user interact with the local application by allowing local methods to be called when the user clicks on a link in that HTML document.
Is this possible?
To answer you question, then, it is possible, however you cannot use a JLabel, you need to insert a JavaFX component, and then you can set your class as a window variable on the DOM, and thus your methods can be called from JavaScript.
Have a look at this answer on this question. It looks like they are doing exactly what you want.

JOptionPane vs. JDialog

This is a crosspost to the thread in Javaranch (includes some images): http://www.coderanch.com/t/567472/GUI/java/Optimal-solution-creating-multiple-dialog
I'm trying to develop a simple swing desktop application where I imagine alot of different dialog's jumping around to fetch user input. Would need to present labels, textfields, passwordfields, combobxes, checkboxes etc in various dialog windows.
For example: creating the database firsthand, creating the first admin account, adding users, changing user accounts etc.
I have an understanding that JOptionPane is used to create simple quick & easy modal dialog's. I would really like to know why one would choose one over another in this case. Which one is more preferable to use: JOptionPane vs. JDialog
Also I could use some pointers how one should appropriately design and implement this.
Thank you.
Here's a statement I found on the Java website that says one key point about the difference between the two.
How to make Dialogs
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.
So it sounds like you would use JOptionPane if you want a user to have to make a choice and close the box before returning to the main screen. If you use a JDialog box, then they can just click around it and get back to the main screen without making a choice. For example, say you wanted to make a user choose the number of results before clicking submit, you wouldn't want them to be able to click around that window and click submit. You would use JOptionPane to force them to select a value first before going back to submit.
Check out http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html it pretty much has everything you would need.
As i understand it, JOptionPane is great for what it can do, but you can't really change the functionality beyond that (not easily). JDialog is better to inherit from if you want to create your own custom Dialogs.

How to copy text from a browser with Java?

Is there a way to copy text from a browser to my Java app ?
For example, at the left side of my screen I open a browser to point to a URL and shows the content of that page, it might be in a frame or CSS or simple html, on the right side of the screen I open a Java Swing application. I'm interested in certain parts of the browser window that shows some text, and I want my Java app [ without me doing anything ] to copy and paste the text into itself, can it be done ?
I know I can use JEditorPane or JTextPane and set it with an HTMLEditorKit, then load the text into the pane, but if the page uses Frames or some other complex ways, the text I get from the Pane is not what I see on the page, so I don't want to do it by loading the URL into my Java app, instead, I wonder if it can be done in the way I mentioned above ?
I think you're looking at the problem from the wrong angle. If what you want is to harvest a website, I suggest you have a look at the awesome library web-harvest. With a little Xpath wizardry you can get everything you want.
Doing what you describe would imply inter-process communication that seems like an overkill. There are more ways to download a web-page content than the browser.
You may try the following depending on your needs.
With java.awt.Robot you can either 1) Take an screenshot if what you neeed is the content ( without the text, just an image of the browser content ) or 2) Move your self into the browser and programatically press: CTRL-A + CTRL-C and return back to your swing app focus and programatically press: CTRL-V ( or CMD or whatever makes sense in your OS )
But again, this might or not work, depending on what you need.
I know a tool but i am not sure it meet your needs. Have you heard about selenium? http://seleniumhq.org/ It can replicate actions taken by the user in a browser and then manipulate them ussing java code. Have a look at the link it may be handy.
Using java.awt.Robot & a TextField will get the job done, not sure if there is any other way. Have robot press ctrl+a then ctrl+c, bring TextField into focus, and finally have robot press ctrl+v. Now from here you can create a button.setOnAction to save the TextField text into a string. Or you can use a change listener on the TextField setOnKeyReleased to do the same.

OfficeBean won't display after moving containing Swing Panel from one Container to another

I'm developing a Java 6 applet which allows users to view OO (v.3.2) documents (read only), and if they choose, click a button which launches a new JDialog window, with the document displayed in it which allows the user to and mark and redact it as they wish. Once they are done, they can close the JDialog, which saves the document to a server and redisplay the updated document (read-only again) in the original applet window
I guessed that I could do this with a single instance of an OfficeBean, embedded in a Swing Panel. However, I cannot seem to successfully move my Panel (containing the OfficeBean) from the applet to the JDialog when the "Redact" button is clicked. All I get is a blank area in the JDialog where the document should be. I get no errors.
I have currently managed to get round this by creating new instances of the OfficeBean every time I need to display the document (once when the applet is loaded, again when the user chooses to redact and it is opened in a JDialog, and finally when they click "Save" in the dialog and the redacted result is displayed in the applet again.) However this means three trips to and from the server where the documents originate. That seems mad to me.
I'm in no way a Swing expert and may well be making a silly mistake. However, I've done a lot of fiddling around, debugging and googling and can't seem to get this to work. Can anyone help me in this? Am I trying to do something which is fundamentally impossible? I hope not.
One rule in Swing is that a component can only be displayed / attached to one part of the gui "tree" at a time. When you "move" your component to the dialog, are you first removing it from the applet?

Categories