I have a Wizard page which contains a Jface TableViewer with 5 columns. Wizard page also contains "Add" and "Remove" buttons. When the Add button is clicked a new Dialog opens up with 3 Text boxes and 2 Combo boxes and the user enters details into them.
Everything is fine up to this point but I would want to know how to fetch the contents of the text boxes and the Combo boxes(after user clicks "OK" button in the dialog box) and store the same contents in the table viewer of my wizard page? DO I need to use a MouseListener and override mouse down method and write the logic in the mouse down method to fetch the contents? Or is there any other way to do this? To fetch the contents I simply say for example :
TextBox txt = new TextBox(shell, SWT.BORDER);
String txtContents = txt.getText();
What is the approach to fetch the dialog window's contents and store the data in the table?
And how do I remove a selected row in a table when the user clicks on "Remove" button of the dialog box?
The best thing to do would be to build a model that stores the values from the controls in the dialog and manipulate the addtion and removal of elements from the table through the list of models set as table input.
DialogModel
List<DialogModel> tableViewerEntries;
as a class member that is to be set as the table input. i.e., tableViewer.setInput(tableViewerEntries);
When Ok button in the dialog is pressed, do the following:
DialogModel model = new DialogModel();
...
@Override
protected void okPressed() {
model.setAttribute1(text1.getText());
model.setAttribute2(combo1.getText()); // or use getItem(selectedItemIndex);
...
}
Create a public method in the dialog class that returns the model. say., getModel()
;
Inside the selection listener of the Add button, do
if(dialog.open == SWT.OK) {
// using a getter method, get the model from the dialog.
DialogModel dialogModel = dialog.getModel();
// add it to the list of models set as input to the tableViewer
tableViewerEntries.add(dialogModel);
// Refresh table to set the new element in the tableviewer.
tableViewer.refresh();
}
For remove, just remove the selected model from the list of table entries.
// remove the selected element from the list of DialogModels set as Table Input.
int index = tableViewer.getTable().getSelectionIndex();
tableViewerEntries.remove(index);
tableViewer.refresh();
The content and label providers of the table viewer should be adjusted accordingly to display the model attributes as cell values. I assume that you are familiar with them.