Reputation: 1
I am building a table in SWT that requires one column to be editable as a text field. I have taken the examples from http://www.java2s.com/ and coded them into my application. In OSX Yosemite this works: when clicking on the table cell, the cell becomes a text box and behaves like a text box.
When the same code is used in Ubuntu 14.04 (OpenJDK 8), the text box does not appear, but editing process works underneath. To test that it was not my version of the example, I have run the example verbatim in this environment, and it does exactly the same thing. The code from the example is:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setLinesVisible(true);
for (int i = 0; i < 3; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(100);
}
for (int i = 0; i < 3; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "" + i, "" + i, "" + i });
}
final TableEditor editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
Rectangle clientArea = table.getClientArea();
Point pt = new Point(event.x, event.y);
int index = table.getTopIndex();
while (index < table.getItemCount()) {
boolean visible = false;
final TableItem item = table.getItem(index);
for (int i = 0; i < table.getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
final int column = i;
final Text text = new Text(table, SWT.NONE);
Listener textListener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut:
item.setText(column, text.getText());
text.dispose();
break;
case SWT.Traverse:
switch (e.detail) {
case SWT.TRAVERSE_RETURN:
item
.setText(column, text
.getText());
//FALL THROUGH
case SWT.TRAVERSE_ESCAPE:
text.dispose();
e.doit = false;
}
break;
}
}
};
text.addListener(SWT.FocusOut, textListener);
text.addListener(SWT.Traverse, textListener);
editor.setEditor(text, item, i);
text.setText(item.getText(i));
text.selectAll();
text.setFocus();
text.redraw(); text.update();
if (text.isFocusControl())
System.out.println("focussed");
return;
}
if (!visible && rect.intersects(clientArea)) {
visible = true;
}
}
if (!visible)
return;
index++;
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Upvotes: 0
Views: 316
Reputation: 8849
Change the Text box style. Replace line final Text text = new Text(table, SWT.NONE);
to final Text text = new Text(table, SWT.BORDER);
When used line final Text text = new Text(table, SWT.NONE);
it looks:
When used line final Text text = new Text(table, SWT.BORDER);
it looks:
Upvotes: 1