How to right-align text in a SWT Table cell? - java

I have a SWT table which wrapped by the JFace TableViewer, but this problem also applies to org.eclipse.swt.widgets.Table.
When I use a StyledCellLabelProvider, the text is always left aligned, even when I use
colA.getColumn().setAlignment(SWT.RIGHT);
Here is the label provider and setup:
TableViewerColumn colA = new TableViewerColumn(measureTable, SWT.NONE);
colA.setLabelProvider(new StyledCellLabelProvider() {
#Override
public void update(ViewerCell cell) {
ModelItem item = (ModelItem) cell.getElement();
cell.setFont(FONT_REGISTRY.get(MY_SPECIAL_FONT));
cell.setText(item.getText());
super.update(cell);
}
});
Any sort of workaround would be great. For e.g, nesting a widget inside the table and right aligning the text in the widget somehow.
Platform: Windows 7

You found a bug in StyledCellLabelProvider. It will not occur with any other CellLabelProvider.
StyledCellLabelProvider uses "owner draw" for drawing the Table cells. That means, the cell content is not drawn natively by the OS. It is drawn in an SWT.PaintItem event by the Table "owner".
StyledCellLabelProvider does not respect the alignment of the TableColumn. You can see the source here, the method getTextLayoutForInfo(.) is of interest.
A workaround could be to copy that class, fix the bug by adding
TableColumn col = ((Table)viewer.getControl()).getColumn(cell.getColumnIndex());
layout.setAlignment(col.getAlignment());
in the method getTextLayoutForInfo(.) (I didn't test this fix, but if it doesn't work, you should get the idea, and be able to make it work)
You should also add a bug report: Eclipse Bugzilla

Related

How to correct a display problem when inserting a row in a JavaFX grid?

I want to insert a new JavaFX bean in a grid using an "insert" button. Everything is fine, except for a display problem. After insertion, a "ghost selection" is displayed lower in the grid, as shown in this screenshot. In this example, a fourth section bean was added and selected as requested. But a fake selection appears 10 lines under the last real bean, where no bean is set for this row.
Has anyone experienced this kind of behavior? Any clue how get rid of this ghost selection? Here is what the code for the insert button looks like:
#FXML
private Button insert;
...
insert.setOnAction(event -> {
JfxBean newBean = createBean();
tableView.getItems().add(newBean);
int index = tableView.getItems().indexOf(newBean);
tableView.getSelectionModel().clearSelection();
tableView.requestFocus();
tableView.scrollTo(index);
tableView.getSelectionModel().focus(index);
tableView.getSelectionModel().select(index);
};
According to javaFX-8 documentation, SelectionModel.java does not expose any focus() method. FocusModel.java does instead. Therefore JVM will fail to compile your presented code.
Below is a possible solution:
insert.setOnAction(event -> {
JfxBean newBean = createBean();
tableView.getItems().add(newBean);
int index = tableView.getItems().indexOf(newBean);
tableView.getSelectionModel().clearSelection();
tableView.requestFocus();
tableView.scrollTo(index);
// below line is the amendment
tableView.getFocusModel().focus(index);
tableView.getSelectionModel().select(index);
};
Finally, adding tableView.refresh() corrected this weird behavior. No more ghost selection.

Vaadin 8 grid editor doesn't work correctly

Vaadin 8 editor of grid doesn't work correctly when I double click on cell that I can edit.
I use the simple code for create grid and add for one column editor component.
VerticalLayout content = new VerticalLayout();
content.setMargin(false);
content.setSpacing(false);
content.setSizeFull();
setContent(content);
Grid<OrderModel> grid = new Grid(OrderModel.class);
grid.setItems(generate());
grid.setSizeFull();
grid.getEditor().setEnabled(true);
grid.getColumn("planningStatus").setEditorComponent(getCombobox());
Next I run app and start to scroll grid till column "planningStatus".
How it works:
So. What should I do or how I should fix it for correct open editor in grid?
Could your problem be related to issue reported here: https://github.com/vaadin/framework/issues/7746 ?
There exists now also add-on which fixes couple of known Editor cell sizing issues:

What is the org.eclipse.swt.widgets.Table; equivalent for jTable1.setValueAt?

I've moved from using NetBeans to Eclipse.
And as such lines like the below don't work, as I've moved from using:
JTables to SWT Tables
How can I modify this line of code to work for SWT Tables:
jTable1.setValueAt(v.getDate(), nCount+1, 0);
Assuming you are just using Table (and not the JFace TableViewer) you would use:
TableItem item = table.getItem(row);
item.setText("text");
if you have more than one column use:
item.setText(column, "text");

Items decorations in a TreeViewer

I have the following problem:
I'm preparing an editor in Eclipse and one of the tab contains TreeViewer to show items in the tree. Each item has a name and a value, which is editable.
The problem I need to indicate to user that value is incorrect (e.g. exceeds a given range). My idea is to decorate incorrect cells with a warning or error icon which will be shown also after editing is complete.
Does anybody have an idea how to decorate items in the tree? I was experimenting with ControlDecoration class but without success.
Thanks in advance,
Marcin
PS. I'm limited to Eclipse 3.4
There are two ways that this can be done. If your TreeViewer displays objects that are instances of EObject (generated by EMF. If your don't understand this part, skip to the next paragraph :)), you can change these EObject's "XyzItemProvider" so that their "getImage" method return a decorated image instead of the "plain" image... and that's it for EMF objects, nothing else needs to be changed.
If you're displaying "classic" Java Objects, you'll have to change your TreeViewer's LabelProvider in order to decorate the Image. This is done through the TreeViewer#setLabelProvider() method.
What you will need then is "how to decorate an Image", which is done through code such as this :
public class MyLabelProvider extends DecoratingLabelProvider {
public Image getImage(Object element) {
Image image = super.getImage(element);
List<Object> images = new ArrayList<Object>(2);
images.add(image);
images.add(<Image of the decorator>);
labelImage = new ComposedImage(images); // This will put the second of the "images" list (the decorator) above the first (the element's image)
return decoratedImage;
}
[...]
}
You then need to give your tree viewer this label provider :
TreeViewer treeViewer = new TreeViewer(...);
treeViewer.setLabelProvider(new MyLabelProvider(new LabelProvider()); // new LabelProvider()... or your previous label provider if you have one.

Can't navigate a JTable by the arrow keys after using setRowSelectionInterval(int, int)

I'm extending JTable and I have a populateData() method that I'm using to load data into the table model. In this method I'm using:
setRowSelectionInterval(tableRow, tableRow);
And it is working as I want it to, which is to highlight the first row. But there is one problem: I cannot move the selection to the next row by the down arrow key. I have to click on any row (even the one that is highlighted) with the mouse to be able to navigate by the arrow keys.
void populateData(Collection<Book> b) {
myModel.populateData(b);
if (myModel.getRowCount() > 0) {
setRowSelectionInterval(0, 0);
}
}
Note: I'm enabling only row selections. Column and cell selections are disabled.
Any ideas how to fix this?
Thanks in advance.
I would guess focus in not on the table so it doesn't react to key presses. You may need to add:
table.requestFocusInWindow();
If this doesn't work the instead of setRowSelectionInterval(...) try using:
table.changeSelection(0, 0, false, false);

Categories