JFace tableviewer item delete cannot refresh - java

I'm a newer in SWT and JFace, recently, I used JFace tableviewer in my project. And I need to delete items by a delete button. But it doesn't work if i refresh the tableviewer after deleting it. I want to know the reason.My code shows below:
btnDeleteConstraint.addSelectionListener(
new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
ISelection selection = tableViewer.getSelection();
logger.debug("datatype selected");
if (selection != null || selection instanceof
IStructuredSelection) {
IStructuredSelection sel = (IStructuredSelection) selection;
Iterator iterator = sel.iterator();
while(iterator.hasNext()) {
Object obj = iterator.next();
tableViewer.remove(obj);
}
tableViewer.refresh();
}
}
});
And i use another method called update() to set input and refresh the table in the end:
public void update()
{
tableViewer.setInput(DataTypeFactory.
getInstance().getCastList(wizard.getSourceInfo().getDBType()));
tableViewer.refresh();
}

When you call refresh the table is updated from your 'content provider' - so when you delete things you must update the data that your content provider returns in its getElements method.

Related

JFace TreeViewer expand or collapse on selection

I've implemented a selection listener for my treeviewer to expand or collapse a node on selection.
This impementation works fine for collapsing, but does not expand a node.
this.getTree().addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
TreeItem treeItem = (TreeItem) event.item;
if (treeItem.getItems().length > 0) {
if (MyTreeViewer.this.getExpandedState(treeItem)) {
MyTreeViewer.this.collapseToLevel(treeItem, MyTreeViewer.this.ALL_LEVELS);
} else {
MyTreeViewer.this.expandToLevel(treeItem, 1);
}
MyTreeViewer.this.refresh();
}
}
});
Do you have any suggestions how to fix this?
For a JFace TreeViewer you should use a ISelectionChangedListener or a IDoubleClickListener - do not use the underlying Tree listeners as they may not interact correctly with the viewer.
This is what I use for double click:
public class TreeDoubleClickListener implements IDoubleClickListener
{
#Override
public void doubleClick(final DoubleClickEvent event)
{
IStructuredSelection selection = (IStructuredSelection)event.getSelection();
if (selection == null || selection.isEmpty())
return;
Object sel = selection.getFirstElement();
TreeViewer treeViewer = (TreeViewer)event.getViewer();
IContentProvider provider = treeViewer.getContentProvider();
if (provider instanceof ITreeContentProvider)
{
ITreeContentProvider treeProvider = (ITreeContentProvider)provider;
if (!treeProvider.hasChildren(sel))
return;
if (treeViewer.getExpandedState(sel))
treeViewer.collapseToLevel(sel, AbstractTreeViewer.ALL_LEVELS);
else
treeViewer.expandToLevel(sel, 1);
}
}
}
The key thing here is to use the selection as the argument to collapseToLevel / expandToLevel.
Just change to implement ISelectionChangedListener to work on selection.
Add the listener with TreeViewer addDoubleClickListener or addSelectionChangedListener

vaadin delete row on keyboard event

How to handle keyboard event on the selected row in the table?
I mean, for example, I selected a row and I want to delete using keyboard button delete.
How to do it? What listener I should use?
You need to use ShortcutListener:
table.setSelectable(true);
table.addShortcutListener(new ShortcutListener("", KeyCode.DELETE, new int[10])
{
#Override
public void handleAction(Object sender, Object target)
{
table.getValue(); //returns selected rows
if (getKeyCode() == KeyCode.DELETE)
{
System.out.println("Merry Christmas");
}
}
});

Set the checkbox of a CheckboxTableViewer when the row is clicked

