I have a button that updates data to a grid.I want to display a message box when grid is updated.So I wanted to invoke that in a selection change listener.Is that possible.Any other suggestions??
If you want to show a MessageBox when the Grid is updated. Then you can add StoreListener on the Grid's Store.
Example
grid.getStore().addStoreListener(new StoreListener<ModelData>() {
#Override
public void storeUpdate(StoreEvent<ModelData> se) {
MessageBox.alert(...);
}
});
Related
My program opens a dialog if a certain string is clicked inside a StyledText. So in the mouseDown() I first want to check what has been clicked and then open a dialog. This works. After closing the dialog the mouseUp() is not called. This leads to selecting the text when moving the cursor, as if the user tries to select a text.
I can reproduce the behavior by performing the following tasks:
Click on String in StyledText
-> Dialog Opens
Close Dialog
Move Mouse without clicking
-> Text gets marked as selected
In my use case I don't need mouseUp() to be fired. But having it not fired means the OS assumes that the mouse button is still down and selects text. This may be the correct behavior if a dialog opens and steals the focus. But than there must be a possibility to tell the system, that the mouse button has been released.
myStlyedText.addMouseListener(new MouseListener() {
#Override
public void mouseUp(MouseEvent e) {
System.out.println("MouseUp is fired");
}
#Override
public void mouseDown(MouseEvent e) {
if (certainStringClicked()) {
openDialog();
}
}
#Override
public void mouseDoubleClick(MouseEvent e) {}
});
I can verify that mouseUp() is not called because "MousUp is fired" is not printed on console.
What is the best way to handle this? I already tried to set focus on another widget (setFocus() and forceFocus()), but that didn't help.
I tried to call mouseUp myself:
Event event = new Event();
event.type = SWT.MouseUp;
event.button = 1;
MouseEvent mouseUpEvent = new MouseEvent(event);
mouseUp(mouseUpEvent);
This leads to the message "MousUp is fired", but the selection problem still exists.
I could move the code into the mouseUp() method, but that's not actually what I want. The dialog should appear immediately. What else can I do?
Try adding myStlyedText.notifyListeners(SWT.MouseUp, null); to your code.
It should work.
myStlyedText.addMouseListener(new MouseListener() {
#Override
public void mouseUp(MouseEvent e) {
System.out.println("MouseUp is fired");
}
#Override
public void mouseDown(MouseEvent e) {
if (certainStringClicked()) {
myStlyedText.notifyListeners( SWT.MouseUp, null );
openDialog();
}
}
#Override
public void mouseDoubleClick(MouseEvent e) {}
});
This is not a good solution. But it may be a workaround for some.
It is possible to add SWT.MODELESS to the shell style in the constructor of the Dialog, which extends jface.dialog.Dialog.
setShellStyle(SWT.MODELESS);
MouseUp() get's fired now.
The problem here is that it is possible to open many dialogs by clicking the text although one dialog is already open.
I created an event that execute on table click ,that open a Joptionpane .
but the problem is the joptionpane pops up 2 times .
keep in mind that , i am adding the event after i generate the table
like this click(table0), the tables are generated after retriving from DB and some calculations .
her is the code for the event
protected void click(JTable table)
{
JScrollPane pane=new JScrollPane();
table.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if(!combo_chau.getSelectedItem().toString().equals("station"))
pane.setViewportView(tab_mat(table.getValueAt(table.getSelectedRow(), 2).toString(),table.getValueAt(table.getSelectedRow(), 3).toString()));
if(combo_chau.getSelectedItem().toString().equals("station"))
{pane.setViewportView(tab_sta(table.getValueAt(table.getSelectedRow(), 5).toString(),table.getValueAt(table.getSelectedRow(), 0).toString()));
if(comboBox_1.getSelectedItem().equals("sans detail"))
{ pane.setViewportView(tab_sta_sansdetail(combo_cam.getSelectedItem().toString()));
if(combo_cam.getSelectedItem().toString().equals("tout"))
pane.setViewportView(tab_sta(table.getValueAt(table.getSelectedRow(), 5).toString(),table.getValueAt(table.getSelectedRow(), 0).toString()));
}
}
if(table.getModel().getColumnName(((JTable) e.getSource()).getSelectedColumn()).equals("autre") )
{ int result = JOptionPane.showConfirmDialog(
frame,
pane,
"Use a Panel",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
}
}
});
}
Make sure you are calling your protected void click(JTable table) method only once on each table because you will add a new listener every time you are calling it.
Another issue might be that you are using mousePressed which reacts on the mouse press already, you should consider using mouseClicked instead to react only on a full click.
Is there a way to set the value in a ComboBoxCellEditor other then when the focus is lost on the cell? I'm using it in each cell of a column in a TreeViewer and the only time that the setValue method is called is when focus is lost on the cell. So when a user makes a selection and doesn't click off of the cell the value is never set to the new selection. I've tried adding listeners on the ComboBoxCellEditor and on the control of the ComboBoxCellEditor but nothing seems to pick up the selection event.
I figured out that I needed to cast the control to a CCombo in order to add the correct type of listener to the ComboBoxCellEditor. Here's what I did:
CCombo combo = (CCombo) cellEditor.getControl();
combo.addSelectionListener(new SelectionListener()
{
#Override
public void widgetSelected(SelectionEvent paramSelectionEvent)
{
//selection code here...
}
#Override
public void
widgetDefaultSelected(SelectionEvent paramSelectionEvent)
{
//do nothing here...
}
});
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?