Table item icon clickevent

Hi everybody

i´ve got a table with one row and an itemitcon

tbl_table.setItemIcon(s1, new ThemeResource("../xxx.png"));

is ist possible to get the clickevent if a user clicks on the itemicon?

Yes you can if the item is a button, Link or any component that has a listener.

ok, and if a add a button in every line how can i get the item to that the button belongs?!

i am trying to add an icon to the table an when the user clicks on it, it should change. but only if he clicks on the icon and not on the other attributes of the item in the table

somebody got another way to solve this problem?!

Instead of using table.setItemIcon… . Create another column for the table which will contain buttons and you can then use their listeners. You can use Button.setData(itemId) to store the itemId with which you can identity each row from the clickevent.

Hopefully this is clearer.

You can’t register any listeners with a “setItemIcon”, so this approach will not work for you.

There are usually two approaches to this problem (“doing something with a particular row”)

  • Buttons/Icons on each row
  • Select row, and button outside the table

It seems you want the first approach - in which case you will need to use a
ColumnGenerator

Essentially, a column generator is called for each row, and allows you to create any component you like.

Cheers,

Charles

is it possible to handle clicks on an image in a table?

if it´s possible, how can i add some images in a table?

i am planing to add a start button in the table and if the user clicks the button i want to do something.

something like that?:


final Table table = new Table();
table.addContainerProperty("bnt", Button.class, null);		
table.addContainerProperty("string", String.class, null);
final Button btn = new Button("test");

table.addItem(new Object[] {btn,"blabla"}, new Integer(1));
btn.setData(table.firstItemId());
		
btn.addListener(new Button.ClickListener() {
	public void buttonClick(ClickEvent event) {
		if(event.getButton().getData().equals(btn.getData())){
			System.out.println("equals1");
		}
	}
});

it is only a test case. when i include this to my program i will use a container. :slight_smile:
but how can i get the item id without checking the complete container?! my only idea was after inserting the item with container.lastitemid()

Well, it’s certainly possible to handle clicks on a button or a link, and both of those can have an image, so yes.

By creating a column generator that generates a button or link with the image that you want!

Cheers,

Charles.

ok sounds very good.

do you have an example for me?!

Yeah. That seems ok .



for (final Person p : nicePeople) {
			final Button btn = new Button();
			btn.setIcon(Icons.NigeriaIcon);
			btn.addListener(new Button.ClickListener() {
				public void buttonClick(ClickEvent event) {
					if (p.country.startsWith("Nigeria")) {
						btn.setIcon(Icons.Alt_NigeriaIcon);
					}

				}
			});
			
			table.addItem(new Object[] { btn, "blabla" }, p);

		}

}



If you will be using the same Listener for all the buttons. then use setData(itemId) to differentiate . For example.



	for ( Person p : nicePeople) {
			final Button btn = new Button();
			btn.setIcon(Icons.NigeriaIcon);
			btn.addListener(changerListener);
			
			table.addItem(new Object[] { btn, "blabla" }, p);

		}
	}

	private Button.ClickListener changerListener  = new ClickListener() {
		
		@Override
		public void buttonClick(ClickEvent event) {
			Person p = (event.getButton().getData());
			if (p.country.startsWith("Nigeria")) {
				btn.setIcon(Icons.Alt_NigeriaIcon);
			}
			
		}
	};

YMMV