How to fix a JFace table to the SWT layout? - java

I am creating a breadcrumb control in SWT which shows a bar at the top to search a location on the file system and a JFace table, using a TableViewer, below that bar to display the results i.e the folders and the files at the given location. The problem is once the data is loaded into the table and I try to maximize the window, the table collapses(sometimes disappears totally) leaving a empty space beneath it and making itself scroll-able. Below is the image before maximizing.
And this happens after maximizing the window.
What should be done to correct this? Here is my code for creating the viewer.
DirectoryViewer container = new DirectoryViewer(this, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION);
GridData conData = new GridData();
conData .horizontalAlignment = SWT.FILL;
conData.verticalAlignment = SWT.FILL;
conData .grabExcessHorizontalSpace = true;
conData.grabExcessVerticalSpace = true;
conData .horizontalSpan = 3;
container.getTable().setLayoutData(conData);
And the constructor of DirectoryViewer:
public DirectoryViewer(Composite parent, int style) {
super(parent, style);
Table table = getTable();
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
gridData .horizontalSpan = 3;
table.setLayoutData(gridData);
createColumns();
table.setHeaderVisible(true);
setContentProvider(new ContentProvider());
}

Related

ScrolledCompsite in TabFolder sizes content to zero

I have the following problem:
I'm using SWT to create a GUI for my application. I have a TabFolder in which I add several TabItems and in each of those I create a ScrolledComposite that holds some content.
The TabFolder is displaying fine, however the ScrolledComposite in the TabFolder does only show it's content in the first TabItem. All other ScrolledComposites are visible themselves just fine but their content is invisible.
Here is a little code snippet that demonstrates what I am referring to:
Display display = new Display();
Shell topShell = new Shell(display);
topShell.setSize(800, 800);
topShell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
topShell.setLayout(new FillLayout());
TabFolder folder = new TabFolder(topShell, SWT.NONE);
for (int i = 0; i < 5; i++) {
TabItem item = new TabItem(folder, SWT.NONE);
item.setText("Item " + i);
ScrolledComposite scroller = new ScrolledComposite(folder,
SWT.H_SCROLL | SWT.V_SCROLL );
scroller.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Composite content = new Composite(scroller, SWT.NONE);
content.setBackground(display.getSystemColor(SWT.COLOR_RED));
scroller.setContent(content);
scroller.setExpandHorizontal(true);
scroller.setExpandVertical(true);
item.setControl(scroller);
}
topShell.setVisible(true);
while (!topShell.isDisposed()) {
display.readAndDispatch();
}
You can tell that the content is being displayed if the area is painted red. If the content is invisible the area is blue (the background of the ScrolledComposite)
I'm not sure if that matters but this occurs on Linux Mint 18 and it appears to only happen within GTK 3 (in 2 it works just fine)
After quite some time I tracked the issue down to the following:
It turned out that the problem was that the "missing" content had a size of zero, because the layouting doesn't set the size of those.
In my case it could be fixed by removing the SWT.V_SCROLL and the SWT.H_SCROLL style constants from the ScrolledComposite. Therefore the above code written as following works as expected.
Display display = new Display();
Shell topShell = new Shell(display);
topShell.setSize(800, 800);
topShell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
topShell.setLayout(new FillLayout());
TabFolder folder = new TabFolder(topShell, SWT.NONE);
for (int i = 0; i < 5; i++) {
TabItem item = new TabItem(folder, SWT.NONE);
item.setText("Item " + i);
ScrolledComposite scroller = new ScrolledComposite(folder,
SWT.NONE);
scroller.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Composite content = new Composite(scroller, SWT.NONE);
content.setBackground(display.getSystemColor(SWT.COLOR_RED));
scroller.setContent(content);
scroller.setExpandHorizontal(true);
scroller.setExpandVertical(true);
item.setControl(scroller);
}
topShell.setVisible(true);
while (!topShell.isDisposed()) {
display.readAndDispatch();
}
Although that causes all content to be properly sized it completely removes the ScrollBars of the ScrolledComposite which somehow is not what you want from a ScrolledComposite.
Does anyone know how to fix that or whether that is a bug (that might have been fixed in newer SWT versions)?
I've fixed bugs related to this a few months ago.
Can you try latest SWT master?
http://download.eclipse.org/eclipse/downloads/

How to set width for GridData?

I was building an UI by using GridLayout/GridData. The layout supposes to have 3 section, one is the headingComposite, one is the leftEditorComposite, one is the rightEditorComposite. But my problem here is, I dont want my leftEditorComposite/rightEditorComposite's width layout be changed by the content. (like the name textbox, if the input get longer, then the leftEdtiorComposite's width will become longer too.) Is there a way to make them as a fix size width? thanks a lot!
GridLayout layout = new GridLayout(2, false);
mainComposite.setLayout(layout);
scrolledComposite.setContent(mainComposite);
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
data.horizontalSpan = 2;
headingComposite = new Composite(mainComposite, SWT.BORDER);
headingComposite.setBackground(getBackground());
headingComposite.setLayoutData(data);
data = new GridData(GridData.FILL_BOTH);
leftEditorComposite = new Composite(mainComposite, SWT.BORDER);
leftEditorComposite.setBackground(getBackground());
leftEditorComposite.setLayoutData(data);
data = new GridData(GridData.FILL_BOTH);
rightEditorComposite = new Composite(mainComposite, SWT.BORDER);
rightEditorComposite.setBackground(getBackground());
rightEditorComposite.setLayoutData(data);
If you know the size of each composite, you could provide a widthHint for both your Composite objects.
leftEditorComposite.widthHint = 200;
and
rightEditorComposite.widthHint = 200;
Alternatively, you can read this question, and hopefully you will get your answer:
How to align two composites in a parent composite without using widthHint and heightHint

How I can add size to text in SWT

I create a label with default text of 'data preview'.
When I tried to change the text ( dnyamic ) to diffrerent text it show me the text only in the size of 'data preview'.
How i can control on the size of the label ?
final Composite previewHeader = new Composite(this.previewPanel, SWT.NULL);
final GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.marginHeight = 0;
layout.marginWidth = 0;
previewHeader.setLayout(layout);
final GridData headerData = new GridData();
headerData.horizontalAlignment = GridData.FILL;
headerData.grabExcessHorizontalSpace = true;
previewHeader.setLayoutData(headerData);
this.previewLabel = new RichTextLabel(previewHeader, SWT.LEFT);
this.previewLabel.setText("<b>Data Preview:</b>"); //$NON-NLS-1$
final GridData labeldata = new GridData();
labeldata.grabExcessHorizontalSpace = true;
labeldata.verticalAlignment = GridData.VERTICAL_ALIGN_END;
labeldata.verticalSpan = 0;
labeldata.verticalIndent = 0;
this.previewLabel.setLayoutData(labeldata);
You need to call layout() on the parent of the text. Here is the javadoc of Composite#layout():
If the receiver has a layout, asks the layout to lay out (that is, set the size and location of) the receiver's children. If the receiver does not have a layout, do nothing.
This solves the problem because of the following reason:
The size of each child is determined every time the parent has to lay out. When you first add your label, the parent will initiate the size computation for the child. When you later on change the text, this is not done automatically, so you have to tell the parent to lay out again.

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