Why can't I apply custom css styles to a single TextBox?
I'm trying to set some error styles on a single TextBox. What's wrong with the following implementation?
init:
static final String STYLES = ErrorRes.INSTANCE.css().style();
#UiField TextBox box;
box.setStylePrimaryName(STYLES);
Resource interface
interface ErrorRes extends ClientBundle {
static final ErrorRes INSTANCE = GWT.create(ErrorRes.class);
#Source("Error.css")
Style css();
interface Style extends CssResource {
#ClassName("gwt-TextBox-error")
String style();
}
}
Error.css
.gwt-TextBox-error {
border: 1px solid red !important;
}
If you whant to get gwt-TextBox-error style name on widget, you need set box.setStylePrimaryName("gwt-TextBox"); - it's by default. And when error occured use box.setStyleDependentName("error", true); HTML will be class="gwt-TextBox gwt-TextBox-error".
And to clear error style, use box.setStyleDependentName("error", false);
OR
You can use addStyleName("gwt-TextBox-error") and removeStyleName("gwt-TextBox-error") in a same way.
---UPDATE---
So, I tryed to run your case and it works well. At the first of all you need inject css from resources to page in runtime:
ErrorRes.INSTANCE.css().ensureInjected();
I use it in the begining of onModuleLoad()
Then, to add error style use: box.addStyleName(STYLES);
and box.removeStyleName(STYLES); to remove it.
You can't use pair setStylePrimaryName() and setStyleDependentName() with bundled css becouse css name will be obfuscated.
Related
I want to define all VaadinGrids in my Application with the component variant GridVariant.LUMO_ROW_STRIPES. I don't want repeat the definition on all grid instances as shown below.
grid.addThemeVariants(GridVariant.LUMO_ROW_STRIPES);
Is there any way to do this with a global configuration or something else?
I have tried so far to use the #Theme Annoation to define a theme variant. But this doesn't work.
As already mentioned in the comment, one approach is to create a subclass of Grid, apply the variant to it, and use it instead of the Grid class in your app.
Another option is to apply the CSS with which the variant is implemented to the Grid in your own theme. It's only 4 lines of CSS: https://github.com/vaadin/web-components/blob/master/packages/grid/theme/lumo/vaadin-grid-styles.js#L312-L316
Just remove the [theme~='row-stripes'] parts from the selector, and load that css into the Grid's shadow DOM e.g. by placing it in themes/your-app-theme/components/vaadin-grid.css
One option is to subclass Grid and add the variant in the constructor:
public class MyGrid extends Grid {
public MyGrid() {
addThemeVariant(GridVariant.LUMO_ROW_STRIPES);
}
}
Another option is to copy-paste the variant CSS to your own custom theme, and remove the host selector, so that that styles are not scoped to any variant:
frontend/themes/myapp/components/vaadin-grid.css:
[part~='row']:not([odd]) [part~='body-cell'],
[part~='row']:not([odd]) [part~='details-cell'] {
background-image: linear-gradient(var(--lumo-contrast-5pct), var(--lumo-contrast-5pct));
background-repeat: repeat-x;
}
If you're using Vaadin 24 (prerelease at the time of writing), you can use the new recommended way of styling (avoid injecting styles into the shadow root of the component):
frontend/themes/myapp/styles.css:
vaadin-grid::part(even-row-cell),
vaadin-grid::part(even-row-cell details-cell) {
background-image: linear-gradient(var(--lumo-contrast-5pct), var(--lumo-contrast-5pct));
background-repeat: repeat-x;
}
I have created a simple grid with one column:
public MyGrid() {
addComponentColumn(this::getIcon).setClassNameGenerator(i -> "icon-img");
setItems(/** some items */);
setClassName("sidebar-grid");
}
And I have a css theme called mangaTheme. I use it like this #Theme("mangaTheme"). In the mangaTheme folder I have styles.css file with the following content:
.icon-img {
padding: 0;
}
.sidebar-grid {
width: 102px;
margin: auto;
margin-left: -30%;
}
The sidebar-grid css properties are applied properly as the grid is moved, but the icon-img properties are not applied whatsoever:
The classnames are applied:
What am I doing wrong or missing? I have also read this guide: https://cookbook.vaadin.com/dynamic-grid-cell-styling
EDIT: After configuring my workspace as was mentioned in the answer this is the resulting structure, but it still does not seem to function properly.
What you're missing is that the cell <td> element is inside the shadow DOM of the vaadin-grid component, and thus cannot be styled with global CSS. To style parts of components that are inside the component's shadow DOM, you need to inject the CSS into the component.
In the Cookbook example, this is done through the themeFor parameter in the annotation that loads the stylesheet:
#CssImport(themeFor = "vaadin-grid", value = "./recipe/dynamicgridcellstyling/dynamic-grid-cell-styling.css")
In your theme folder, however, you can do the same thing by putting that CSS in a stylesheet called vaadin-grid.css in the components subfolder, i.e.:
themes/mangaTheme/components/vaadin-grid.css
Another thing you're missing is that the classname is applied to the <td> cell, but the padding is on the vaadin-grid-cell-content element slotted into the cell, not the cell itself, so you need to rewrite your selector:
.icon-img ::slotted(vaadin-grid-cell-content) {
padding: 0;
}
(The sidebar-grid CSS class works fine as-is because it's applied to the vaadin-grid root element, which is in the page's regular DOM.)
with Vaadin 14(.1.19) comes this Time Picker component: https://vaadin.com/components/vaadin-time-picker/java-examples
This is how it looks like (when it's read-only):
How can I get this Time Picker to show the time centered like this (this is a screenshot of a manual manipulation in the browser (setting text-align:center directly at the embedded input field), not a programmed solution)?
I tried to set the text-align property in the Java code without effect:
TimePicker timepicker = new TimePicker();
timepicker.getElement().getStyle().set("text-align", "center");
And I searched for a theme variant. But that seems to exist for TextFields and derived fields only:
EmailField emailFeld = new EmailField();
emailFeld.addThemeVariants(TextFieldVariant.LUMO_ALIGN_CENTER);
You will need to change the CSS within the shadow DOM of TimePicker's TextField value part, we use theme attribute as additional selector in order to not to theme all the text fields:
[part="value"] {
:host([theme~="center"]) text-align: center;
}
Include the CSS via #CssImport annotation, theming the text field and set the theme attribute to the date picker. The theme attribute is propagated to the text field used in the date picker:
#CssImport(value = "./styles/my-time-picker-styles.css", themeFor = "vaadin-time-picker-text-field")
public class YourViewOrLayout extends Composite<Div> {
...
timePicker.getElement().setAttribute("theme", "center");
}
I explained it in bit more detail in this answer.
I have two labels, two datepickers, and a submit button within a DockLayoutPanel. I'm trying to just get the button to show up in the center of the panel.
Here is the code I am trying to get just to get the button centered:
Button b = new Button("Submit", new ClickListener()
{
public void onClick(Widget sender)
{
getAwards(text.getText(),text2.getText());
}
});
b.setWidth("80px");
b.addStyleName("gwt-Button");
and within my css:
.gwt-Button{
margin-left:auto;
margin-right:auto;
}
What edits or method do I have to take to actually get this to work?
Edit: For extra information, this button is being added to a DockLayoutPanel
Try:
.gwt-Button{
position:absolute;
left:50%;
margin-left: -40px; /* Width Button /2 */
}
Try disabling the loading of whatever default GWT theme you are using (such as clean) in your gwt.xml file, it may be messing with things, since you're using the same class name.
Alternatively, just use a CSS class name other than "gwt-Button" here.
How to change single element of the widget style in GWT. I would like to create new version of TextBox style, so that only the border color changed to red, for example.How to get to the style responsible for the TextBox?
I tried to create new style
.gwt-TextBox.invalid {
border-color: red;
}
but it does not work.
Make sure you add class invalid to your TextBox:
textBox.addStyleName("invalid");
Use CssResource to associate your CSS file with GWT: http://code.google.com/webtoolkit/doc/latest/DevGuideUiCss.html#cssfiles