Align checkbox with checkbox group horizontally in Vaadin 8 - vaadin

My application (based on Vaadin 8) has a CheckBox and a CheckBoxGroup wrapped in a HorizontalLayout.
CheckBox myCheckBox = new CheckBox("My Checkbox");
CheckBoxGroup<String> multi = new CheckBoxGroup<>("Multiple Selection");
multi.setItems("Many", "Muchos", "Monta");
multi.setCaption(null);
multi.addStyleName(ValoTheme.OPTIONGROUP_HORIZONTAL);
checkBoxLayout.addComponents(myCheckBox, multi);
checkBoxLayout.setComponentAlignment(myCheckBox, BOTTOM_LEFT);
checkBoxLayout.setComponentAlignment(multi, BOTTOM_LEFT);
this.addComponent(checkBoxLayout);
The problem is that the horizontal alignment of the checkbox and the checkbox group is not correct.
Setting the checkbox group caption to null does not help either. I suppose that the checkbox group has some additional spacing which could cause this behavior. How can I make sure that the horizonal alignment is working as used with setComponentAlignment?
Update 1
The solution suggested by #ollitietavainen (using MIDDLE_CENTER) results in the following:

If you have nothing else than the checkboxes inside the HorizontalLayout, using Alignment.MIDDLE_CENTER seems to work better than BOTTOM_LEFT.

I tried removing the CheckBoxGroupState.primaryStyleName "v-select-optiongroup" style name than along with the alignment suggested by https://stackoverflow.com/a/65180983/12854146 I have the following code
HorizontalLayout checkBoxLayout = new HorizontalLayout();
checkBoxLayout.setMargin(true);
CheckBox myCheckBox = new CheckBox("My Checkbox");
CheckBoxGroup<String> multi = new CheckBoxGroup<>("Multiple Selection");
multi.setItems("Many", "Muchos", "Monta");
multi.setCaption(null);
multi.addStyleName(ValoTheme.OPTIONGROUP_HORIZONTAL);
multi.removeStyleNames("v-select-optiongroup");
checkBoxLayout.addComponents(myCheckBox, multi);
checkBoxLayout.setComponentAlignment(myCheckBox, MIDDLE_CENTER);
checkBoxLayout.setComponentAlignment(multi, MIDDLE_CENTER);
you may have a similar output if you do not remove the style name "v-select-optiongroup" from multi and instead you add it to your checkbox with
myCheckBox.addStyleName("select-optiongroup");

Related

Dynamically change ag grid header name?

How to change the header of ag-grid dynamically using angular 7?
I have tried:
this.gridOptions.columnController.allDisplayedCenterVirtualColumns[1].colDef.headerName = "22";
but it works only in debug mode.
Columns are updated with setColumnDefs()
Refer this doc
const col = document.getElementById('colSelect').value;
columnDefs[col].headerName = value;
gridOptions.api.setColumnDefs([]);
gridOptions.api.setColumnDefs(columnDefs);
Try this solution

Adding components to BorderLayout

I'm trying to implement BorderLayout within IntelliJ and am having trouble getting it to work. It compiles fine, but when it runs I see the viewer for a second and then it crashes. The code I currently have is
Button Next=new Button("Next");
Button Back=new Button("Back");
Container panel1=new Container();
Container panel2=new Container();
home = new Form("Home");
home.setLayout(new BorderLayout());
panel1.setLayout(new BorderLayout());
panel2.setLayout(new BorderLayout());
home.addComponent(BorderLayout.EAST,panel1);
home.addComponent(BorderLayout.WEST,panel2);
panel1.addComponent(Next);
panel2.addComponent(Back);
The error I get after it crashes is "Cannot add component to BorderLayout Container without constraint parameter". I tried researching a constraint parameter and also working with BorderLayout in IntelliJ, but any texts I found were either not helpful or too complicated to understand. Thanks so much!
The error message is telling you that if you want to add a component to a border layout you have to specify where to put it. You've done this when adding components to home, but you haven't when you add components to panel1 and panel2. You need to add BorderLayout.EAST (or WEST, or whatever) to get:
panel1.addComponent(BorderLayout.EAST, Next);
panel2.addComponent(BorderLayout.WEST, Back);
However, I think you're using this wrong. You probably don't want a border layout in panel1 or panel2 -- they're fine with the default flow layout, so if you remove the panel1/2.setLayout() lines your code should work fine.
BTW: In java we don't use capital letters at the start of variables, so Next and Back should be next and back. Also, panel1 should be something like nextPanel, and panel2 should be something like backPanel.
To add one more thing -- nobody uses awt any more. We've all moved on (mostly to html5). So, trying this in swing, you would get:
import javax.swing.*;
...
JButton next = new JButton("Next");
JButton back = new JButton("Back");
JFrame home = new JFrame("Home");
home.setLayout(new BorderLayout());
home.add(back, BorderLayout.WEST);
home.add(next, BorderLayout.EAST);

