Label and comment make a web address. An Easter egg in Netbeans? - java

It is well known, that if we'll put a web address as a line in a Java method, it should and would be interpreted as a label with // comment after it.
public static void setUpClass() throws Exception {
https://www.google.cz/ <------------------------------- This one
FailOnThreadViolationRepaintManager.install();
}
It seems, that NetBeans has one more interpretation for that: It really interprets the whole line as a web address. It works as web address! When mouse-hovering over it with pressed Ctrl you see the name of the site, and if you click, the page appears in the default browser.
Is it a bug? A feature? Or an Easter egg?
Edit.
Eclipse works almost the same way. But it shows the line as a link only with pressed CTRL + hovering. Whereas Netbeans shows it as a link constantly.

Related

How to implement printing in chrome pacakaged app created from GWT Application.?

I have integrated the GWT application with Chrome packaged app with help of DirectLinkerinstaller like the code below:
public class CSPCompatibleLinker extends DirectInstallLinker {
#Override
protected String getJsInstallLocation(LinkerContext context) {
return "com/google/gwt/core/ext/linker/impl/installLocationMainWindow.js";
}
}
But now I want to call print function from Chrome packaged app. When I call window.print() it allows me to print current window, but I need to open a new separate window and print that.
Could you anyone please help me in this?
I can't answer anything about GWT or DirectLinkerinstaller, but here's an answer about Chrome Apps, assuming that's what you're asking about:
You use the chrome.app.window.create API to create a window. Then, you can call the print method for that window.
In my apps, I seldom want to print what's in a window, but rather something I've generated specifically for printing. For that, I create a PDF with jsPDF (Google it), which works well. Then I display the PDF in a window, and let the user print the PDF (or save it).

Release java applet resources when browsing to a URL via code

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.

Eclipse: Ctrl+Shift+Right is incorrect

Consider this simple class:
package net.zomis.test;
public class Test {
public Test() {
}
public void registerSomething(String key) {
}
}
When I have the cursor placed right before registerSomething and pressing CtrlShift→, I'm used to Eclipse only selecting register at first. But now, it selects the entire registerSomething text, when I press it again it selects the entire method (from public void to }), when I press it again it selects the entire class, and the last time the entire file is selected. The exactly same thing happens if I press CtrlShift←.
Also, when I simply click the End key, I get an option to choose: Line End or Text End.
What has gone wrong in my settings for this to happen? Where can I find these keybindings?
If it matters, I have this workspace within my Dropbox-folder to synchronize it between my PC and Mac.
Turns our that the fact that I had the workspace in my Dropbox folder matters a lot. Note to everybody else: Do not share your workspace between computers. How I solved this is the following:
Create a new workspace on each computer
In the computer-specific workspace, import projects from your shared Dropbox/other folder
I noticed that when having a shared workspace on the computers, the Mac somehow changed the key-bindings to the Mac-version, which made them not work on the PC. The correct Ctrl+Shift+→ key-binding on Mac is Alt+Shift+→.
So by using a separate workspace on each computer, I can use that computer's specific key-bindings, without mixing them up.
Open the preferences dialog from the Main Menu: Window->Preferences.
In the dialog, open General->Keys. You'll see a table of keybindings, with a field with type filter text. In that field, type: Ctrl+Shift+Right, and you'll narrow the table down to this key.
The behavior that you expect - only register is selected with the first keypress - is the behavior that I see when using Ctrl+Shift+Right, and the keybinding for me is: Select Next Word.
There is a Restore Command button that should reset this binding if it has been changed.

Is there a way to expand a JFileChooser directory without a mouse

