Posts

Showing posts from 2016

Java Embedded H2 Tutorial

Image
This tutorial includes JAVA code to simplify the creation and use of H2 embedded databases. The example 'little main test' creates an embedded database  on c:/tmp, named 'h2db' using username 'sa' and password 'sasa', in server mode. Then it connects to it, does nothing (little main test does nothing, make it grow bigger), disconnects and then frees the resources used by the database before ending its execution. public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException { // Instance an embedded H2 EmbeddedH2 h2db = new EmbeddedH2(); //Optional: set SQL mode //h2db.setH2SQLMode("MYSQL"); //Optional, open it as server on port: h2db.setPort(9876); h2db.init( new File("f:/tmp"), "h2db", "sa", "sasa"); System.out.println("OK."); // Create a connection Connection con=h2db.getC

Vaadin 7: Setting the Nth Element in a Container Layout

VAADIN's AbstractLayout, the parent of Horizontal and VerticalLayouts, contains methods to add components to the back and in front of existing ones, but not in the middle of them . It should be pretty obvious how to perform this task, but it is not. Once a component is moved to another layout it is removed from the original, so an iteration will not work as the list of components changes in size in each iteration. Here is the code: public void addNthComponent(             AbstractOrderedLayout layout,             Component c,             int pos) {         HorizontalLayout nc = new HorizontalLayout();         int count = layout.getComponentCount();         for (int i = 0; i < pos; i++) {             Component ci = layout.getComponent(0);             nc.addComponent(ci);         }         nc.addComponent(c);         for (int i = pos; i < count; i++) {             Component ci = layout.getComponent(0);             nc.addComponent(ci);         }         for