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");
}
}
});
Related
I have a SWT Table with check boxes enable on the rows to allow TableItems to be selected. I currently have a listener on there
myTable.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event selectionEvent) {}});
This listener fires both when a check box for a row is checked and when a row is highlighted / selected, I haven't been able to find a way to separate these actions. Am I using the wrong listener or is there a way to do this?
Thanks for any help.
You can check within the Listener if it's a checkbox event or a selection event:
myTable.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event e)
{
if(e.detail == SWT.CHECK)
{
// Checkbox
}
else
{
// Selection
}
}
});
I Have a Jtable where a TableModelListener is listening
Implemented by
private void anadeListenerAlModelo() {
tabla.getModel().addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent evento) {
if (evento.getType() == TableModelEvent.UPDATE) {
System.out.println("Editing...");
}
}
});
}
And I want to detect when a cell is being edit but only if is edit from de keyboard, because, other events could update the content.
I only need when the user is editing a cell from the keyboard. It's this posible?
Thanks!
Check out the Table Cell Listener.
It listens for PropertyChange events on the table for when the cell editor is activated/deactivated and then notifies you when the data in the cell has actually been changed.
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)));
}
});
I am trying to select/focus a row of a TableView programmatically.
I can select a row, but it is not getting rendered as focused (not highlighted). I have tried many combinations of the code below, but nothing seems to work.
table.getSelectionModel().select(0);
table.focusModelProperty().get().focus(new TablePosition(table, 0, column));
table.requestFocus();
Is it possible to highlight a row programmatically?
I am using JavaFX 2.2.21
Try putting your request for table focus first and then wrapping the whole thing in a runLater.
Platform.runLater(new Runnable()
{
#Override
public void run()
{
table.requestFocus();
table.getSelectionModel().select(0);
table.getFocusModel().focus(0);
}
});
table.getFocusModel().focus(0); is not needed, but I would also add scrollTo as well.
Java 8:
Platform.runLater(() ->
{
table.requestFocus();
table.getSelectionModel().select(0);
table.scrollTo(0);
});
Java 7:
Platform.runLater(new Runnable()
{
#Override
public void run()
{
table.requestFocus();
table.getSelectionModel().select(0);
table.scrollTo(0);
}
});
I have two components: a ListView and a TableView. When an item in the ListView is clicked, I want the focus and selection to move to the TableView and render the selected component in the TableView. To accomplish this, I did it with:
void listViewClickHandler(MouseEvent e){
A a = listView.getSelectionModel().getSelectedItem();
if(a != null){
// some stuff
// move focus & selection to TableView
table.getSelectionModel().clearSelection(); // We don't want repeated selections
table.requestFocus(); // Get the focus
table.getSelectionModel().selectFirst(); // select first item in TableView model
table.getFocusModel().focus(0); // set the focus on the first element
tableClickHandler(null); // render the selected item in the TableView
}
void tableClickHandler(MouseEvent e){
B b = table.getSelectionModel().getSelectedItem();
render(b);
}
table.getSelectionModel().select(0); works for me. Maybe the problem is in your css?
I have created a popup box that extends DialogBox and uses a cellTable that contains a list of values, one of which will be selected and inserted into a textBox.
-I have an onSelectionChange handler which is fired when one of the rows is clicked.
-I have an onDoubleClick handler which is fired when the same rows are double clicked.
both work when the other is commented out. But when they are both in the live code, whichever one is written first gets overwritten by the other one and no longer gets called.
Any way around this?
Code snipbit:
final SingleSelectionModel<popUpBoxContent> selectionModel= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){
public void onSelectionChange (selectionChangeEvent event){
//Do something
}});
final SingleSelectionModel<popUpBoxContent> selectionModel2= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionMode2);
cellTable.addDomHandler(new DoubleClickHandler(){
public void onDoubleClick(final DoubleClickEvent event){
//Do something else
}},
DoubleClickEvent.getType());
Thank you!
Yes they get overwritten from what I can see in the snippet. Assuming "popUpBoxContent" is the data type with which the CellTable (I presume cellTable is a CellTable) is being populated you could try this and see if it works:
final SingleSelectionModel<PopUpBoxContent> selectionModel = new SingleSelectionModel<PopUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
cellTable.addDomHandler(new DoubleClickHandler() {
public void onDoubleClick(final DoubleClickEvent event) {
PopUpBoxContent selected = selectionModel.getSelectedObject();
if (selected != null) {
System.out.println("double clicked");
}
}
},
DoubleClickEvent.getType());
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
System.out.println("clicked");
}
});