Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.cerebro.provevaadin; import com.vaadin.event.ShortcutAction; import com.vaadin.external.org.slf4j.Logger; import com.vaadin.external.org.slf4j.LoggerFactory; import com.vaadin.ui.Button; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Label; import com.vaadin.ui.Panel; import com.vaadin.ui.TextField; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; /** * * @author matteo */ public class ChatOffGame extends VerticalLayout implements BroadcasterOffGame.BroadcastListener { String FROM = "offGame"; Logger logger = LoggerFactory.getLogger(ChatOffGame.class); VerticalLayout messages = new VerticalLayout(); public ChatOffGame() { Panel messagesPanel = new Panel(); messagesPanel.setSizeFull(); messagesPanel.setContent(messages); this.addComponent(messagesPanel); this.setExpandRatio(messagesPanel, 1.0f); HorizontalLayout sendBar = new HorizontalLayout(); sendBar.setWidth("100%"); final TextField input = new TextField(); input.setWidth("100%"); sendBar.addComponent(input); sendBar.setExpandRatio(input, 1.0f); Button send = new Button("Send"); send.setClickShortcut(ShortcutAction.KeyCode.ENTER, null); send.addClickListener((Button.ClickEvent event) -> { BroadcasterOffGame.broadcast(input.getValue(), FROM); input.setValue(""); }); sendBar.addComponent(send); this.addComponent(sendBar); BroadcasterOffGame.register(this); } @Override public void receiveBroadcast(String from, String message) { UI.getCurrent().access(new Runnable() { @Override public void run() { if (from.equals(FROM)) { messages.addComponent(new Label(message)); } } }); } }