Inserting a Button to JFace Table - swt

How can I insert a SWT Button control into JFace TableViewer ?

The answer given is nice a nice way to implement your own buttons with custom drawings, in or outside the a table. However, you can put SWT controls in JFace Tables.
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/PlacearbitrarycontrolsinaSWTtable.htm
The solution for building a table with columns containing comboboxes, text fields, and buttons provided by the link is:
Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setLinesVisible(true);
for (int i = 0; i < 3; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(100);
}
for (int i = 0; i < 12; i++) {
new TableItem(table, SWT.NONE);
}
TableItem[] items = table.getItems();
for (int i = 0; i < items.length; i++) {
TableEditor editor = new TableEditor(table);
CCombo combo = new CCombo(table, SWT.NONE);
editor.grabHorizontal = true;
editor.setEditor(combo, items[i], 0);
editor = new TableEditor(table);
Text text = new Text(table, SWT.NONE);
editor.grabHorizontal = true;
editor.setEditor(text, items[i], 1);
editor = new TableEditor(table);
Button button = new Button(table, SWT.CHECK);
button.pack();
editor.minimumWidth = button.getSize().x;
editor.horizontalAlignment = SWT.LEFT;
editor.setEditor(button, items[i], 2);
}

You can't. More generally, you can't insert any widgets in tables and trees in SWT, because not all platforms support it. What you can do instead is
Take two screenshots of the button in normal and clicked states;
Put the normal screenshot in table as an image;
Handle clicks on the TableItem.
Here is an example for checkboxes: http://tom-eclipse-dev.blogspot.com/2007/01/tableviewers-and-nativelooking.html

Related

Unable to clear the table in RCP

I am new in RCP. I have created one table and using TableEditor for some columns for putting Label and Progress bar.
But For some cases I want to clear the table. For textual content table.removeAll(); is working but its not clear the Label and Progress bar;
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
compositeObj = toolkit.createComposite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
compositeObj.setLayout(layout);
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 4;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
myTable = new Table(compositeObj,
SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER | SWT.CHECK);
myTable .setLayoutData(gridData);
final int[] bounds = { 30, 80, 100, 80, 100, 50, 100, 50, 50, 70, 50, 50 };
for (int i = 0; i < titles.length; i++) {
TableColumn tblColumn = new TableColumn(btsTable, SWT.NONE);
tblColumn.setText(titles[i]);
tblColumn.setWidth(bounds[i]);
tblColumn.setResizable(true);
tblColumn.setMoveable(true);
tblColumn.pack();
}
myTable.setHeaderVisible(true);
myTable.setLinesVisible(true);
for (int i = 0; i < receiverViewDataProvider.size(); i++) {
items[i] = new TableItem(btsTable, SWT.NONE);
}
fillData();
}
Table Filling Method
{
items[cntIndex].setText(1, sID);
items[cntIndex].setText(2, sName);
editor[cntIndex] = new TableEditor(myTable);
signalStrengthLabel1[selCnt] = toolkit.createLabel(myTable, sText, SWT.CENTER);
editor[cntIndex].grabHorizontal = true;
if (bIsAllow == true)
editor[cntIndex].setEditor(nameLabel1[selCnt], items[cntIndex], 3);
editor[cntIndex] = new TableEditor(myTable);
pbReverse[selCnt] = new ProgressBar(myTable, SWT.HORIZONTAL);
pbReverse[selCnt].setMinimum(0);
pbReverse[selCnt].setMaximum(30);
signalStrengthLabel1[selCnt].setText("" + iLevel + "");
pbReverse[selCnt].setSelection(iLevel);
editor[cntIndex].grabHorizontal = editor[cntIndex].grabVertical = true;
if (bAllow == true)
editor[cntIndex].setEditor(pbReverse[selCnt], items[cntIndex], 4);
}
Tried
for (int Index = 0; Index < Cnt; Index++) {
nameLabel1[btsIndex].setText("");
pbReverse[selIndex].setMaximum(0);
pbReverse[Index].setVisible(false);
editor[Index].dispose();
}
myTable.removeAll();
I am getting below result but want to clear the whole table

SWT Table inside ScrolledCompsite: no scrolling by Mouse Wheel

