I have a parent ScrolledComposite with a dynamic child Composite. When I delete a row, the alignment appears off when using the ScrolledComposite. I have tried this using just Composite instead of ScrolledComposite and it appears to work, so I am wondering if the scrolled layout is just not updating correctly.
Attached are some images showing 1 entry versus 3 entries. The area of interest is the Movable Rings section. The alignment is clearly off when a single entry, but near normal with 3 entries. Code below...
/**
* Create display area for movable rings.
*
* #param topComposite
*/
private void createMovableRingsComposite(Composite topComposite) {
new Label(topComposite, SWT.NONE).setText("Movable Rings");
Composite borderComposite = new Composite(topComposite, SWT.BORDER);
borderComposite.setLayout(new GridLayout(1, false));
borderComposite.setLayoutData(new GridData(
GridData.HORIZONTAL_ALIGN_FILL));
scroll = new ScrolledComposite(borderComposite,SWT.V_SCROLL|SWT.BORDER);
GridLayout gridL = new GridLayout(1, false);
GridData gridD = new GridData(SWT.FILL,SWT.FILL,true,true);
gridD.heightHint = 200;
scroll.setLayout(gridL);
scroll.setLayoutData(gridD);
scroll.setAlwaysShowScrollBars(true);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
movableRingsComposite = new Composite(scroll, SWT.NONE);
movableRingsComposite.setLayoutData(new GridData(SWT.CENTER,
SWT.CENTER, true, true, 1, 1));
movableRingsComposite.setLayout(new GridLayout(6, false));
// Make all the labels
new Label(movableRingsComposite, SWT.NONE).setText("Show");
new Label(movableRingsComposite, SWT.NONE).setText("ID");
new Label(movableRingsComposite, SWT.NONE).setText("Lat");
new Label(movableRingsComposite, SWT.NONE).setText("Lon");
new Label(movableRingsComposite, SWT.NONE).setText("Radius");
new Label(movableRingsComposite, SWT.NONE).setText("Labels");
scroll.setContent(movableRingsComposite);
Composite createComposite = new Composite(borderComposite, SWT.NONE);
createComposite.setLayout(new GridLayout(3, false));
createComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
true, true, 1, 1));
new Label(createComposite, SWT.NONE).setText("New at: ");
pointsMenuButton = new MenuButton(createComposite);
populatePointsMenuButton();
pointsDataManager.addPointsChangedListener(this);
pointsMenuButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
MenuButton menuButton = (MenuButton) e.widget;
MenuItem mi = menuButton.getSelectedItem();
addMovableRing(mi.getText());
menuButton.setSelectedItem("Select One");
}
});
Button delete = new Button(createComposite, SWT.PUSH);
delete.setText("Delete");
delete.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
deleteMovableRing();
}
});
}
/**
* Remove the ring of the selected movable ring row.
*/
private void deleteMovableRing() {
for (MovableRingRow row : movableRings) {
if (row.isSelected()) {
row.dispose();
movableRings.remove(row);
movableRingsComposite.layout(true);
getShell().pack();
if (!movableRings.isEmpty()) {
movableRings.iterator().next().selectRow(true);
}
break;
}
}
}
Related
In my Eclipse plugin editor there is a button called "Add Graph" which adds a composite with a graph on another already created composite with "Grid Layout", but the created composite is not appearing until resizing the Editor manually or switch to another editor and go back
i have also used
parentComposite.pack();
parentComposite.layout();
but the added composite is shown up with a small size, and it`s adjusted also after resizing the Editor manually or switch to another editor and go back.
So my Question is how to call the same handler which runs while resizing the Editor manually or switch to another editor and go back. to adjust my view after adding the new composite.
Adding the Example:
public class MyEditor extends EditorPart {
private Composite compGraphesArea;
#Override
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout(2, false));
Button btnAddGraph = new Button(container, SWT.NONE);
btnAddGraph.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
createGraph(compGraphesArea);
}
});
btnAddGraph.setText("Add Graph");
compGraphesArea = new Composite(container, SWT.NONE);
compGraphesArea.setLayout(new GridLayout(1, true));
compGraphesArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
createGraph(compGraphesArea);
}
private void createGraph(Composite compGraphArea) {
JFreeChart chart = chartHandler.getChart();
compGraphPlot = new Composite(compGraphArea, SWT.BORDER | SWT.EMBEDDED);
compGraphPlot.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Frame chartFrame = SWT_AWT.new_Frame(compGraphPlot);
ChartPanel label = new ChartPanel(chart);
chartFrame.add(label);
chartFrame.pack();
chartFrame.setVisible(true);
XYPlot plot = chart.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setFixedAutoRange(1000000 + slider.getMaximum() / 2);
}
And this what i got after pressing "Add Graph":
and this what i am expecting and what i got after making any change on the Editor manually:
I'm trying to put a ScrolledComposite on my UI with SWT.
but now it only shows blank area or something wrong.
The area to show ScrolledComposite is blank without the code,
"optionCompositeAtLeft.setSize(optionCompositeAtLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT)); "
This is the pic of screenshot.
The optionTab should have the buttons with Scroll....
and with the code, it shows scroll, but too long height and narrow width.
Somehow, the upper is gone, but come back when I extend the size of window with scroll disappearing.
I set the color of background red to make it easier to see for you.
Can anyone help me make "optionCompositeAtLeft" filled in ScrolledComposite?
I want the left area of Option tab to be filled with a red background from "optionCompositeAtLef".
Below is my code.
private BomCloneDialog dialog = null;
private TabFolder tabFolder = null;
private TabItem tabOption = null;
public CreateOptionTab(TabFolder tabFolder, TabItem tabOption) {
this.tabFolder = tabFolder;
this.tabOption = tabOption;
}
public void createOptionTabUI() {
GridData gdWithFillBoth = new GridData(GridData.FILL_BOTH);
Composite optionComposite = new Composite(tabFolder, SWT.NONE);
optionComposite.setLayout(new GridLayout(2, true));
tabOption.setControl(optionComposite);
ScrolledComposite scrolledCompositeForOption = new ScrolledComposite(optionComposite,SWT.V_SCROLL | SWT.BORDER);
scrolledCompositeForOption.setLayoutData(gdWithFillBoth);
Composite optionCompositeAtLeft = new Composite(scrolledCompositeForOption, SWT.BORDER);
scrolledCompositeForOption.setContent(optionCompositeAtLeft);
optionCompositeAtLeft.setLayout(new GridLayout(1, true));
optionCompositeAtLeft.setLayoutData(gdWithFillBoth);
optionCompositeAtLeft.setBackground(new Color(Display.getCurrent(), 255,0,0));
Button b [] = new Button[30];
for(int a=0; a<30; a++){
b[a] = new Button(optionCompositeAtLeft, SWT.PUSH);
b[a].setText("button"+a);
}
**optionCompositeAtLeft.setSize(optionCompositeAtLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT));**
Composite optionButtonComposite = new Composite(optionComposite, SWT.BORDER);
optionButtonComposite.setLayout(new GridLayout(1, true));
optionButtonComposite.setLayoutData(gdWithFillBoth);
Button optionSaveButton = new Button(optionButtonComposite, SWT.NONE);
optionSaveButton.setLayoutData(gdWithFillBoth);
optionSaveButton.setText("Save");
Button optionAddButton = new Button(optionButtonComposite, SWT.NONE);
optionAddButton.setLayoutData(gdWithFillBoth);
optionAddButton.setText("Add");
}
You need to configure ScrolledComposite to resize the content:
scrolledCompositeForOption.setExpandHorizontal(true);
scrolledCompositeForOption.setExpandVertical(true);
I cannot figure out how to correctly set the height of my row composite. I would like the following code snippet to display only one row of data. Any additional rows would be seen by using the scrollbar. I have tried using rowContainer.setSize() but to no avail. Any help would be appreciated.
Please note that the scrolled composite is not directly linked to the rowContainer composite and contains other composites as well.
private void doStuff(final Composite parentContainer) {
final ScrolledComposite scrolledComposite = new ScrolledComposite(
parentContainer, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, true));
final Composite borderComposite = new Composite(scrolledComposite,
SWT.BORDER);
borderComposite.setLayout(new GridLayout(1, false));
borderComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
true));
scrolledComposite.setContent(borderComposite);
scrolledComposite.setSize(borderComposite.computeSize(SWT.DEFAULT,
SWT.DEFAULT));
// Create a container for a label and button
final Composite labelAndAddButtonContainer = new Composite(
borderComposite, SWT.NONE);
labelAndAddButtonContainer.setLayout(new GridLayout(2, false));
labelAndAddButtonContainer.setLayoutData(new GridData(SWT.LEFT,
SWT.TOP, false, false));
// Create a container to hold all dynamic rows
final Composite rowContainer = new Composite(borderComposite, SWT.NONE);
rowContainer.setLayout(new GridLayout(1, false));
rowContainer.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
// Create at least one row.
final Collection<String> rowData = getRowData();
if (rowData.isEmpty()) {
createRow(scrolledComposite, rowContainer, "");
} else {
for (final String text : rowData) {
createRow(scrolledComposite, rowContainer, text);
}
// How do I adjust for the height so that only 1 row shows,
// and scrollbars appear if there is more than one row?
}
// Create a button
final Button addButton = new Button(labelAndAddButtonContainer,
SWT.PUSH);
addButton.setImage(someImage);
final Label label = new Label(labelAndAddButtonContainer, SWT.NONE);
label.setText("SOME_LABEL");
}
When i create a check button and push button on both ends of the dialog in a buttons bar,
it is defaultly created well, but when i drag or resize the dialog, the check button moves away from the left end of the dialog. How should I prevent it from moving when i resize the dialog.
This is how i overrided my buttons bar.
#Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
((GridLayout) parent.getLayout()).numColumns = ((GridLayout) parent.getLayout()).numColumns+2;
GridData checkData = new GridData(SWT.LEFT,
SWT.BOTTOM,true,true);
GridData labelData = new GridData(SWT.CENTER,
SWT.CENTER,true,true);
GridData closeLayoutData = new GridData(SWT.RIGHT,
SWT.BOTTOM,true,true);
parent.setLayoutData(closeLayoutData);
Button close = getButton(IDialogConstants.CANCEL_ID);
close.setText("Close");
close.setLayoutData(closeLayoutData);
close.setParent(parent);
final Button checkButton = new Button(parent, SWT.CHECK);
checkButton.setText("View All schedules");
checkButton.setSelection(false);
checkButton.setLayoutData(checkData);
checkButton.setParent(parent);
checkButton.moveAbove(close);
checkButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (checkButton.getSelection()) {
treeViewer.setFilters(new ViewerFilter[] { new ExistingExpiredScheduleFilter() });
} else {
treeViewer.setFilters(new ViewerFilter[] { new ExistingScheduleFilter() });
}
}
});
Label label = new Label(parent,SWT.HORIZONTAL);
label.setLayoutData(labelData);
label.setParent(parent);
label.moveAbove(close);
}
This works:
protected void createButtonsForButtonBar(Composite parent)
{
final GridLayout layout = (GridLayout)parent.getLayout();
layout.numColumns += 2;
layout.makeColumnsEqualWidth = false;
parent.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
final Button checkButton = new Button(parent, SWT.CHECK);
checkButton.setText("View All schedules");
checkButton.setSelection(false);
checkButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
// Left out selection code for clarity
final Label label = new Label(parent, SWT.NONE);
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, "Close", false);
}
There is no need to call setParent. I have created things in order so there is no need to reorder.
I am using SWT StyledText to populate the responses to my queries in my plugin. I learnt how to autoscroll to the bottom, but couldn't figure out the correct way to auto select the last entry. I have managed to do this in an un-optimized way, i.e updating the background of all the previous lines to white and then highlighting the latest added line, but there has to be a better way to do this.
Here is my code:
rspST.append(rsp + "\n"); ** //<--- Appending the new response to the styledText **
rspST.setTopIndex(rspST.getLineCount() - 1); ** //<-- Scroll to bottom**
for(int i=0; i<rspST.getLineCount() - 2; i++) ** //Setting the background of previous entries to white**
{
rspST.setLineBackground(i, 1,rspST.getDisplay().getSystemColor(SWT.COLOR_WHITE));
}
rspST.setLineBackground(rspST.getLineCount() - 2,
1,rspST.getDisplay().getSystemColor(SWT.COLOR_GRAY)); ** Setting the background of the last line added to gray**
Thanks!
This code will select the last line added by clicking the Button:
private static int lineNr = 0;
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final StyledText text = new StyledText(shell, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button button = new Button(shell, SWT.PUSH);
button.setText("Add new line");
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
/* Get the start position of the new text */
int start = text.getText().length();
/* Create the new line */
String newText = "Line: " + lineNr++ + "\n";
/* Add it */
text.append(newText);
/* Determine the end of the new text */
int end = start + newText.length();
/* Set the selection */
text.setSelection(start, end);
}
});
shell.pack();
shell.setSize(600, 400);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
This is what it looks like: