07 January, 2015

How to close Java splash image after the application starts

There is a very easy way to get Splash screen in Java application.
With a "-splash:imagepath" command line parameter, or with "SplashScreen-Image: imagepath" manifest parameter.

But when an application is started, Java still shows this image (below the application layer, but it visible when you switching between the applications), until the application is finished. Strange behavior, but it is possible to close this kind of splash screen.

With these lines of code:
import java.awt.SplashScreen;
...
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen != null) {
    splashScreen.close();
}

How to create a file extension association with the program in Windows

Assume that you have a program named "table-explorer.exe".
And you want to open all ".csv" and ".sas7bdat" files using  this program by default in Windows.

First you need to create a file extension association with the program via these commands:


reg add HKCU\SOFTWARE\Classes\.csv /d "table-explorer" /f
reg add HKCU\SOFTWARE\Classes\.sas7bdat /d "table-explorer" /f


And then register the application:


reg add HKCU\SOFTWARE\Classes\table-explorer\shell\open\command /d "\"C:\Path\to\table-explorer.exe\"" /f

06 January, 2015

How to bind hotkeys in Java FX

It is possible to create global keyboard shortcuts using a code like this:
import static javafx.scene.input.KeyCombination.keyCombination;
...
private void initHotkeys() {
    ObservableMap accelerators = stage.getScene().getAccelerators();
    accelerators.put(keyCombination("Alt+S"), searchTextField::requestFocus);
    accelerators.put(keyCombination("Alt+C"), columnsTextField::requestFocus);
    accelerators.put(keyCombination("Alt+O"), openFileButton::fire);
    accelerators.put(keyCombination("Alt+E"), exportButton::fire);
    accelerators.put(keyCombination("Alt+T"), truncateButton::fire);
    accelerators.put(keyCombination("Alt+H"), () -> filterChoiceBox.setValue(MATCH_HIGHLIGHT));
    accelerators.put(keyCombination("Alt+F"), () -> filterChoiceBox.setValue(MATCH_FILTER));
    accelerators.put(keyCombination("Alt+U"), () -> filterChoiceBox.setValue(MATCH_FILTER_UNIQUE));
    accelerators.put(keyCombination("Alt+D"), () -> {
        //todo some debug action
    });
}

09 November, 2014

How much memory is allocated to the String object in Java

Size in bytes
String() object
Object header 16
Hash field 4
Reference to char array 4
Char[] array
Object header 16
Array length 4
Array size N*2

String allocates in memory at least (24+20+N*2) bytes, where N - number of characters in the string. If N=10 then string size = 64 bytes.

One million of these strings = 64 000 000 bytes. But also we need array or collection to store these strings.
This requires additional memory.
ArrayList<String>(1 000 000) ~= 4 000 000 bytes (4 bytes per reference to string object).

So, total size will be 68 000 000 bytes.

Gist code: Output:
3,355,792 Initial
1,217,624 After GC
218,921,768 Created list of strings
76,489,936 After GC
68,000,000 Expected size in memory
But if you have many similar strings, then using of String.intern() greatly reduces amount of allocated memory. Output:
3,355,792 Initial
1,217,792 After GC
106,750,232 Created list of strings
13,127,480 After GC
68,000,000 Expected size in memory
10x less memory! 6.8 megabytes instead of 68

03 November, 2014

The best resolutions

List of multiple resolutions in the most popular proportions:

Base width 1/2 2/3 3/4 4/5 5/6 9/16 10/16
240 120 160 180 192 200 135 150
480 240 320 360 384 400 270 300
720 360 480 540 576 600 405 450
960 480 640 720 768 800 540 600
1200 600 800 900 960 1000 675 750
1440 720 960 1080 1152 1200 810 900
1680 840 1120 1260 1344 1400 945 1050
1920 960 1280 1440 1536 1600 1080 1200
2160 1080 1440 1620 1728 1800 1215 1350
2400 1200 1600 1800 1920 2000 1350 1500
2640 1320 1760 1980 2112 2200 1485 1650
2880 1440 1920 2160 2304 2400 1620 1800
3120 1560 2080 2340 2496 2600 1755 1950
3360 1680 2240 2520 2688 2800 1890 2100
3600 1800 2400 2700 2880 3000 2025 2250
3840 1920 2560 2880 3072 3200 2160 2400