I am very new to SWT. Started working on it today actually. I have a table of type CheckboxTableViewer. What i want to be able to do is whenever the user selects the row (i.e clicks anywhere on the row) I want the check box to be checked (ticked). Currently I have a listener on the CheckboxTableViewer as follows:
diagnosesTableViewer.addCheckStateListener(new ICheckStateListener() {
#Override
public void checkStateChanged(CheckStateChangedEvent event) {
Nomenclature changedStateNomenclature = (Nomenclature) event
.getElement();
if (event.getChecked()) {
selectedNomenclatures.add(changedStateNomenclature);
} else {
selectedNomenclatures.remove(changedStateNomenclature);
}
}
});
I am able to select the row by checking on the checkbox. But i want to select the check box even when the user selects the row by clicking anywhere on that row on any column (not just the checkbox).
I guess that logic would go somewhere in the addSelectionChangedListener for the addSelectionChangedListener. But I am not sure how to go about it. Can anyone help me with this?
Use this code: Add selection listener to the table. ctv is the instance of of your CheckboxTableViewer.
Also I assumed CheckboxTableViewer allow only single selection not multi.
ctv.getTable().addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
int df = ctv.getTable().getSelectionIndex();
ctv.setChecked(ctv.getElementAt(df), !ctv.getChecked(ctv.getElementAt(df)));
}
});

java swing jlist addListSelectionListener ListSelectionListener called twice

I have this bug, after populating the JList, I try to retrieve the value of the selected item. But when I am doing it, it is called twice.
Here is my code :
public CaveAdventureUI(CaveGame game) {
initComponents();
playerCarryItemModel = new DefaultListModel();
caveCarryItemModel = new DefaultListModel();
this.caveGame = game;
this.world = game.getCaveWorld();
listSelectionModel = this.jListCaveCarryItems.getSelectionModel();
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.listSelectionModel.addListSelectionListener( new SharedListSelectionHandler());
//for debug purpose,see the world grid in console
game.drawCaves();
//new JComboBox(Mood.values());
createGridQuarePanels();
//world.startGame(GameLevel.Beginner);
update();
}
private class SharedListSelectionHandler implements ListSelectionListener {
public void valueChanged(ListSelectionEvent listSelectionEvent) {
ListSelectionModel lsm = (ListSelectionModel)listSelectionEvent.getSource();
if (!lsm.isSelectionEmpty()) {
Occupant opt = mapOccupants.get(Integer.toString(jListCaveCarryItems.getSelectedIndex()));
System.out.println("selected index :" +jListCaveCarryItems.getSelectedIndex() +"[["+opt.getName()+"]]");
}
}
}
In the above code, when I am making selection on jList : jListCaveCarryItems, it triggers the SharedListSelectionHandler class. However, when I am clicking on the JList, it print out the selected value twice.
Can anyone help me to figure it out?
Thanks & Regards,
2 ListSelectionEvents are dispatched when the JList is selected—one during and another after the selection event. From How to Write a List Selection Listener
The isAdjusting flag is true if the user is still manipulating the selection, and false if the user has finished changing the selection.
Therefore, ensure that the ListSelectionEvent value is not adjusting.
public void valueChanged( ListSelectionEvent listSelectionEvent) {
if ( !listSelectionEvent.getValueIsAdjusting() && !lsm.isSelectionEmpty()) {
Occupant opt = ...
...
}
}

How to add a ViewerFilter on a JFace TableViewer that update dynamically?

I want to add in a SWT/JFace application a search functionality that filter a TableViewer as the user enter text in the search text field.
final Text filterText = new Text(parent, SWT.NONE);
filterText.addModifyListener(new ModifyListener() {
#Override
public void modifyText(ModifyEvent arg0) {
//TODO how to update the viewer filter with the new text ?
}
});
TableViewer tableViewer = new TableViewer(...);
ViewerFilter filterViewer = new ViewerFilter() {
#Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if (filterText.getText() == "") {
return true;
}
//do my stuff to know if element need to be filtered or not
return false;
}
};
tableViewer.addFilter(filterViewer);
Do I need to remove the filter and create a new one in the modify listener or is there a better solution?
Basically, you need to have a way of passing the entered text to the filter, in your select method you should filter based on this text, and in your text widget's listener pass the text to the filter and call viewer.refresh() on your table.
This example should help you: http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html#jfacetable_filter
org.eclipse.ui.dialogs.FilteredTree is specifically available for that purpose. Why can't you use that?

Categories