The code below is taken from:
How to always show vertical scroll bar in SWT table?
Table in the ScrolledComposite scrolls only by moving vertical scrollbar by Mouse or by putting Mouse directly onto vertical scrollbar and scroll the Wheel.
Could you advise, please, how to make table scrollable by Mouse Wheel by putting Mouse onto the Table?
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final ScrolledComposite composite = new ScrolledComposite(shell, SWT.V_SCROLL);
composite.setLayout(new GridLayout());
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Table table = new Table(composite, SWT.NO_SCROLL | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
composite.setContent(table);
composite.setExpandHorizontal(true);
composite.setExpandVertical(true);
composite.setAlwaysShowScrollBars(true);
composite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Button fillTable = new Button(shell, SWT.PUSH);
fillTable.setText("Fill table");
fillTable.setLayoutData(new GridData(SWT.FILL, SWT.END, true, false));
fillTable.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
if (table.getColumnCount() < 1)
{
for (int col = 0; col < 4; col++)
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + col);
}
}
for (int row = 0; row < 20; row++)
{
TableItem item = new TableItem(table, SWT.NONE);
for (int col = 0; col < table.getColumnCount(); col++)
{
item.setText(col, "Item " + row + " " + col);
}
}
for (int col = 0; col < table.getColumnCount(); col++)
{
table.getColumn(col).pack();
}
composite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
});
Button clearTable = new Button(shell, SWT.PUSH);
clearTable.setText("Clear table");
clearTable.setLayoutData(new GridData(SWT.FILL, SWT.END, true, false));
clearTable.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
table.removeAll();
composite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
});
shell.pack();
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
The table is receiving the mouse wheel events which it is ignoring because the Table control is large enough to show all the rows.
I don't see a way to just pass on the wheel events to the scrolled composite.
You could try listening to the wheel events in the table and adjusting the scrolled composite origin - something like:
table.addListener(SWT.MouseVerticalWheel, event ->
{
Point origin = scrolled.getOrigin();
origin.y -= event.count;
scrolled.setOrigin(origin);
});
The count field in the wheel event is 1/-1 depending on the scrolling direction.

How to add buttons to a grid WP8.1 MVVM Light?

I'm having trouble building a dashboard in WP8.1.
I'm using the MVVM Light toolkit.
I'm looking after this outcome:
screenshot of dashboard
I can currently build a dashboard but only with one object per row.
I've searched a lot but haven't found anything to get the desired effect.
Here's the code of the dashboard done programatically:
int i = 0;
Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition());
for (int ct = 0; ct < 3; ct++)
grid.ColumnDefinitions.Add(new ColumnDefinition());
StackPanel stackPanel = new StackPanel();
stackPanel.Name = "stackPanel";
stackPanel.Background = new SolidColorBrush(new Windows.UI.Color());
foreach (var flow in dashboard.DashboardFlows)
{
if (flow.Flow.IsInstalled)
{
//if next object should change line
if (i == 3)
{
stackPanel.Children.Add(grid);
grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition());
for (int ct = 0; ct < 3; ct++)
grid.ColumnDefinitions.Add(new ColumnDefinition());
i = 0;
}
Button button = new Button();
if (flow.Flow.Type.Equals("COMPNEWS"))
{
button.Click += delegate { Frame.Navigate(typeof(NewsPage), flow); };
}
//button stackpanel
StackPanel stack = new StackPanel();
//Button image
Image buttonImage = new Image();
if (!String.IsNullOrEmpty(flow.Flow.IconUrl) || !String.IsNullOrWhiteSpace(flow.Flow.IconUrl))
buttonImage.Source = new BitmapImage(new Uri(flow.Flow.IconUrl));
buttonImage.Height = 45;
buttonImage.VerticalAlignment = VerticalAlignment.Top;
stack.Children.Add(buttonImage);
//Button name
TextBlock buttonName = new TextBlock();
buttonName.Text = flow.Flow.Name;
buttonName.FontSize = 12;
buttonName.TextWrapping = TextWrapping.Wrap;
stack.Children.Add(buttonName);
button.Content = stack;
button.Margin = new Thickness(5, 0, 5, 0);
button.SetValue(Grid.ColumnProperty, i);
button.SetValue(Grid.RowProperty, 0);
button.HorizontalAlignment = HorizontalAlignment.Stretch;
button.VerticalAlignment = VerticalAlignment.Stretch;
button.Background = new SolidColorBrush(Color.FromArgb(130, 30, 30, 30));
grid.Children.Add(button);
i++;
}
}
scroll.Content = stackPanel;
Thank you in advance.

cell click event on text (cell's text) of dynamically created table layout in android

where allValues is array list and i am creating 5 column table.So help me to implement text click event on each text of each cell.I searched and found click event of table row but by this way i am not able to implement cell click event in table layout.
TableLayout tl = (TableLayout) findViewById(R.id.tlparent);
for (int j = 0, k = 0; j < allValues.size() / 5; j++)
{
final TableRow tableRow = new TableRow(this);
for (int y = 1; y <= 5; y++)
{
TextView tv = new TextView(this);
tv.setText(allValues.get(k));
tableRow.addView(tv);
k++;
}
tl.removeView(tableRow);
tl.addView(TABROW);
}
Simply add an onClickListener to your TextView:
TextView tv = new TextView(this);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) v;
String text = tv.getText().toString();
// process your text
}
});
If you want to pass your table "coordinates" (j,k,y, etc.) to your calling function, make additional final variables before setting the OnClickListener and use these inside the onClick method.

Getting value from dynamic table in Android

This is my code for creating a table and appending data dynamically:
table = (TableLayout)findViewById(R.id.table);
inflater = getLayoutInflater();
for(int i = 0; i < 5; i++) {
row = (TableRow)inflater.inflate(R.layout.row,table, false);
TextView text = (TextView)row.findViewById(R.id.text1);
text.setText("sss:" + i);
text1 = (EditText)row.findViewById(R.id.text2);
text2 = (TextView)row.findViewById(R.id.text3);
text2.setText("");
text1.addTextChangedListener(new MyWatcher(text2));
table.addView(row);
}
This code is working fine, but my problem occurs when I click the button. I want to get the values from each row which is to be only filled.
NxtBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
Please help me.