Composites are getting truncated inside ScrolledCompsite - swt

In the following code, I give 2000 labels inside the ScrolledComposite. But I see only 1400+ labels. I find that this is due to OS restriction. I need the workaround for this with canvas.
Display display = new Display();
Shell parent = new Shell(display);
parent.setLayout(new GridLayout(1, false));
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
final ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
final Composite childComposite = new Composite(scrolledComposite, SWT.NONE);
scrolledComposite.setContent(childComposite);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
childComposite.setLayout(layout);
for (int i = 0; i < 2000; i++) {
Label label = new Label(childComposite, SWT.BORDER);
label.setText("Label " + i);
}
Point size = childComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
scrolledComposite.setMinSize(size);
parent.setSize(parent.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 200);
parent.open();
while (!parent.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();`

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.

Why does SWT not show all rows of a Table contained in a ScrolledComposite?

I took the attached code from How to always show vertical scroll bar in SWT table?, except for the nuber of rows being 4000 instead of 20. In that case I can not go beyond 1416 rows in the table, what is the reason for that ?
private static final int MAX_ROWS = 4000;
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 < MAX_ROWS; 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();
}
}

JavaFX resize rotated object to GridPane cell

I have a problem with the orientation of a node (GridPane) after rotating it with setRotate() in JavaFX. When i rotate the node and put it in a cell of the GridPane, I would like the rotated node to fit inside the cell and also resize with the cell. I added some sample code to show you what I would like the result to be.
public class MonopolyApp extends Application {
#Override
public void start(Stage primaryStage) {
//Construction of the grid
GridPane square = new GridPane();
square.setGridLinesVisible(true);
final int heightPercent = 14;
final int widthPercent = 8;
RowConstraints rowsEdge = new RowConstraints();
rowsEdge.setPercentHeight(heightPercent);
RowConstraints rowsMid = new RowConstraints();
rowsMid.setPercentHeight(widthPercent);
ColumnConstraints colEdge = new ColumnConstraints();
colEdge.setPercentWidth(heightPercent);
ColumnConstraints colMid = new ColumnConstraints();
colMid.setPercentWidth(widthPercent);
square.getColumnConstraints().addAll(colEdge, colMid,
colMid, colMid, colMid, colMid, colMid, colMid, colMid, colMid, colEdge);
square.getRowConstraints().addAll(rowsEdge, rowsMid,
rowsMid,rowsMid,rowsMid,rowsMid,rowsMid,rowsMid, rowsMid,rowsMid, rowsEdge);
GridPane wrongSuare = makeMonopolySquare();
square.add(wrongSuare, 0, 4);
wrongSuare.setRotate(90);
GridPane rightSquare = makeMonopolySquare();
square.add(rightSquare, 1, 10);
Scene s = new Scene(square);
primaryStage.setHeight(500);
primaryStage.setWidth(500);
primaryStage.setScene(s);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
private GridPane makeMonopolySquare(){
GridPane monopolySquare = new GridPane();
RowConstraints top = new RowConstraints();
top.setPercentHeight(20);
RowConstraints bottom = new RowConstraints();
bottom.setPercentHeight(80);
ColumnConstraints c = new ColumnConstraints();
c.setPercentWidth(100);
monopolySquare.getRowConstraints().addAll(top,bottom);
monopolySquare.getColumnConstraints().addAll(c);
bottom.setValignment(VPos.CENTER);
monopolySquare.setGridLinesVisible(true);
Label name = new Label("name");
Pane colorPane = new Pane();
colorPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
colorPane.setBackground(new Background( new BackgroundFill(Color.BLUE, null, null)));
GridPane.setMargin(colorPane, new Insets(1));
monopolySquare.add(colorPane,0,0);
monopolySquare.add(name, 0, 1);
return monopolySquare;
}
}
If you run the code you will see that the GridPane at the bottom of the stage perfectly fits it's cell. But the rotated GridPane does not. I will also add a picture to show you what my problem is:
Does anyone know how to solve this? I know that I could put it in a group, but the problem with putting it in a group is that the group would not resize to the cell of the GridPane.
Well, that is a tough one. No optimal solution comes to mind, but if I was in your position, I would try to stay within the confinements of the layout (not use low-level operations like rotation).
For example, build a differently layout cell for each row/column:
private static Node makeMonopolySquare( final Layout layout )
{
final GridPane monopolySquare = new GridPane();
monopolySquare.setGridLinesVisible( true );
final Label label = new Label( "name" );
final StackPane name = new StackPane( label );
final Pane colorPane = new Pane();
GridPane.setMargin( colorPane, new Insets( 1 ) );
colorPane.setMaxSize( Double.MAX_VALUE, Double.MAX_VALUE );
colorPane.setBackground( new Background( new BackgroundFill( Color.BLUE, null, null ) ) );
final RowConstraints top = new RowConstraints();
final RowConstraints bottom = new RowConstraints();
// bottom.setValignment( VPos.CENTER );
// top.setValignment( VPos.CENTER );
final ColumnConstraints c = new ColumnConstraints();
c.setPercentWidth( 100 );
final ColumnConstraints right = new ColumnConstraints();
// right.setHalignment( HPos.CENTER );
final ColumnConstraints left = new ColumnConstraints();
// left.setHalignment( HPos.CENTER );
final RowConstraints r = new RowConstraints();
r.setPercentHeight( 100 );
switch ( layout )
{
case BOTTOM:
top.setPercentHeight( 20 );
bottom.setPercentHeight( 80 );
monopolySquare.getRowConstraints().addAll( top, bottom );
monopolySquare.getColumnConstraints().addAll( c );
monopolySquare.add( colorPane, 0, 0 );
monopolySquare.add( name, 0, 1 );
break;
case LEFT:
right.setPercentWidth( 20 );
left.setPercentWidth( 80 );
monopolySquare.getColumnConstraints().addAll( left, right );
monopolySquare.getRowConstraints().addAll( r );
monopolySquare.add( name, 0, 0 );
monopolySquare.add( colorPane, 1, 0 );
break;
case RIGHT:
right.setPercentWidth( 80 );
left.setPercentWidth( 20 );
monopolySquare.getColumnConstraints().addAll( left, right );
monopolySquare.getRowConstraints().addAll( r );
monopolySquare.add( colorPane, 0, 0 );
monopolySquare.add( name, 1, 0 );
break;
case TOP:
top.setPercentHeight( 80 );
bottom.setPercentHeight( 20 );
monopolySquare.getRowConstraints().addAll( top, bottom );
monopolySquare.getColumnConstraints().addAll( c );
bottom.setValignment( VPos.CENTER );
monopolySquare.add( colorPane, 0, 1 );
monopolySquare.add( name, 0, 0 );
break;
}
This however, will not rotate the label (which, I would argue is better anyways for a digital monopoly board ;) ). As soon as you rotate the label, you get the same problem you had with the cell, only smaller.
The full code example.

Inserting a Button to JFace Table

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