Grid: show selected item value in a window

Hi,

I can show the selected item value of an individual cell by simply doing like this:

 [code]

grid.addItemClickListener(event →
Notification.show("Value: " +
MyContainer.getContainerProperty(event.getItemId(),
event.getPropertyId()).getValue().toString()));
[/code]How do I show the selected item value in a window?

If my window is created like this:

MyWindow subWin = new MyWindow();
//add container item value inside a window, but how??
UI.getCurrent().addWindow(subWin);

Make sure you have access to the Grid component where you create the Window and then you can use:

https://vaadin.com/api/com/vaadin/ui/Grid.html#getSelectedRows()
and getSelectedRow()

Hi,

Thanks for the your answer. I am still strugling to show grid selected row inside a window. I did this:

Window sub = new Window();     
 grid.getColumn("example").setRenderer(new ButtonRenderer(event -> {
               BeanItem<Example> item =
                        myContainer.getItem(grid.getSelectedRow());
               item.getBean().getExample();
              // how to show above selected value inside my window, 'sub'
                UI.getCurrent().addWindow(sub);
            }));

As you’Re using BeanITems you can just cast item.getBean() to your Bean class and then use the getter and setter of this class to get the values.
In your Window you can then use Labels, TextFields,… whatever… to display the values.

Thanks for the suggestion.

How can I "
cast item.getBean() to your Bean class
"?

In my window, I am using following code;

VerticalLayout content = new VerticalLayout();
        content.setMargin(true);
        content.setStyleName(ValoTheme.WINDOW_TOP_TOOLBAR);
        // Some content for the window
        content.addComponent(new Label("Example xx"));

In place of "
Example xx
" inside a Label that is currently being displayed, I would like to get value for
item.getBean().getExample();

Do you have some suggestions how to accomplish this?

I would be greateful for your help.

When the BeanItemContainer is set up correctly (e.g. like BeanItemContainer) item.getBean() should return an instance of your Bean class without the need to cast it. So item.getBean().getExample() should work.
In case this might be because of your casting to BeanItem just try
myContainer.getItem(event.getItemId()).getBean().getExample();

Thanks for your kind suggestion.

But, I’m still stuck with this…
Here I am posting some code

my Window Class:

