5

I'm trying to migrate an existing Vaadin 8 application to Vaadin 12 and need to know how to recreate the functionality of Vaadin 8's GridLayout in Vaadin 12.

According to Vaadin Docs a GridLayout can be replaced in Vaadin 12 by: "Use Div together with the new CSS Grid functionality that is supported in most browsers"

Unfortunately it's not totally clear how exactly this can be done.

Lets assume that I have a Vaadin composite "HelloGrid":

@StyleSheet("styles/hello-grid.css")
public class HelloGrid extends Composite<Div> {

   public HelloGrid(){

     // EDIT: This line is my solution to the question
     getElement().getClassList().add("hello-grid");

     Label labelOne = new Label();
     labelOne.addClassName("label-one");

     Label labelTwo = new Label();
     labelTwo.addClassName("label-two");

     add(labelOne);
     add(labelTwo);
   }
}

And a css file "hello-grid.css":

.hello-grid {
    display: grid !important;
    grid-template-rows: auto;
    grid-template-columns: 1fr 1fr;
}

.label-one {
    grid-column: 1;
}

.label-two {
    grid-column: 2;
}
  • How can I associate the ".hello-grid" css class with the HelloGrid Composite.
  • Is this the right/preferred way to use a css grid in Vaadin 12 at all
1
  • Meanwhile I found a possible solution by myself: add getElement().getClassList().add("hello-grid"); to the constructor of the Composite. But I initially expected to be able to set the elements classname by an annotation instead. I'll edit my question with this solution. Commented Jan 29, 2019 at 13:55

1 Answer 1

3

It's a bit too late, but maybe answers someone else question.

It depends where exactly you have placed your css styles. I would suggest to place under webapp\frontend\styles, then you would be able to access them using @StyleSheet("frontend://styles/hello-grid.css"). As it also noted in official documentation, here Including Style Sheets it's

Relative to Servlet URL

Using your example with this set-up: Style's file location

and styles like those (basically just adding colors to your stylesheet to verify, it works ) Style's file contant

this was the output: enter image description here

I haven't found any exact guides on Vaadin page, how you could use Grid styles, but this tutorial looks quite promising A Complete Guide to Grid. Otherwise, there shouldn't be anything special regarding Vaadin.

Also, it seems that there is an add-on in directory, which might help Css Grid Layout (Through, I haven't tried it out myself)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.