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
}
}
});
Related
I have this Eclipse RCP application which uses SWT. Here is a sample code.
Combo combo = new Combo(shell, SWT.NONE);
combo.setItems(items); // items is a String[]
combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
combo.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println("In widgetDefaultSelected");
}
#Override
public void widgetSelected(SelectionEvent e) {
System.out.println("In widgetSelected");
}
});
The combo has been set up in the code for auto complete. The selection event is supposed to get triggered for mouse or keyboard events. A selection using mouse triggers the selection event but one with keyboard does not. I am trying to see why.
My eclipse is not the latest, it is version is 3.6.2 and the swt JARs that come with it. I would appreciate any help.
Since the selection event is not triggered with keyboard, I added a KeyListener to the combo widget and check to see if the user has pressed enter key.
combo.addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
if (e.keyCode==SWT.CR || e.keyCode==SWT.KEYPAD_CR) { // Enter key
Combo c = (Combo) e.getSource();
System.out.println(c.getText());
// Do rest of processing
}
}
});
Seems like I am getting the selected item out of the list box. So far it seems to be working OK.
Selection event is not used for keyboard events, the Javadoc of Combo#addSelectionListener is pretty clear here:
widgetSelected is called when the user changes the combo's list selection.
widgetDefaultSelected is typically called when ENTER is pressed the combo's text area.
I Have an AbstractDecoratedTextEditor and it has a tab with components of AbstractGraphicalEditPart.
I want to
1) Select single components. If I select one component, other componentes was unselected
2) When I select one component, I wan to to fire the selection listenner of eclipse. Because this listenner will change the properties view of Eclipse.
I tried this code for number 2, but not work.
((IFigure) componentFigure).addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent arg0) {
ComponentEditPart.this.setSelected(SELECTED);
fireSelectionChanged();
super.mousePressed(arg0);
}
}
There is a selection listener in GEF, but it's on the EditPartViewer. Add a ISelectionChangedListener to your Graphical viewer. Each editpart has a method #getViewer() (i.e. AbstractGraphicalEditPart#getViewer()).
graphicalEditPart.getViewer().addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
// TODO: implement it to handle selection change
}
}};
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 a JTable with editable cells. When I click in a cell, it enters edit mode; the same happens when I'm moving through cell using the directional arrows.
Now I want to select the cell instead of start editing, and edit the cell only when the Enter key is pressed.
If any other information is needed, please just ask for it.
Edit: Action for Enter key
class EnterAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
JTable tbl = (JTable) e.getSource();
tbl.editCellAt(tbl.getSelectedRow(), tbl.getSelectedColumn());
if (tbl.getEditorComponent() != null) {
tbl.getEditorComponent().requestFocus();
}
}
}
Now this is for left arrow action the rest of 3 are not hard to deduce from this one:
class LeftAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
JTable tbl = (JTable)e.getSource();
tbl.requestFocus();
tbl.changeSelection(tbl.getSelectedRow(), tbl.getSelectedColumn() > 0 ? tbl.getSelectedColumn()-1:tbl.getSelectedColumn(), false, false);
if(tbl.getCellEditor()!=null)
tbl.getCellEditor().stopCellEditing();
}
}
And this is how you bind this actions:
final String solve = "Solve";
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, solve);
table.getActionMap().put(solve, new EnterAction());
final String sel = "Sel";
KeyStroke arrow = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(arrow, sel);
table.getActionMap().put(sel, new LeftAction());
Oh,i almost forgot,to select the cell instead of edit on Mouse Click:
public static MouseListener mAdapterTable = new MouseListener()
{
#Override
public void mousePressed(MouseEvent e)
{
JTable tbl=((JTable)e.getComponent());
if(tbl.isEditing())
{
tbl.getCellEditor().stopCellEditing();
}
}
#Override
public void mouseClicked(MouseEvent e) {
JTable tbl=((JTable)e.getComponent());
if(tbl.isEditing() )
tbl.getCellEditor().stopCellEditing();
}
#Override
public void mouseReleased(MouseEvent e) {
JTable tbl=((JTable)e.getComponent());
if(tbl.isEditing() )
tbl.getCellEditor().stopCellEditing();
}
};
The EventListner must be added to table like so:
table.addMouseListener(mAdapterTable);
Use Key Bindings for this. Most Look & Feel implementations already bind F2 to the table's startEditing action, but you add a different binding:
tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing");
This will effectively replace the previous binding of Enter to the table's selectNextRowCell action.
Here is what i would do:
First enable the single cell selection for the JTable
Create a KeyAdapter or KeyListener for the JTable or for the JPanel,
what contains your table.
In the KeyAdapter's keyPressed() method enter the edit mode of the
selected cell, something like this:
http://www.exampledepot.com/egs/javax.swing.table/StopEdit.html
You can check in the keyPressed() method, if the user pressed the right button for editing. I'm not sure, if the normal (double click) editing is disabled in your table, then what happens, if you try to edit it programmatically, but if it doesn't work, then you can enable the editing on the selected cell, when the user presses the edit button, then when he/she finished, disable it again.