I have a Java applet that presents a JButton that allows users to navigate to another URL (which has a feedback form).
Problem: when I navigate to the form in Safari from the applet, typing into the form is garbled or impossible (only every 10th keystroke or so is actually entered).
Manually terminating the Java Web Plug-in for Safari fixes this immediately. I assume that I am somehow not releasing resources properly in my applet. I am aware that cleanup should be performed in the stop() method, but I'm not sure what resources I am failing to release that could cause this kind of behavior.
Here is the code that browses to the URL:
final JButton btnLaunch = new JButton("Go to survey");
final myJApplet mj = this;
btnLaunch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String url ="www.mywebform.example";
try {
getAppletContext().showDocument(new URL(url));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
finally {
SwingUtilities.getWindowAncestor(btnLaunch).dispose();
mj.stop();
}
}
My best guess is that somehow Safari is mishandling resources by passing them to some Java ActionListener. However, including:
btnLaunch.removeActionListener(this);
in a finally block does not seem to help. I have other action listeners in my applet that I know are not explicitly destroyed, but I was under the assumption that generally this wasn't a problem as they are supposed to be disposed of automatically.
EDIT: Checking through my code, it seems I only have MouseListener or ActionListeners (on JButtons). So it is even more baffling that I'm having a keyboard issue. I tried manually removing them all in the stop method of the applet (which I manually call in the finally block), but it didn't help. MouseEvents and ActionEvents shouldn't continue to run upon navigating away from the page!
EDIT 2: Further information: having the applet open a new window which then contains a link to the final survey also does not work. However, making the link open in a new window (using html's target="_blank") seems to correct the problem. So for a work around, I have the applet direct users to a splash page which thanks them and then presents a link for the final survey. This is annoyingly kludgey, and doesn't answer the original question, but for now it seems like what I will be going with.
Related
I am trying to set the default close operation in NetBeans 8.0.2 (in Ubuntu 14.04 on an older Asus gaming laptop.) My program is very large but uses no JFrame or java.swing components.
I merely need to save some values when the "x" in the lower right corner is clicked (this is one usual way to stop execution of a java program in NetBeans.)
I found suggestions that involved swing & JFrame, but it wasn't clear just where to insert the code:
DefaultApplicationView view = new DefaultApplicationView(this);
javax.swing.JFrame frame = view.getFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.out.println("CLOSING");
}
}
show(view);
I also found a set of instructions that I think I would prefer to use, but the post is old enough that my NetBeans doesn't have the tabs/menu-items referred to:
Set Window to Design Mode by clicking the 'Design' Tab
In the Navigator: Right click the 'JFrame' -> 'Properties'
In the Properties Tab: Set 'defaultCloseOperation' (top of the list) to 'DO_NOTHING'
Select 'Events' Tab
Scroll down to 'windowClosing'
Click on the "..." button on the right of the event to bring up the custom editor
Click 'Add...' and name the handler (i.e. custom function that you want to have execute on click of the 'X', or window close event).
Click 'Ok'
Netbeans now automatically creates the function and takes to you the function body in the source view
Now simply add what you want to do here: eg. dispose(), or system.exit or pintln(), or whatever your heart desires, as long as its JAVA and makes sense to the app.
Then there are a few other possibly relevant posts, but they all explicitly involve JFrame and/or swing. (Am I ignorant of some fact such as "All NetBeans java applications use JFrame", or some such?)
A pared down example of code for what I'm trying to do would be:
public class MyApp{
public static void main(String[] args){
loadMyVariables();
// do some work that changes variables' values
// during this work user clicks the 'x' box to halt execution
// I need then automatically to save the variables' new values
}
// needs to be called by the OS or GUI when execution is halted by user
public static void saveMyVariables{
// here the usual printStream stuff saves some values to a file
System.exit(0);
}
public static void loadMyVariables{
// here the usual Scanner stuff reads some values from a file
}
}
(I need help setting the tags for this, so I'm doing as instructed and asking the community.)
THANKS
I´ve already searched for related posts on stack, but didn´t find quite the right answer;
Im using THIS to display a .pdf file in my frame.
Now I want to use the JWebBrowser.navigate()+ (filePath + "#search=anyString") to search this .pdf file for the specific string.
Unfortunately I´m unable to reload the JWebBrowser afterwards. So the correct filepath is submitted (checked that in the pdf adress bar), but the JWebBrowser turns gray and nothing happens.
When using the navigate() to load another file and afterwards navigate to the old file again, it works just fine.
I tried revalidate(), repaint() and stuff like this but I cant get this to work.
Example:
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
browser.navigate(filePath + "#search=flower");
browser.revalidate();
browser.repaint();
}
});
I appreciate any kind of advice!
Thanks!
meanwhile I've found a solution, although it might be not the perfect one:
You can just remove the JWebbrowser object from your current Frame/Panel and just add it again.
This way it will work just fine.
Just in case someone is having a related problem to this one.
Greetz
I'm working on a Java7 Swing "wizard" type of project that needs to validate a web address before continuing on to the next step of the wizard. The validation requires accessing a URL over the internet to verify that expected resources are available. In some cases, this can take a few seconds which can be long enough to confuse a user.
As a quick solution to this, I would like to disable the "next" button and change the display text while the validation is running so the user knows that the wizard is working and not hung up on anything. The problem is that when I add the code to modify the JButton, none of the changes happen until after the validation has completed. This is the case even if I change the button and call revalidate() before I execute the validation methods.
Here is an excerpt of what I've tried:
// create next button
next = new JButton("Next", new ImageIcon(getClass().getResource("/navigate_right.png")));
next.setActionCommand("MYACTION");
next.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Is this the event dispatch thread? "
+ javax.swing.SwingUtilities.isEventDispatchThread());
System.out.println("Changing button");
next.setEnabled(false);
next.setText("Testing Connection");
next.getRootPane().revalidate();
System.out.println("Validating Service");
boolean isValidLocation = ServiceValidator.serviceExists(locationField.getText());
// ...etc...
When I run this code, the lines "Changing button" and "Validating Service" both get printed before the actual button changes in the display. How can I get the JButton to change before System.out.println("Validating Service"); is printed?
The problem is that when I add the code to modify the JButton, none of the changes happen until after the validation has completed.
Your code is executing on the EDT, so you long running code prevents the GUI from repainting itself until the task is finished executing. You need to use a separate Thread for the long running task, maybe a SwingWorker. Read the section from the Swing tutorial on Concurrency for more information.
I have an web application, which generates a JS message
'Are you sure you want to navigate away from this page?' when you try to open a new page.
I know I can handle this message by
Selenium.getAlert();
(or some modification of it, I haven't tried it yet)
But my main problem is that this message generates only when I leave this page.
In selenium I can leave page by using
Selenium.open("new address");
or
Selenium.back();
So I use code like this
Selenium.open("new address");
Selenium.getAlert();
But the problem is that Selenium.open doesn't finish and go to next code line in program until the new page is fully loaded, but the page can't be loaded until program goes to next code line and handles this alert. So it's ablocked situation and I don't know how to handle it.
I don't think it can be done, staying entirely within the Selenium RC API. I have cases similar to this that I handle by launching an AutoIt script, before the open(), that waits for the prompt and answers it. That only works on Windows, but if you need something for other systems, I'm sure there are equivalent tools.
Selenium has always had a problem with alerts and confirmations (which this is - a confirmation has an OK/Cancel choice) that occur duing page loading. There's even an ancient bug number enshrined in one of the error messages that explains that it can't catch them.
Use:
openAndWait(..)
Maybe this other SO question will help you.
I don't know if this will work, but it's an idea. You could try something like:
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
Selenium.open("new address");
} catch (Exception e) {
// Should throw after 1 second
}
// Now we may be able to interact with the alert.
Selenium.getAlert();
GWT FileUpload comes as a widget that produces so that one can upload a file during form submit (at least it's how I understand it :) ) What I want to do is to make it a better-looking and get rid of standard "Browse..." button.
Since I don't have good GWT experience (and no JavaScript), I looked for existing solutions and found quite a good project - gwtupload. It's good, but I realized I'd love to have my own miniature version (and also, I was curious about how it works). So I browsed through code and tried to extract the magic part. I realized that GWT FileInput is used, but it's not shown, and Button clicks are delegated to this FileInput. The code I tried to extract (only the part that delegates the click) after reviewing sources of gwtupload contains this tricky elem.click() JSNI:
class MyUpload extends Composite {
private static native void clickOnInputFile(Element elem) /*-{
elem.click();
}-*/;
public MyUpload() {
final FileUpload upload = new FileUpload();
AbsolutePanel container = new AbsolutePanel();
// container.add(upload);
Button btn = new Button("My Browse..");
btn.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
clickOnInputFile(upload.getElement());
}
});
container.add(btn);
initWidget(container);
}
}
But this seems not to work: clicking on 'My Browse..' results in no effect (just in case I also tried running with un-commented container.add(upload) line) . Could you please help me in understanding what's wrong/missing in this code sample?
Thank you in advance.
P.S. I know that I have to place it on the FormPanel, and I know the rest about how to perform the actual submit/handling in Servlet; the only thing I want to do is this kind of decoration.
Since I have not received any answers, I had to have more investigation of this issue, so I performed a deeper code analysis of gwtupload project in order to understand how GWT FileUpload (which gets transformed into ) can be decorated.
It turned out that element.click() will only work in browsers which support #click() method (IE, Chrome, Safari). Actually, Manuel Carrasco Moñino - project author - mentions it within comments. There's second approach (for Firefox & Opera) that uses hack when FileInput is placed on transparent panel, which however is placed over some decorated button (using absolute positioning); comment by Manuel:
When the user puts his mouse over the button and clicks on it, what really happens is that the user clicks on the transparent file input showing the choose file dialog.
After that, the main work is correctly applying style attributes to elements.
Thus, there are two implementations of custom file upload component, and GWT deferred binding is used to instantiate them depending on Browser.
As for example I mention in my question, there're few fixes ("upload" has to be added to to the container, and it can be set to #setVisible(false)):
class MyUpload extends Composite {
private static native void clickOnInputFile(Element elem) /*-{
elem.click();
}-*/;
public MyUpload() {
final FileUploadWithMouseEvents upload = new FileUploadWithMouseEvents();
AbsolutePanel container = new AbsolutePanel();
container.add(upload);
upload.setVisible(false);
Button btn = new Button("My Browse..");
btn.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
clickOnInputFile(upload.getElement());
}
});
container.add(btn);
initWidget(container);
}
}
This example works fine in IE8.
P.S. Thanks to Manuel for his great library :)