Vaadin 7 check if style name exists

In my Vaadin 7 application I can add a new style name to a component for example via following method:
panel.addStyleName("criteria-menu-active");
But right the next click on this component I need to check if this style was already added to this component and if so - remove it.
So, how to test the component for the presence of a certain style by its name ?
Did you try the following?
myLayout.addStyleName("custom-style");
System.out.println("Style = " + myLayout.getStyleName());
Output:
Style = custom-style

Programmatically select a row in Grid in Vaadin 7?

In the Grid widget in Vaadin 7.5.3, we can determine the current selection of rows by calling SelectionEvent::getSelected or Grid::getSelectedRows.
So how do we set the selection programmatically?
While that's true that official documentation for Grid class doesn't have this method stated, still you can do it programmatically. I won't argue whether it's a bug or not. Firstly you need to know what is your SelectionMode. Then you can select a row (or rows):
#Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
Customer c = new Customer(1);
container = new BeanItemContainer<>(Customer.class, Arrays.asList(c, new Customer(2)));
grid = new Grid(container);
grid.setSelectionMode(SelectionMode.SINGLE);
SingleSelectionModel m = (SingleSelectionModel) grid.getSelectionModel();
m.select(c);
layout.addComponents(grid);
setContent(layout);
}
In newer Vaadin (in my case 7.5.6) there is select(Object) method directly in Grid interface.
Example:
Grid grid = new Grid(container);
grid.setSelectionMode(Grid.SelectionMode.SINGLE);
grid.select(row);
The row object for example could be taken from SelectionListener event or from added before object (as in #kukis answer).
Setter Method Missing (bug?)
The Book of Vaadin mentions the setter method Grid::setSelectedRows along with a getter.
The currently selected rows can be set with setSelectedRows() by a collection of item IDs, and read with getSelectedRows().
However, the Grid class doc does not list that method. Nor does NetBeans 8.0.2 suggest that method in its auto-complete.
So apparently a bug. See Ticket # 18,580.

Vaadin combobox

I want to create Vaadin drop down with 2 separators in it. I couldn't find a way to implement that, can anyone help me to solve this issue?
This is the way I want to display my drop down:
Option 1
Option 2
------------;
select 1
select 2
-----------;
group 1
How can I do that?
There is no built-in way to add separators to selects. The only way I can think of is to add an item with the desired separator as its caption. For example if you use the default caption (item id) select.addItem("-----"); should be enough. This should work for both ComboBoxes and NativeSelects.
You can implement a new Vaadin component including the client behaviour, but this is not an easy solution. This page https://vaadin.com/book/-/page/gwt.html of "Book of Vaadin" and Vaadin forum can help for that.
Also, creating your own component using existing components is another solution. You can implement a special combobox which takes values of String or Component arrays. The way of doing this is using Vaadin panels, layouts and windows with size and locations and click listeners.
I haven't tried it myself but give a go at NativeSelection dropdown.
You can always do
{select.addItem("-----");}
Once I also wanted a do something like that but there was no proper way to do that with Vaadin. I actually created a Vaadin widget extending the Combo Box. In the client side widget of Vaadin they filter out the HTML content before adding items to the list. So Using the client side code I override that functionality and use HTML tag "" to add the line.
select.addItem("-----");
looks like the best way, I dont know about some other
Btw if you are reading items from some list you can combine that with some item counter and (itemsCount%n)==0 operator to set separator after 'n' items inserted :)
You can add the item to the selected (as mentioned before) and then disable the separators with some javascript:
add the item to the select.
cb.addItem("separator");
cb.setItemCaption("separator", "-------------");
execute the javascript
final String javascript = ""
"var selects = document.getElementsByTagName('select');"
"for(var j = 0;j < selects.length;j++){"
"var op = selects[j].getElementsByTagName('option');"
"for (var i = 0; i < op.length; i++) {"
" if(op[i].text == '" + separatorText + "') op[i].disabled = true;"
"}}";
Page.getCurrent().getJavaScript().execute(javascript);
Is there a reason that you use the ComboBox instead of the Select, because with the select you can do that.
Select select = new Select();
select.setItems("Option 1", "Option 2", "select 1", "select 2", "group 1");
select.addComponentAtIndex(2, new Hr());
select.addComponentAtIndex(5, new Hr());
Or you can use a MenuBar but it looks very diferent that the ComboBox.
menuBar = new MenuBar();
MenuItem menuItem = menuBar.addItem("Select");
menuItem.getSubMenu().addItem("Option 1");
menuItem.getSubMenu().addItem("Option 2");
menuItem.getSubMenu().addItem(new Hr());
menuItem.getSubMenu().addItem("select 1");
menuItem.getSubMenu().addItem("select 1");
menuItem.getSubMenu().addItem(new Hr());
menuItem.getSubMenu().addItem("group 1");

Resources