How to remove unwanted space of jface widget which is disposing dynamically - java

I am designing a GUI in jface/swt,in which I am creating a scrolledform which contains a section and inside that section there is a composite.
On that composite,i am adding a comboviewer which has 2 elements.
on selecting first element, there are 2 widgets, one is label with textbox and other is table.
On selecting second element, there is only one widget that is label with textbox.
Now,my problem is that when i am selecting second element it is disposing the table but the space occupied by table is remain there.
so,how to remove that unwanted space after selecting seconnd element.
Please help me out.
Here is my first function in which i am creating scrolled form
class Test extends FormDialog{
protected void createFormContent(IManagedForm managedform) {
toolkit = managedform.getToolkit();
scrolledForm = managedform.getForm();
Shell shell = scrolledForm.getShell();
shell.setText("First Form");
// I am using GridLayout and GridData.
scrolledForm.pack();
scrolledForm.reflow(true);
}
// After this there is a method containing section and a composite inside the section.
protected void createCombo(final Composite client) {
Label label1 = toolkit.createLabel(client,"First Label : ", SWT.NULL);
final ComboViewer comboViewer1 = new ComboViewer(client,SWT.NORMAL|SWT.NORMAL);
//In this Compoviewer1, I am adding 2 persons ie "ABC" and "XYZ".
comboViewer1.getCombo().select(0);
// Here I am Calling Two methods, createFirstContent() method contains label and comboviewer. And createSecondContent() method contains TableViewer which is created inside a Group
createFirstContent(client);
createSecondContent(client);
comboViewer1.addSelectionChangedListener(new ISelectionChangedListener(){
#Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection) event
.getSelection();
if(((Person) selection.getFirstElement()).getName().equals("ABC")){
createFirstContent(client);
createSecondContent(client);
}else if(((Person) selection.getFirstElement()).getName().equals("XYZ")){
if(tablegroup != null && !(tablegroup.isDisposed())){
tablegroup.dispose();
createFirstContent(client);
}
});
}
protected void createFirstContent(Composite clientComposite){
Label label = toolkit.createLabel(clientComposite,"First Lbl : ", SWT.NULL);
label .setLayoutData(new GridData(SWT.FILL, SWT.FILL,true, false));
ComboViewer combo = new ComboViewer(clientComposite, SWT.READ_ONLY | SWT.NORMAL);
}
protected void createSecondContent(Composite clientComposite) {
tablegroup = new Group(clientComposite, SWT.FILL);
tablegroup.setLayout(new GridLayout());
tablegroup.setText("Table Group");
createTableViewer(tablegroup);
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = false;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
tablegroup.setLayoutData(gridData);
}
protected void createTableViewer(Group tablegroup) {
viewer = new TableViewer(tablegroup, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
TableColumnLayout tcl = new TableColumnLayout();
createColumns(tcl);
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
GridData gridData = new GridData(SWT.FILL, SWT.FILL,true, true;
gridData.horizontalSpan = 2;
viewer.getControl().setLayoutData(gridData);
}
In the createColums() method, i am creating the columns of table.
After all these things, i am creating another section.and creating some other widgets in that section.
but my problem is that when i am disposing taht table while selecting XYZ, table will get disposed but the space occupied by table is not removing.
So, there will be an unwanted gap/space between both the sections.
Please tell me how to remove that space.

Related

Unable to redraw SWT Composite in a Wizard Page

I have a composite, which has 3 UI controls - SWT Group, SWT TabFolder, and a child composite that has a label and a link. In some use-cases the SWT Group is hidden, and I would like that the blank space is not seen instead the composite has to be redrawn and show only the SWT TabFolder and the child composite, without any space.
I'm using composite.layout(true), but still there is blank space of the SWT Group when it is made hidden.
Could someone help me solve this issue
TestComposite extends Composite{
private Label childlabel;
private Group group;
private TabFolder tabFolder;
private Button button;
TestComposite(Compossite parent){
super(parent, SWT.NONE);
setLayout(new GridLayout(1, true));
setFont(parent.getFont());
setBackground(parent.getBackground());
GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
createControl();
}
public void createControl() {
this.group = new Group(this, SWT.SHADOW_IN);
this.group.setSize(this.getDisplay().getActiveShell().getSize());
this.button = new Button(this.group, SWT.PUSH);
GridDataFactory.swtDefaults().applyTo(this.button);
this.tabFolder = new TabFolder(this, SWT.TOP);
GridDataFactory.swtDefaults().align(SWT.FILL, SWT.FILL).grab(true,
true).applyTo(this.tabFolder);
Composite childComposite = new Composite(this, SWT.NONE);
childComposite .setLayout(new GridLayout(2, false));
GridDataFactory.swtDefaults().grab(true, false).align(SWT.LEFT,
SWT.TOP).applyTo(childComposite );
this.childLabel = new Label(childComposite, SWT.NONE);
GridDataFactory.swtDefaults().grab(true, false).applyTo(childlabel);
setInput(abc);
enableVisibilityOfGroup(false);
}
public void enableVisibilityOfGroup(Boolean visible){
this.group.setVisible(visible);
// this shoudl redraw the UI and shown only tabFolder and label in the
// whole wizard page, but there is blank space in place of group
this.layout(true);
}
}
Set GridData on the Group
GridDataFactory.swtDefaults().applyTo(this.group);
Use GridData.exclude to exclude/include the group in the layout:
public void enableVisibilityOfGroup(boolean visible) {
this.group.setVisible(visible);
GridData gridData = (GridData)this.group.getLayoutData();
gridData.exclude = !visible;
this.layout(true);
}

How can I show Scroll on my UI, with JAVA, SWT?

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);

ScrolledComposite children not populating content

I have a ScrolledComposite widget that I'm initializing inside a selection listener (in turn attached to another composite). This ScrolledComposite contains in turn another Composite, which in turn holds a Button and a Label. While the internal composite appears, none of its children widgets do. I've used ScrolledComposite plenty of times before, and everything looks right to my eye. Can any of you see anything wrong? Note, the ScrolledComposite is a class variable. Also note that this problem is occurring regardless of if I ever dispose of the composite and its contents in the else condition.
final Button showConsole = new Button(topLeft, SWT.CHECK);
showConsole.setText("Show Debug Console");
showConsole.setFont(new Font(domains.getDisplay(), "Segoe UI", 9, SWT.ITALIC));
showConsole.setSelection(false);
showConsole.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
//The total widget group is only supposed to appear when the button is selected
if (showConsole.getSelection()) {
scrolledConsoleComp = new ScrolledComposite(leftComposite,
SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Composite consoleComposite = new ScrolledComposite(scrolledConsoleComp, SWT.NONE | SWT.BORDER);
consoleComposite.setLayout(new GridLayout());
consoleComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
consoleComposite.setVisible(true);
scrolledConsoleComp.setContent(consoleComposite);
scrolledConsoleComp.setExpandHorizontal(true);
scrolledConsoleComp.setExpandVertical(true);
scrolledConsoleComp.setLayout(new GridLayout());
scrolledConsoleComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button clear = new Button(consoleComposite, SWT.PUSH);
clear.setText("Clear Console");
final Label consoleText = new Label(consoleComposite, SWT.WRAP);
consoleText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
consoleText.setText("Messages: \n" + consoleData);
clear.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
consoleData = "";
consoleText.setText("Messages: \n" + consoleData);
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
scrolledConsoleComp.setMinSize(leftComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
leftComposite.layout(true);
} else {
scrolledConsoleComp.setVisible(false);
scrolledConsoleComp.dispose();
leftComposite.layout(true);
}
}
I appreciate any insight. Let me know if anything in this question is unclear. Thank you!
You're creating your Button and your Label inside a ScrolledComposite. They won't be shown because setContent() is not called.
Usually a ScrolledComposite contains a Composite that holds all the other Widgets.
Your consoleComposite should become a Composite.
No need for a call to setVisible()

Resize SWT Table

I have a wizard with table and button components. When I click to the add button I choose from dialog window what items should be added to the table. I confirm items, and then this ones appears in the table with scrollbar. But if I resize the wizard, the table size is changed. How to fix it?
Table before resize:
Table after wizard resize
Composite compositeArea = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
compositeArea.setLayout(layout);
Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL);
new TableColumn(someList, SWT.NULL);
table.setLayoutData(new GridData(HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL));
** Please note that the use of HORIZONTAL_ALIGN_FILL and GRAB_HORIZONTAL is discouraged. Instead, you should be using the public GridData(int, int, boolean, boolean) constructor. **
To slighly simplify your code snippet (only one column on the composite, and only one default table column - see full code below):
// ...
final Composite compositeArea = new Composite(parent, SWT.NONE);
compositeArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
compositeArea.setLayout(new GridLayout());
final Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL);
table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
// ...
...and we see that the Table isn't fitting in the available space or showing the scrollbar as expected, and the same occurs when the Shell is resized.
In answer to your question, this is because the layout data of the Table doesn't know how to layout vertically - you've only specified two horizontal style attributes.
If we instead use the suggested constructor:
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
...the Table takes the available space correct, shows the scrollbar, and updates correctly when the Shell is resized. Using the layout data, we've told the Table to fill the available horizontal space, and the Table will display the scrollbar if necessary.
Full example:
public class TableResizeTest {
private final Display display;
private final Shell shell;
public TableResizeTest() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setMaximized(true);
final Composite parent = new Composite(shell, SWT.NONE);
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
parent.setLayout(new GridLayout());
// -- snippet --
final Composite compositeArea = new Composite(parent, SWT.NONE);
compositeArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
compositeArea.setLayout(new GridLayout());
final Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL);
// table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// -------------
for (int i = 0; i < 20; ++i) {
new TableItem(table, SWT.NONE).setText(String.valueOf(i));
}
}
public void run() {
shell.setSize(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(final String... args) {
new TableResizeTest().run();
}
}

SWT GridLayout and GridData alignment of composite blocks

I understand that I have layout but I have problem with setting achivementsChild next to each other. I want to make some alignment or something similar. Now they are standing one below other and I want to set one next to the other.
TabItem tbtmStudent = new TabItem(tabFolder_1, SWT.NONE);
tbtmStudent.setText("Student");
ScrolledComposite scrolledComposite_1 = new ScrolledComposite(tabFolder_1, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite_1.addMouseWheelListener(new MouseWheelListener() {
public void mouseScrolled(MouseEvent e) {
scrolledComposite.setFocus();
}
});
tbtmStudent.setControl(scrolledComposite_1);
scrolledComposite_1.setExpandHorizontal(true);
scrolledComposite_1.setExpandVertical(true);
List<STUDENT> allSTUDENTNodes = currentData.getStudentNodes();
Composite studentChild = new Composite(scrolledComposite_1, SWT.NONE);
studentChild.setLayout(new GridLayout());
for(STUDENT studentNode: allSTUDENTNodes){
new STUDENTNode(studentChild, SWT.NONE, studentNode);
Composite achivementsChild = new Composite(scrolledComposite_1, SWT.DEFAULT);
achivementsChild.setLayout(new GridLayout());
for(Achivements achivementsNode: studentNode.getAchivements()){
new AchivementsNode(mscbcChild, SWT.NONE, achivementsNode);
}
}
scrolledComposite_1.setContent(studentChild);
scrolledComposite_1.setMinSize(studentChild.computeSize(SWT.DEFAULT, SWT.DEFAULT));
You can specify how many columns you want when you create the GridLayout:
new GridLayout(numberOfColumns, columnsEqual);
numberofColumns is the number of columns you want.
If columnsEqual is true all columns will have the same width, if it is false they will have their preferred size.

Categories