当我点击按钮(" backBtn")时,我希望将列添加到网格中。然后我从文本字段中获取值(" filterText"),这将是新列的名称。谁能帮我?代码来自教程,但我需要在这里添加新功能。谢谢 !您可以在附件中找到我的代码。网格在课堂上" MyUI"
这是clas Customer
package my.vaadin.app;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Date;
/**
* A entity object, like in any other Java application. In a typical real world
* application this could for example be a JPA entity.
*/
@SuppressWarnings("serial")
public class Customer implements Serializable, Cloneable {
private Long id;
private String firstName = "";
private String datum = "";
private String lastName = "";
private LocalDate birthDate;
private CustomerStatus status;
private String email = "";
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
* Get the value of email
*
* @return the value of email
*/
public String getEmail() {
return email;
}
/**
* Set the value of email
*
* @param email
* new value of email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Get the value of status
*
* @return the value of status
*/
public CustomerStatus getStatus() {
return status;
}
/**
* Set the value of status
*
* @param status
* new value of status
*/
public void setStatus(CustomerStatus status) {
this.status = status;
}
/**
* Get the value of birthDate
*
* @return the value of birthDate
*/
public LocalDate getBirthDate() {
return birthDate;
}
/**
* Set the value of birthDate
*
* @param birthDate
* new value of birthDate
*/
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
/**
* Get the value of lastName
*
* @return the value of lastName
*/
public String getLastName() {
return lastName;
}
/**
* Set the value of lastName
*
* @param lastName
* new value of lastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Get the value of firstName
*
* @return the value of firstName
*/
public String getFirstName() {
return firstName;
}
/**
* Set the value of firstName
*
* @param firstName
* new value of firstName
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getDatum() {
return datum;
}
/**
* Set the value of firstName
*
* @param firstName
* new value of firstName
*/
public void setDatum(String datum) {
this.datum = datum;
}
public boolean isPersisted() {
return id != null;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (this.id == null) {
return false;
}
if (obj instanceof Customer && obj.getClass().equals(getClass())) {
return this.id.equals(((Customer) obj).id);
}
return false;
}
@Override
public int hashCode() {
int hash = 5;
hash = 43 * hash + (id == null ? 0 : id.hashCode());
return hash;
}
@Override
public Customer clone() throws CloneNotSupportedException {
return (Customer) super.clone();
}
@Override
public String toString() {
return firstName + " " + lastName;
}
}
这是clas MyUI
package my.vaadin.app;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.swing.text.TableView;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.shared.ui.ValueChangeMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.components.grid.HeaderCell;
import com.vaadin.ui.components.grid.HeaderRow;
import com.vaadin.ui.themes.ValoTheme;
/**
* This UI is the application entry point. A UI may either represent a browser window
* (or tab) or some part of a html page where a Vaadin application is embedded.
* <p>
* The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
* overridden to add component to the user interface and initialize non-component functionality.
*/
@Theme("mytheme")
public class MyUI extends UI {
private CustomerService service = CustomerService.getInstance();
private Grid<Customer> grid = new Grid<>(Customer.class);
private TextField filterText = new TextField();
// private CustomerForm form = new CustomerForm(this);
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
Label tancore = new Label();
filterText.setPlaceholder("Meno ...");
//filterText.addValueChangeListener(e -> updateList());
//filterText.setValueChangeMode(ValueChangeMode.LAZY);
Button clearFilterTextBtn = new Button(FontAwesome.TIMES);
clearFilterTextBtn.setDescription("Clear the current name");
clearFilterTextBtn.addClickListener(e -> filterText.clear());
tancore.setCaption("TanCore s.r.o");
CssLayout filtering = new CssLayout();
filtering.addComponents(filterText, clearFilterTextBtn);
filtering.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
Button addCustomerBtn = new Button("Pridaj zamestnanca");
Button downloadXlsBtn = new Button("Stiahnuť ako .xls");
Button loginBtn = new Button("Prihlásiť sa");
// Button addEmplBtn = new Button("Pridaj");
Button backBtn = new Button("Vlož");
downloadXlsBtn.setVisible(false);
TextField name = new TextField();
PasswordField pass = new PasswordField();
// addEmplBtn.setVisible(false);
addCustomerBtn.setVisible(false);
backBtn.setVisible(false);
filtering.setVisible(false);
clearFilterTextBtn.setVisible(false);
filterText.setVisible(false);
addCustomerBtn.setStyleName(ValoTheme.BUTTON_PRIMARY);
//.setStyleName(ValoTheme.BUTTON_PRIMARY);
addCustomerBtn.setClickShortcut(KeyCode.ENTER);
backBtn.addClickListener(e -> {
//Here i want include the new data after click on the button
//grid.addColumn(filterText.getValue()); -> That it's not good, because after click the Java will Warning you
addCustomerBtn.setVisible(true);
backBtn.setVisible(false);
filtering.setVisible(false);
clearFilterTextBtn.setVisible(false);
filterText.setVisible(false);
});
addCustomerBtn.addClickListener(e -> {
addCustomerBtn.setVisible(false);
filtering.setVisible(true);
backBtn.setVisible(true);
clearFilterTextBtn.setVisible(true);
filterText.setVisible(true);
//addEmplBtn.setEnabled(true);
});
loginBtn.addClickListener(e -> {
if(name.getValue().equals("admin"))
{
if(pass.getValue().equals("admin"))
{
name.setVisible(false);
pass.setVisible(false);
loginBtn.setVisible(false);
addCustomerBtn.setVisible(true);
downloadXlsBtn.setVisible(true);
}
}
});
pass.setPlaceholder("Heslo ...");
name.setPlaceholder("Meno ...");
HorizontalLayout toolbar = new HorizontalLayout(name, pass, loginBtn,filtering, addCustomerBtn,backBtn,downloadXlsBtn);
// grid.setSelectionMode(SelectionMode.MULTI);
grid.setColumns("datum", "lastName","email","status");
// grid.setStyleName(ValoTheme.BUTTON_PRIMARY);
/*HeaderRow extraHeader = grid.prependHeaderRow();
HeaderCell joinedCell = extraHeader.join("datum", "lastName");
joinedCell.setText("Joined cell");*/
HorizontalLayout main = new HorizontalLayout(grid);
main.setSizeFull();
grid.setSizeFull();
// main.setExpandRatio(grid, 1);
grid.getColumn("datum").setWidth(100);
// grid.getColumn("datum").set
// grid.getColumn("datum").set
grid.getColumn("datum").setCaption("Dátum");
grid.getColumn("lastName").setCaption("Adam");
layout.addComponents(toolbar, main);
// fetch list of Customers from service and assign it to Grid
updateList();
setContent(layout);
// form.setVisible(false);
grid.asSingleSelect().addValueChangeListener(event -> {
//if (event.getValue() == null) {
// form.setVisible(false);
//} else {
// form.setCustomer(event.getValue());
//}
});
}
public void updateList() {
List<Customer> customers = service.findAll(filterText.getValue());
grid.setItems(customers);
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
这是clas CustomerForm.java
package my.vaadin.app;
import com.vaadin.data.Binder;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.DateField;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.ValoTheme;
public class CustomerForm extends FormLayout {
private TextField firstName = new TextField("First name");
private TextField lastName = new TextField("Last name");
private TextField email = new TextField("Email");
private NativeSelect<CustomerStatus> status = new NativeSelect<>("Status");
private DateField birthdate = new DateField("Birthday");
private Button save = new Button("Save");
private Button delete = new Button("Delete");
private CustomerService service = CustomerService.getInstance();
private Customer customer;
private MyUI myUI;
private Binder<Customer> binder = new Binder<>(Customer.class);
public CustomerForm(MyUI myUI) {
this.myUI = myUI;
setSizeUndefined();
HorizontalLayout buttons = new HorizontalLayout(save, delete);
addComponents(firstName, lastName, email, status, birthdate, buttons);
status.setItems(CustomerStatus.values());
save.setStyleName(ValoTheme.BUTTON_PRIMARY);
save.setClickShortcut(KeyCode.ENTER);
binder.bindInstanceFields(this);
save.addClickListener(e -> this.save());
delete.addClickListener(e -> this.delete());
}
public void setCustomer(Customer customer) {
this.customer = customer;
binder.setBean(customer);
// Show delete button for only customers already in the database
delete.setVisible(customer.isPersisted());
setVisible(true);
firstName.selectAll();
}
private void delete() {
service.delete(customer);
myUI.updateList();
setVisible(false);
}
private void save() {
service.save(customer);
myUI.updateList();
setVisible(false);
}
}
这是clas CustomerService.java
package my.vaadin.app;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* An in memory dummy "database" for the example purposes. In a typical Java app
* this class would be replaced by e.g. EJB or a Spring based service class.
* <p>
* In demos/tutorials/examples, get a reference to this service class with
* {@link CustomerService#getInstance()}.
*/
public class CustomerService {
private static CustomerService instance;
private static final Logger LOGGER = Logger.getLogger(CustomerService.class.getName());
private final HashMap<Long, Customer> contacts = new HashMap<>();
private long nextId = 0;
private CustomerService() {
}
/**
* @return a reference to an example facade for Customer objects.
*/
public static CustomerService getInstance() {
if (instance == null) {
instance = new CustomerService();
instance.ensureTestData();
}
return instance;
}
/**
* @return all available Customer objects.
*/
public synchronized List<Customer> findAll() {
return findAll(null);
}
/**
* Finds all Customer's that match given filter.
*
* @param stringFilter
* filter that returned objects should match or null/empty string
* if all objects should be returned.
* @return list a Customer objects
*/
public synchronized List<Customer> findAll(String stringFilter) {
ArrayList<Customer> arrayList = new ArrayList<>();
for (Customer contact : contacts.values()) {
try {
boolean passesFilter = (stringFilter == null || stringFilter.isEmpty())
|| contact.toString().toLowerCase().contains(stringFilter.toLowerCase());
if (passesFilter) {
arrayList.add(contact.clone());
}
} catch (CloneNotSupportedException ex) {
Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
}
}
Collections.sort(arrayList, new Comparator<Customer>() {
@Override
public int compare(Customer o1, Customer o2) {
return (int) (o2.getId() - o1.getId());
}
});
return arrayList;
}
/**
* Finds all Customer's that match given filter and limits the resultset.
*
* @param stringFilter
* filter that returned objects should match or null/empty string
* if all objects should be returned.
* @param start
* the index of first result
* @param maxresults
* maximum result count
* @return list a Customer objects
*/
public synchronized List<Customer> findAll(String stringFilter, int start, int maxresults) {
ArrayList<Customer> arrayList = new ArrayList<>();
for (Customer contact : contacts.values()) {
try {
boolean passesFilter = (stringFilter == null || stringFilter.isEmpty())
|| contact.toString().toLowerCase().contains(stringFilter.toLowerCase());
if (passesFilter) {
arrayList.add(contact.clone());
}
} catch (CloneNotSupportedException ex) {
Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
}
}
Collections.sort(arrayList, new Comparator<Customer>() {
@Override
public int compare(Customer o1, Customer o2) {
return (int) (o2.getId() - o1.getId());
}
});
int end = start + maxresults;
if (end > arrayList.size()) {
end = arrayList.size();
}
return arrayList.subList(start, end);
}
/**
* @return the amount of all customers in the system
*/
public synchronized long count() {
return contacts.size();
}
/**
* Deletes a customer from a system
*
* @param value
* the Customer to be deleted
*/
public synchronized void delete(Customer value) {
contacts.remove(value.getId());
}
/**
* Persists or updates customer in the system. Also assigns an identifier
* for new Customer instances.
*
* @param entry
*/
public synchronized void save(Customer entry) {
if (entry == null) {
LOGGER.log(Level.SEVERE,
"Customer is null. Are you sure you have connected your form to the application as described in tutorial chapter 7?");
return;
}
if (entry.getId() == null) {
entry.setId(nextId++);
}
try {
entry = (Customer) entry.clone();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
contacts.put(entry.getId(), entry);
}
/**
* Sample data generation
*/
public void ensureTestData() {
if (findAll().isEmpty()) {
for (int i = 31; i > 0; i--) {
Customer c = new Customer();
c.setDatum(i+".7");
save(c);
}
}
}
}
我希望在我的WebApp中有这个
这是我的WebApp