Using a JFileChooser, I can select a directory by double clicking the directory (going down a level) with my mouse. Is there a way to select a directory without the mouse? For example, is there a key binding to go down a directory level or do I have to somehow add a key listener to the JFileChooser?
You should be able to use tab to move between the different parts of the chooser, and then use the arrow keys to change which directory is highlighted, and then press Enter to change the directory to the highlighted one.
I have tested the following example code on my machine (Vista/JDK 1.6) and it works as I would expect:
import javax.swing.*;
public class test {
public static void main(String[] args) {
(new JFileChooser("")).showOpenDialog(new JFrame());
System.out.println("OK!");
}
}
If your project is not responding similiarly in your JFileChooser, I would debug as follows:
Create test.java with only the code necessary to pop up a chooser.
If the test app differently than within your app, its something in your code causing it to fail, such as UI skinning code, keyboard listeners, etc. Modify the example, one change at a time to closer replicate your settings for your chooser in your app and see if you can pinpoint where it breaks.
If even a basic test app doesn't work right, it is probably something about your setup, such as a bug in your JDK version, your OS, etc. Troubleshoot your setup.
Have you tried the space-bar or enter key?
Try using ctrl+enter key to select directory.
This behavior is happened when you set to JFileChooser file selection mode to “files and direcories”:
JFileChooser fileBrowser = new JFileChooser();
fileBrowser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

How to give focus to default program of shell-opened file, from Java?

From within Java, I am opening an Excel file with the default file handler (MS Excel, in this case :-) ) using the method described in this stackoverflow question:
Desktop dt = Desktop.getDesktop();
dt.open(new File(filename));
However, the Excel program doesn't get the focus. Is there any easy way to do so?
Edit: There is a related stackoverflow question for C#, but I didn't find any similar Java method.
Edit 2: I've did some simple tests, and discovered that Excel starts and gets the focus whenever no instance of Excel is running. When Excel is already open en NOT minimized, the application doesn't get the focus. If instead the Excel Windows was minimized, the above code will trigger a maximization of the window and Excel getting the focus (or vice versa :-) ).
If you only care about Windows (implied in the question), you can change the way you invoke Excel: use "cmd start...".
I have been using this piece of code to launch Windows applications for some time now. Works every time. It relies on the file association in Windows to find the application. The launched application becomes the focused window on the desktop.
In your case, Excel should be associated with .xls, .csv and other typical extensions. If it is, Windows will launch Excel, passing your file to it.
Usage:
MyUtilClass.startApplication( "c:\\mydir\\myfile.csv", "my window title" );
file is the full path to the input file for Excel and title is the window title (the application may or may not take it - Excel changes the window title).
public static void startApplication( String file, String title )
{
try
{
Runtime.getRuntime().exec( new String[] { "cmd", "/c", "start", title, file } );
}
catch( Exception e )
{
System.out.println( e.getMessage() );
}
}
From a scala-program, which runs in the JVM too, I can open an application, and that get's the focus by default. (Tested with xUbuntu, which is a kind of Linux).
import java.awt.Desktop
val dt = Desktop.getDesktop ();
dt.open (new java.io.File ("euler166.svg"));
I can't say, whether this is specific for Linux, or maybe something else - however starting Inkscape in my example, excel in yours, may take a few seconds, while the user impatiently clicks in the javaprogram again, thereby claiming the cursor back. Did you check for that?
You could then change to the last application, at least on Linux and Windows with ALT-Tab aka Meta-Tab (again shown in scala code, which you can easily transform to javacode, I'm sure):
import java.awt.Robot
import java.awt.event._
val rob = new Robot ()
rob.keyPress (KeyEvent.VK_META)
rob.keyPress (KeyEvent.VK_TAB)
rob.keyRelease (KeyEvent.VK_TAB)
rob.keyRelease (KeyEvent.VK_META)
but unfortunately the unknown source off more trouble, also known as user, might do nothing, so switching would be the false thing to do. Maybe with a thread, which checks for a certain amount of time, whether the java-program has the focus, but it keeps a form of roulette, in an interactional environment, because the user may have a fast or slow machine, or change to a third application meanwhile, and so on. Maybe a hint before triggering the new app is the best you can do?

Categories

Resources