I couldn't find anything on this - so I wanted to post a simple question. I want to create a custom ComboBox that displays a combination of the fields in when extended. The code below correctly displays my list, and reacts to keyboard arrows, but when I select the arrow from the dropdown, the list has no reaction to the mouse click for selection.
[EDIT]Also - I can't seem to get type ahead working on this either.
I am not sure what I am missing.
public class JavaFXApplication7 extends Application {
#Override
public void start(Stage primaryStage) {
ComboBox<Person> cb = new ComboBox();
cb.setCellFactory(column -> new ListCell<Person>() {
private HBox graphic = new HBox();
private Label label1 = new Label() ;
private Label label2 = new Label(" || ");
private Label label3 = new Label();
{
graphic.getChildren().addAll(label1, label2, label3);
}
#Override
public void updateItem(Person person, boolean empty) {
if (person == null || empty) {
setGraphic(null);
} else {
label1.setText(person.firstName.get());
label3.setText(person.email.get());
setGraphic(graphic);
}
}
});
ObservableList<Person> items = FXCollections.observableArrayList();
Person p = new Person();
p.email.set("foo#bar.com");
p.firstName.set("Tony");
p.lastName.set("Stark");
p.phone.set("(555) 555-1212");
items.add(p);
Person p2 = new Person();
p2.email.set("foo#bar.com");
p2.firstName.set("Bruce");
p2.lastName.set("Wayne");
p2.phone.set("(444) 444-1212");
items.add(p2);
cb.setItems(items);
StackPane root = new StackPane();
root.getChildren().add(cb);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public class Person {
StringProperty firstName = new SimpleStringProperty();
StringProperty lastName = new SimpleStringProperty();
StringProperty phone = new SimpleStringProperty();
StringProperty email = new SimpleStringProperty();
#Override
public String toString () {
return firstName.get() + " " + lastName.get();
}
}
}
You're missing
super.updateItem(person, empty);
in the updateItem method.
Related
I tried to run my Eclipse RCP code to run in Eclipse RAP environment. In my Eclipse RCP code, there is functionality to add the rows in to table. But
adding the code does not work in Eclipse RAP. I am using TableViewer.
Following is my code.
public class BasicEntryPoint extends AbstractEntryPoint {
private static final int COLUMNS = 2;
private TableViewer viewer;
private class ViewContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
List<Person> list = (List<Person>) inputElement;
return list.toArray();
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
private class ViewLabelProvider extends LabelProvider implements
ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
Person p = (Person) element;
if (columnIndex == 0) {
return p.getName();
}
return p.getPlace();
}
}
private class Person{
String name;
String place;
public void setName(String name) {
this.name = name;
}
public void setPlace(String place) {
this.place = place;
}
public String getName() {
return name;
}
public String getPlace() {
return place;
}
}
public List<Person> persons() {
List<Person> list = new ArrayList<Person>();
Person person = new Person();
person.setName("bb");
person.setPlace("jjj");
list.add(person);
person = new Person();
person.setName("sss");
person.setPlace("fff");
list.add(person);
return list;
}
#Override
protected void createContents(Composite parent) {
parent.setLayout(new GridLayout(2, false));
viewer = new TableViewer(parent, SWT.NONE);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
final Table table = viewer.getTable();
viewer.setColumnProperties(initColumnProperties(table));
viewer.setInput(persons());
viewer.getTable().setHeaderVisible(true);
Button checkbox = new Button(parent, SWT.CHECK);
checkbox.setText("Hello");
Button button = new Button(parent, SWT.PUSH);
button.setText("World");
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
System.out.println("Button clicked");
Person p = new Person();
p.setName("Dee");
p.setPlace("TCR");
persons().add(p);
String prop[] ={"name","place"};
viewer.update(p, prop);
//viewer.refresh();
}
});
}
private String[] initColumnProperties(Table table) {
String[] result = new String[COLUMNS];
for (int i = 0; i < COLUMNS; i++) {
TableColumn tableColumn = new TableColumn(table, SWT.NONE);
result[i] = "Column" + i;
tableColumn.setText(result[i]);
if (i == 2) {
tableColumn.setWidth(190);
} else {
tableColumn.setWidth(70);
}
}
return result;
}
}
You should use:
viewer.add(p);
rather than update to add a new item to a table (both for SWT and RAP).
You must also update your model to contain the new item.
I have the following main class:
public class JavaFXApplication4 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Add to the list");
TextArea peopleList = new TextArea();
String name = "name";
String surname = "surname";
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Person p = new Person(name, surname);
}
});
StackPane root = new StackPane();
root.getChildren().addAll(btn, peopleList);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("People list");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
I want that the constructor of the class Personadds name and surname into the TextArea peopleList into the main class.
public class Person {
public String name, surname;
public Person(String n, String s){
}
}
How can I access elements of the main class from other classes?
Make those variables either instance variables or class variables , Since local variable can be accessed only inside the function in which they are declared .
if you use instance variable make them private and Use getters and setters to access them
private TextArea peopleList = new TextArea();
public TextArea getPeopleList(){ // getter to access
return peopleList;
}
public void setPeopleList(TextArea peopleList){
this.peopleList=peopleList;
}
Made the object of MainClass in other class and use them using getters and setters.
Or Made them static/class variables in Main class so can be accesed with the name of Main Class.
static TextArea peopleList = new TextArea();
and in Other class
MainClass.peopleList
i have been trying to implement datagrid in my project , the problem is that the data grid does not show data , the data is there but is not visible , i tried to inspect the html page and with adding some height and width to the inner divs inside the datagrid parent div the data is displayed perfectly , i can do this using css and its done , but if there is another solution or in other words anyone knows whats wrong with datagrid?
public class DataGridTest implements EntryPoint, ClickHandler {
private ListDataProvider<TestObject> listProvider;
public void onModuleLoad() {
List<TestObject> list = new ArrayList<TestObject>();
list.add(new TestObject("Amer",22));
list.add(new TestObject("Essa",25));
list.add(new TestObject("Asem",29));
DataGrid<TestObject> dataGrid = new DataGrid<TestObject>();
dataGrid.setStyleName("dataGridTest");
TextColumn<TestObject> nameColumn = new TextColumn<TestObject>(){
#Override
public String getValue(TestObject object) {
return object.getName();
}
};
dataGrid.addColumn(nameColumn,"Name");
TextColumn<TestObject> ageColumn = new TextColumn<TestObject>(){
#Override
public String getValue(TestObject object) {
return object.getAge()+"";
}
};
dataGrid.addColumn(ageColumn,"Age");
listProvider = new ListDataProvider<TestObject>();
listProvider.addDataDisplay(dataGrid);
listProvider.setList(list);
LayoutPanel layoutPanel = new LayoutPanel();
layoutPanel.add(dataGrid);
RootPanel.get().add(layoutPanel);
layoutPanel.forceLayout();
Button btn = new Button("lala");
btn.getElement().getStyle().setPosition(Position.ABSOLUTE);
RootPanel.get().add(btn);
btn.addClickHandler(this);
}
public class TestObject {
private String name;
private int age;
public TestObject(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
}
#Override
public void onClick(ClickEvent event) {
listProvider.getList().add(new TestObject("lala", 10));
}
}
You are mixing RootPanel and LayoutPanel, which doesn't work.
You must use RootLayoutPanel instead.
The DataGrid has to be put in a LayoutPanel and there must an unbroken chain up to the RootLayoutPanel.
Alternatively you have to specify explicit dimensions (height, width) on the parent container of your DataGrid or use a CellTable instead.
RootLayoutPanel.get().add(layoutPanel) should fix your issue.
I have a cell table in one of my GWT screen. Now requirement is that there will be some button on the screen clicking on which one editable row will be inserted into the cell table. if I am not wrong this is called inline editing.
Can somebody please help me with that? I have just started working in GWT.
Here is very simple example of adding new editable row to the celltable. I hope I understand what you meant :)
public class Test
implements EntryPoint
{
public void onModuleLoad()
{
//create and attach table
final CellTable<A> table = new CellTable<A>();
RootPanel.get().add(table);
//add editable cell
table.addColumn(new Column<A, String>(new TextInputCell())
{
#Override
public String getValue(
A object)
{
return object.getA() == null ? "" : object.getA();
}
});
//put some data
ListDataProvider<A> dataProvider = new ListDataProvider<A>(
Arrays.asList(new A("AAA"), new A("BBB"), new A("CCC")));
dataProvider.addDataDisplay(table);
//add button with click handler
RootPanel.get().add(new Button("add new", new ClickHandler()
{
#Override
public void onClick(
ClickEvent event)
{
//add new object to visible items
List<A> data = new ArrayList<A>(table.getVisibleItems());
A newOne = new A();
data.add(newOne);
table.setRowData(data);
}
}));
}
class A
{
String a;
public A(
String a)
{
super();
this.a = a;
}
public A()
{
super();
}
public String getA()
{
return a;
}
public void setA(
String a)
{
this.a = a;
}
}
I have the following cell table. I've added a button to every row. when it is clicked the new data should be updated.
But when I click update, the old information about the Contact object is being passed to the updateContact method. How Do I get the edited text entered by the user
following is my code:
public class CellTableExample implements EntryPoint {
/**
* A simple data type that represents a contact.
*/
private static class Contact {
private final String address;
private final Date birthday;
private final String name;
public Contact(String name, Date birthday, String address) {
this.name = name;
this.birthday = birthday;
this.address = address;
}
}
/**
* The list of data to display.
*/
#SuppressWarnings("deprecation")
private static final List<Contact> CONTACTS = Arrays.asList(
new Contact("John", new Date(80, 4, 12), "123 Abc Avenue"),
new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln fasfasdfasfdasdfasfasdfasfasdfasfasfasdfasdfasdf"),
new Contact("Tom", new Date(85, 3, 22), "33 Lance Lnasdfasfdasdfffffffffffffffffff"),
new Contact("Jack", new Date(85, 4, 22), "44 Lance Lnsddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"));
public void onModuleLoad() {
// Create a CellTable.
final CellTable<Contact> table = new CellTable<Contact>();
// Display 3 rows in one page
table.setPageSize(3);
// Add a text column to show the name.
TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
#Override
public String getValue(Contact object) {
return object.name;
}
};
table.addColumn(nameColumn, "Name");
// Add a date column to show the birthday.
DateCell dateCell = new DateCell();
Column<Contact, Date> dateColumn = new Column<Contact, Date>(dateCell) {
#Override
public Date getValue(Contact object) {
return object.birthday;
}
};
table.addColumn(dateColumn, "Birthday");
// Add a text column to show the address.
TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
#Override
public String getValue(Contact object) {
return object.address;
}
};
table.addColumn(addressColumn, "Address");
ButtonCell updateButton= new ButtonCell();
Column <Contact,String> update= new Column <Contact,String>(updateButton)
{
#Override
public String getValue(Contact c)
{
return "Update";
}
};
update.setFieldUpdater(new FieldUpdater<Contact,String>()
{
#Override
public void update(int index, Contact c,String value)
{
SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
AsyncCallback callback = new AsyncCallback()
{
#Override
public void onFailure(Throwable caught)
{}
#Override
public void onSuccess(Object result)
{
}
};
service.updateContact(c.id, c.name, callback);
}
});
provider.addDataDisplay(table);
provider.updateRowCount(CONTACTS.size(), true);
SimplePager pager = new SimplePager();
pager.setDisplay(table);
VerticalPanel vp = new VerticalPanel();
vp.add(table);
vp.add(pager);
// Add it to the root panel.
RootPanel.get().add(vp);
}
}
Found the solution, all I had to do was the following to each column:
colName.setFieldUpdater(new FieldUpdater<Contact,String>()
{
public void update(int index, Contact c, String value)
{
comp.name=value;
}
});
When you update the value it is updated in the variable but not shown in the table. To show the updated data you have to use:
cellTable.redraw();
The redraw function renders the table with the updated data.