Вы очень близко.
Переопределить getCellStyleNames
в новом Column() {} — это первая половина решения.
Вторая половина:
yourColumn.setFieldUpdater(new FieldUpdater<DataType, String>() {
@Override
public void update(int index, DataType object, String value) {
// the following line will apply the correct css
// based on the current cell value
cellTable.redrawRow(index);
}
});
Надеюсь, это поможет!
Следующий код является простым, но полным примером.
Определена таблица ячеек с двумя столбцами. В каждой ячейке первого столбца отображается простой вопрос. Каждая ячейка во втором столбце является редактируемой ячейкой, которая позволяет вам ввести ответ на вопрос, показанный в первом столбце. Если ваш ответ правильный, то текст ответа будет выделен жирным шрифтом и черным цветом. В противном случае текст будет окрашен в красный цвет с обычным шрифтом.
Исходный код приложения Trivia GWT:
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.RootPanel;
import java.util.Arrays;
import java.util.List;
public class CellTableExample implements EntryPoint {
private static class Question {
private final String question;
private final String correctAnswer;
private String userProvidedAnswer;
public Question(String question, String correctAnswer) {
this.question = question;
this.correctAnswer = correctAnswer;
this.userProvidedAnswer = "";
}
public String getQuestion() {
return question;
}
public String getCorrectAnswer() {
return correctAnswer;
}
public String getUserProvidedAnswer() {
return userProvidedAnswer;
}
public void setUserProvidedAnswer(String userProvidedAnswer) {
this.userProvidedAnswer = userProvidedAnswer;
}
}
private static final List<Question> questionList = Arrays.asList(
new Question("Which city is capital of England?", "London"),
new Question("Which city is capital of Japan?", "Tokyo"));
@Override
public void onModuleLoad() {
final CellTable<Question> cellTable = new CellTable<>();
TextColumn<Question> questionCol = new TextColumn<Question>() {
@Override
public String getValue(Question object) {
return object.getQuestion();
}
};
Column<Question, String> ansCol = new Column<Question, String>(new EditTextCell()) {
@Override
public String getValue(Question object) {
return object.getUserProvidedAnswer();
}
@Override
public String getCellStyleNames(Cell.Context context, Question object) {
if (object.getUserProvidedAnswer().equalsIgnoreCase(object.getCorrectAnswer())) {
return "correct-answer";
} else {
return "wrong-answer";
}
}
};
ansCol.setFieldUpdater(new FieldUpdater<Question, String>() {
@Override
public void update(int index, Question object, String value) {
object.setUserProvidedAnswer(value);
cellTable.redrawRow(index);
}
});
cellTable.addColumn(questionCol, "Question");
cellTable.addColumn(ansCol, "Your Answer");
cellTable.setRowData(0, questionList);
RootPanel.get().add(cellTable);
}
}
Сопутствующий css-файл:
.correct-answer {
font-weight: bold;
color: black;
}
.wrong-answer {
font-weight: normal;
color: red;
}
Скриншот 1: Сразу после запуска приложения. Столбец ответов был пуст.
![введите описание изображения здесь](https://i.stack.imgur.com/U3YZJ.png)
Скриншот 2: После того, как я ввел ответы. Видимо, я правильно ответил на первый вопрос, но не на второй.
![введите описание изображения здесь](https://i.stack.imgur.com/nVQZi.png)
person
dydigging
schedule
24.10.2016