public class ExampleWindow extends Window {
    private static final long serialVersionUID = 1L;
    public ExampleWindow() {
        super("My Bootiful Small Window"); // Set window caption
        center();
        VerticalLayout content = new VerticalLayout();
        content.setMargin(true);
        content.setStyleName(ValoTheme.WINDOW_TOP_TOOLBAR);
        
        Grid grid = new Grid();
        grid.setWidth(16, Unit.INCH);
        
        BeanItemContainer<Example> myContainer = createExampleContainer();
        BeanItem<Example> item = myContainer.getItem(grid.getSelectedRow());
        
        // Some content for the window
        content.addComponent(new Label("Example XX"      //what should i put here??);
        
        content.addComponent(new Button("Empty"));
   
        setContent(content);
      
        // Trivial logic for closing the sub-window
        Button close = new Button("Close");
        close.addClickListener(new ClickListener() {
            /**
             *
             */
            private static final long serialVersionUID = 1L;
            @Override
            public void buttonClick(ClickEvent event) {
                close();
            }
        });
        content.addComponent(close);
    }

Here is my another class in which I have a Component called myGrid,
where I am using a Button renderer to display above window

private Component myGrid(Grid grid) { BeanItemContainer<Example> myContainer = createExampleContainer(); grid.setContainerDataSource(myContainer); grid.setColumnOrder("A", "B", "C", "D"); grid.getColumn("D").setRenderer(new ButtonRenderer(event -> { ExampleWindow sub = new ExampleWindow(); // how do i show the value "D" in my Window/Sub-Window inside above Label??? when I click on this, I need to show "[b] Example D [/b]" inside a Window. UI.getCurrent().addWindow(sub); })); When I click on “D” it displays a sub-widnow with


Example XX
(in place of
XX
i need to display "
D
")

Empty
(as button)

And I don’t know,
if it would be possible to delete/empty specific Grid cell by just clicking on "
Empty
" button?

You could just pass the Bean to your ExampleWindow class so change its constructor to:

public ExampleWindow(Example exampleBean){ ..... and then you should be able to pass the bean in your ButtonRenderer sort of like:

ExampleWindow sub = new ExampleWindow(myContainer.getItem(event.getItemId().getBean())); In public ExampleWindow(…) you can then access the Bean direcly using exampleBean.

I wrote this code without IDE so the syntax might differ slightly.

For your second (delete/remove)-question: Yes you can add another ButtonRenderer and then just call

myContainer.removeItem(event.getItemId()); in the Listener

Thanks for your suggestion. I have one more question:

I can now show one item of exampleBean by doing this:

content.addComponent(new Label(exampleBean.getExampleD())); But how do I show it for all the columns?
Do I have to remove the Label component and again replace it with another Label component? And do I have to create another constructor for each columns? in Window public class.

I don’t really get what your problem is and what you’re trying to achieve here.
In your ExampleWindow you have access to the entire Bean through exampleBean. So to display the other Columns/Properties just add more Labels and use exampleBean.get… to get the other Values.
I have no idea if that is what you want though…

I am sorry for not making this clear enough. You are right, I can easily get the bean now. However, the problem with Window is that I don’t want all beans at once.

For example, when I use
exampleBean.getExampleD
inside a
Label
I’ll get the value ’
D
'.
Now when somebody clicks on other columns, let’s say
C
… the window does not replace the value ’
D
’ and gives me ’
C
’ instead. Of course, to get columns value for
C
, we have to do:
exampleBean.getExampleC

     // Example content for the window

content.addComponent(new Label(exampleBean.getExampleC); So the problem I am facing is not in getting the bean but how to
replace the label with another label for getting the values of a bean and also for all respective columns
.

When user clicks on any column, s(he) should sees only values associated with that column in a
Label
window because I have four items in the window.

First one is this
Label
(where I have to get bean item value and display it –
thank’s to you
, now I can do this easily)
Next item is
Button
. And Whenever I click on this button, I have to empty the value of that Grid cell. (Your solution empties entire row… not individual Grid cell, perhaps this type of feature is not implemented in Grid, I guess).
And I also have other two components (like
TextFied
and
Button
) in a Window.

I hope you got some idea what I am trying to acheive here.

You could change the Window constructor to
public ExampleWindow(String columnValue)
now you can directly use the String in the Window without accessing the Bean. This way you can pass the actual Value of the column you want to the window like:
new ExampleWindow(…getBean().getExampleD())

To “empty” just one colum value either use the Bean’s setter method or use Item.getItemProperty(String propID).setValue(null) or setValue(“”) .

Thanks for your response.

I don’t know what are those … so I put:
new ExampleWindow(myContainer.getItem(event.getItemId().getBean()))

But it says:
“The method getBean() is undefined for the type Object”

Am I missing something here?

Yes. Your code doesn’t work like this.
I’m not sure if I understood what you actually want to do. Just for clarification here is what I think you want. Then you can correct me if that’s wrong.
You have a Window (ExampleWindow) which looks the same but has a Label which schould display different column Values.
You have a grid with several columns. One of them has a ButtonRenderer.
Now on click on the Button you want to display the Window with the Value of one of the other columns. (Really only one column? You also said something about displaying different columns? How do you know which one to display?)

If you have your Window constructor set like
public ExampleWindow(String colValue)
you can use new Label(colValue) instead

When creating the Window use
new ExampleWindow(myContainer.getItem(event.getItemId()).getBean().getExampleC()) or getExampleD or whatever the column value you want is.

I owe you a beer.

Thanks my dear Vaadin friend. :slight_smile:

What I’ve managed so far I set my grid to:

new ExampleWindow(myContainer.getItem(event.getItemId()).getBean().setExampleD(null));

and it does empties the bean item value in the
Window
. However, it
does not empty
the bean item value from the container itself. I would like to
keep the bean item value in window
and just
empty it from container
, but I don’t know how.

In my window class, I have a button and I am trying to empty the bean value when user clicks on this “empty” button…
Here is what I have been trying so far in ExampleWindow

Button empty = new Button("Empty");

        empty.addClickListener(new ClickListener() {

            /**
             *
             */
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                   content.removeComponent(new Button(String anotherValue));
            }

        })

Well problem with BeanContainer is that while accessing Values is easy to do with the Bean, manipulating items isn’t. Or at least i wasn’t able to do it easily but I rarely use Bean containers.
In you Bean Class your Properties have a name like exampleD or exampleC (or something like that) To now change that Value use Item.getItemProperty("exampleC).setValue(null) [exampleC is Case sensitive and should match the variable name in your Bean]
while Item needs to be replaced by your instance of item. Whether you access it directly from the container with myContainer.getItem(…).getItemProperty… or you have the item in a variable:

Item someItem = myContainer.getIe.....
someItem.getItemProper.....

As your ExampleWindow is written quite dynamically it has no idea about which item, itemId or even which column it should delete. Either you pass some variable to the Window from which you can then get the item and the column to manipulate (like for example the item object + the column ID or the itemID and column ID, etc…)
or you implement some Listener/Eventbus interface (should be examples on Google. This is more basic Java than Vaadin specific) to carry the Button Click back to your RendererClickListener where you know what to manipulate. If you do that make sure to remove Listeners if not needed to avoid weird behaviour.

Thanks for your kind suggestions.

When I try to put this:
Item.getItemProperty(“exampleC”).setValue(null)

in my Bean Class, it complains like this:


“Cannot make a static reference to the non-static method getItemProperty(Object) from the type Item”

So I don’t know how I can access it directly from the container with commands…

myContainer.getItem(…).getItemProperty… (what are those missing dots… full syntax, please)

Please give full syntax I don’t know what to fill in these dots… (I hope you will not mind)

Item someItem = myContainer.getIe.....
someItem.getItemProper.....

I’ll have to investigate more on second part … for ExampleWindow as it is written quite dynamically (you are right) I will investigate more on your recommendation. Thanks.

Okay let’s explain this one by one.
getItemProperty isn’t a static method so you can’t use it like Item.getItemProperty… I just wrote this to clarify that it’s a method of Item.
You probably shouldn’t use getItemProperty inside your Bean class but rather at whatever point you want to access the property and manipulate its value. For the sake of an example I’m going to write a bit of code that should create an instance of ExampleWindow with ExmapleC’s value and then remove the Value of the column/Property/Bean variable exampleC.

new ExampleWindow(myContainer.getItem(event.getItemId()).getBean().getExampleC());
myContainer.getItem(event.getItemId()).getItemProperty("exampleC").setValue(null);

This only works in a place where myContainer is defined and visible and there is some kind of event.getItemId() so for example in your RendererClickListener. Also exampleC might be written differently in your Bean so be aware of that. Last but not least: The type of exampleC might not accept/like null Values so maybe “” or 0 or 0f or whatever type it is might be better.
Your question are often quite basic Vaadin or even just plain Java specific. The whole ItemProperty thing is for example explained in great detail in the Book of Vaadin and several forum posts.
Not to sound rude or annoyed but I think a carefully written tutorial might explain things better than I can