Refreshing table Editor with table item in swt - java

Hello i have a following code for swt table
private Table folderAssociationTable;
private TableItem item;
private TableEditor editor;
folderAssociationTable = componentsRenderer.createTableWidget(container, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION, 2, true);
folderAssociationTable.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
event.height = 20;
}
});
componentsRenderer.createTableColumWidget(folderAssociationTable, SWT.CHECK, "Item", 80);
// add a column to display source folders
componentsRenderer.createTableColumWidget(folderAssociationTable,
SWT.LEFT, PropertyClass.getPropertyLabel(QTLConstants.SOURCE_FOLDER_COLUMN_LABEL), 200);
// add a column to display target folders
componentsRenderer.createTableColumWidget(folderAssociationTable,
SWT.LEFT, PropertyClass.getPropertyLabel(QTLConstants.TARGET_FOLDER_COLUMN_LABEL), 200);
and then adding table data in this fashion
item = new TableItem(folderAssociationTable, SWT.NONE);
editor = new TableEditor (folderAssociationTable);
Button button = new Button(folderAssociationTable, SWT.CHECK);
button.setText("item "+ (i+1));
button.pack();
editor.minimumWidth = button.getSize ().x;
editor.setEditor(button, item, 0);
item.setText(1, folderlist[i]); // add source folder to the first column of the table
item.setText(2, targetFolderPath); // add target folder to the second column of the table
sourceTargetFolderMap.put(folderlist[i], targetFolderPath);
I have remove button on this page outside table component , on its action Listener i am removing selected row from table , but in this case when table is getting updated only table items are updated but table editor remain at same position , how can i refresh both table editor and table items on click of remove button.

Have you seen the example given in the documentation? Add this to your ActionListener:
// to close the old editor
Control oldEditor = editor.getEditor();
if (oldEditor != null) oldEditor.dispose();
// add some logic if you want to open the editor again on a different row

Related

Select a row of a Table for second time

i currently able to select a row of table using ListSelectionModel object and open a new window. but if i close that window and click that row again, it would not open anymore until i select another row of table (i can't select a row for a second time). do you know how can i solve this issue ?
this is what i have done:
ListSelectionModel model = table.getSelectionModel();
model.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e)
{
if(!model.getValueIsAdjusting())
{
int row = model.getMinSelectionIndex();
//new Window opens :
SubjectDetail sd = new SubjectDetail(Datalist2.project.listOfData().get(row));
}
}
});
The selection is not working because that particular row is already selected.
Try clearing the selection when a new window is opened.
table.getSelectionModel().clearSelection().

JFace : Adding images to a table viewer is creating an issue

I have a table viewer with 5 columns. In the 1st column I check some conditions and add an image and the rest of the columns will have text, But when I try to add the image and then add the rest of the columns with text then the 2nd column's text sits right beside the image in the 1st column. Here's my sample code snippet :
TableItem item = new TableItem(table, SWT.NONE);
if(condition) {
item.setImage(image);
}
else {
item.setImage(image);
}
item.setText(new String[]{"street", "city","state","Zip Code"});
Problem is that the string "street" sits right beside the image in the 1st column itself but I want the string "street" in the second column and the rest of the strings in the subsequent columns. What am I doing wrong here? How do I add table items from the 2nd column?
Each column in the table can have both an image and text.
Calling item.setImage(image) sets the image for the first column.
Calling item.setText(String []) sets the text for each column starting at the first column. So your "street" goes next to the image.
If you don't want text in the first column then set the text for that column to blank:
item.setText(new String[]{"", "street", "city", "state", "Zip Code"});
Update:
You can also set the text for individual columns with
item.setText(column, text);
If you are using TableViewer you should not be using TableItem, instead use the label provider and content provider.
Not as clear. If you want just an image in the first colum then then just provide an empty string:
item.setText(new String[]{"", "street", "city","state","Zip Code"});
But it is not worth working with jface without using the benefits of ContentProvider and LabelProvider. So you should read some jface tutorials before doing stuff like that.
Example:
TableViewer tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
TableViewerColumn tableViewerColumnImg = new TableViewerColumn(tableViewer, SWT.NONE);
tableViewerColumnImg.setLabelProvider(new ColumnLabelProvider()
{
#Override
public Image getImage(Object element)
{
Person p = (Person) element;
return p.getImage();
}
});
TableColumn tblclmnImg = tableViewerColumnImg.getColumn();
tblclmnImg.setWidth(100);
tblclmnImg.setText("Img");
TableViewerColumn tableViewerColumnStreet = new TableViewerColumn(tableViewer, SWT.NONE);
tableViewerColumnStreet.setLabelProvider(new ColumnLabelProvider()
{
#Override
public String getText(Object element)
{
Person p = (Person) element;
return p.getStreet();
}
});
TableColumn tblclmnStreet = tableViewerColumnStreet.getColumn();
tblclmnStreet.setWidth(100);
tblclmnStreet.setText("Street");
tableViewer.setInput(//set an array of persons here
);

how to move the text to the left?

I created a toolbar with text field and two toolItem
The problem that the Text is exist in the right scrren and not in the left screen.
How I can put the text in the right screen ?
It should look :
Text item1 item2
ToolBar treeToolBar = new ToolBar(treeComposite, SWT.RIGHT_TO_LEFT);
Text text = new Text(treeToolBar, SWT.NONE);
text.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, false, false));
text.setText("Text");
text.pack();
item1 = new ToolItem(treeToolBar, SWT.PUSH | SWT.RIGHT);
item2 = new ToolItem(treeToolBar, SWT.PUSH | SWT.RIGHT);
treeToolBar.pack();
After you create the text, you also have to create a tab item for it. Remove the text.pack() call and insert this:
ToolItem textItem = new ToolItem(treeToolBar, SWT.SEPARATOR);
textItem.setControl(text);
textItem.setWidth(text.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
This is how ControlContribution does in method fill(ToolBar parent, int index).

selection in CheckboxTableViewer on win7 is not working

i try to use an org.eclipse.jface.viewers.CheckboxTableViewer, as a component of a org.eclipse.jface.wizard.WizardPage. I created it this way:
public void createControl(Composite parent) {
composite = new Composite(parent, SWT.NULL);
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
composite.setLayout(gridLayout);
setControl(composite);
/* CheckboxTableViewer */
viewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
final Table table = viewer.getTable();
GridData data1 = new GridData();
data1.grabExcessHorizontalSpace = true;
data1.grabExcessVerticalSpace = true;
data1.horizontalSpan = 2;
data1.horizontalAlignment = SWT.FILL;
data1.verticalAlignment = SWT.FILL;
table.setLayoutData(data1);
table.setHeaderVisible(true);
table.setLinesVisible(true);
checkboxColumn = new TableColumn(table, SWT.LEFT);
...
the content of the viewer is inserted dynamically by a contentprovider. Everything works fine on gnome. While testing this on windows 7 (64 and 32 bit also), i am not able to select any entries of that view. Mouseclicks just seems to have no impact on the view.
I added a mouselistener to the table, and the mouseUp-/Down event is fired, selectionChanged and doubleClick on the viewer is not fired. Anyone who can explain this behaviour to me?
thx in advance,
hage
(i already posted this question in the eclipse forum without any response yet: http://www.eclipse.org/forums/index.php/t/250953/ )
You have to add another style flag while creating the CheckboxTableViewer: SWT.FULL_SELECTION
viewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER | SWT.FULL_SELECTION);
You can now select rows in the table by a single clicking.

java SWT: how can I draw a widget to an offscreen buffer

I would like to draw the contents of a StyledText widget in to an Image; this will then provide a convenient way to stick the image into a Table cell.
Any suggestions about the best way to go about this?
Why do you want to create an image?
You could just render the the StyledText-widget in the table cell. If you have a lot of items and it is a performance problem you could create a virtual table by using SWT.VIRTUAL. If you're using JFace check out org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider.
If you're using plain SWT you should be able to use a TableEditor with a StyledText-widget as the editor. Something like this:
Table table = new Table(new Shell(new Display()), SWT.NONE);
table.setHeaderVisible (true);
TableColumn column = new TableColumn (table, SWT.NONE);
StyledText styledText = new StyledText(table, SWT.NONE);
TableItem item = new TableItem (table, SWT.NONE);
TableEditor editor = new TableEditor (table);
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor (styledText, item, 0);

Categories