I am building an java SWT standalone application with an expandbar inside a scrolledcomposite. At some point i will add a field to one of the components. Hence the size of the item will grow. First i destroy all the current items inside the expandbar:
public void closeAll(){
for(int a = bar.getItemCount()-1; a>=0; a--){
bar.getItem(a).getControl().dispose();
bar.getItem(a).dispose();
}
}
Then i will readd them all again ( i have the composites stored somewhere else). And i recompute the height of each item:
for(int a = 0; a<bar.getItemCount(); a++){
bar.getItem(a).setHeight(bar.getItem(a).getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
}
And when i do this all, the scrolled composite will not scroll further down (a part of the expandbar is now outside the client area of the ScrolledComposite).
When i resize the window itself all the problems are solved. So how can i force this update normally assosiated with resizing of windows from without the code?
Thank you all in advance!
//edit
I have added the class i am talking about:
public class InputPanel extends Panel implements Refresh{
private Editor parent;
private ExpandBar expandBar;
private ArrayList<InputBlock> blocks;
public ScrolledComposite scroll;
public InputPanel(Editor parent, Composite panel) {
super(panel);
this.parent = parent;
setGridLayout(0,0,1);
blocks = new ArrayList<InputBlock>();
init();
}
private void init(){
//two pannels first button pannel
Panel buttonPanel = new Panel(new Composite(panel, SWT.BORDER));
buttonPanel.setGridLayout(0, 0, 3);
buttonPanel.fillGridData();
Composite barComp = new Composite(panel, SWT.BORDER);
barComp.setLayout(new FillLayout());
GridData data1 = new GridData();
data1.grabExcessHorizontalSpace = true;
data1.horizontalAlignment = SWT.FILL;
data1.verticalAlignment = SWT.FILL;
barComp.setLayoutData(data1);
scroll = new ScrolledComposite(barComp, SWT.V_SCROLL);
expandBar = new ExpandBar(scroll, SWT.None);
GridData data = new GridData();
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
data.horizontalAlignment = SWT.FILL;
data.verticalAlignment = SWT.TOP;
scroll.setContent(expandBar);
scroll.setExpandVertical(true);
scroll.setExpandHorizontal(true);
scroll.setAlwaysShowScrollBars(true);
for(int a = 0; a<Main.database.size(); a++){
Composite composite = new Composite(expandBar, SWT.BORDER);
blocks.add(new InputBlock(this, composite, Main.database.get(a)));
addItem(composite, Main.database.get(a).name);
}
refreshHeights();
scroll.setMinSize(expandBar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scroll.layout(true);
}
public void addItem(Composite composite, String tabName){
ExpandItem expandItem = new ExpandItem(expandBar, SWT.None);
expandItem.setText(tabName);
expandItem.setControl(composite);
expandItem.setExpanded(true);
composite.setVisible(true);
}
public void refreshHeights(){
for(int a = 0; a<expandBar.getItemCount(); a++){
expandBar.getItem(a).setHeight(expandBar.getItem(a).getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
}
}
public void closeAll(){
ExpandItem[] items = expandBar.getItems();
System.out.println(items.length);
for(int a = items.length-1; a>=0; a--){
items[a].getControl().dispose();
items[a].dispose();
}
}
public void refresh(){
closeAll();
blocks = new ArrayList<InputBlock>();
for(int a = 0; a<Main.database.size(); a++){
Composite composite = new Composite(expandBar, SWT.BORDER);
blocks.add(new InputBlock(this, composite, Main.database.get(a)));
addItem(composite, Main.database.get(a).name);
}
refreshHeights();
scroll.setMinSize(expandBar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scroll.layout(true);
}
public Editor getParent(){
return parent;
}
public void setEditorMenu(boolean show){
for(int a = 0; a<blocks.size(); a++){
blocks.get(a).setEditorMenu(show);
}
}
public ArrayList<InputBlock> getBlocks(){
return blocks;
}
public boolean setValue(DatabaseField field, String value){
value = value.trim();
for(int a = 0; a<blocks.size(); a++){
if(blocks.get(a).setValue(field, value)){
return true;
}
}
return false;
}
}
I tried to make an example, however the example worked out of the box. I can't see what the difference is. The InputBlocks are classes that hold data about the fields needed in a composite, if called they will produce an composite.
The composite holding the ScrolledComposite is currently in as SashForm, i don't know if this will make any difference?
And i get this error (on Linux only), but not in the example i tried:
SWT:12455): Gtk-CRITICAL **: IA__gtk_widget_map: assertion `gtk_widget_get_visible (widget)' failed
(SWT:12455): Gtk-CRITICAL **: IA__gtk_widget_map: assertion `gtk_widget_get_visible (widget)' failed
(SWT:12455): Gtk-CRITICAL **: IA__gtk_widget_map: assertion `gtk_widget_get_visible (widget)' failed
UPDATE
I made a better example of my problem. This code is running and generating the same error for me:
(SWT:6765): Gtk-CRITICAL **: IA__gtk_widget_map: assertion `gtk_widget_get_visible (widget)' failed
I've used this code:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class ExpandBarExample implements SelectionListener{
public static void main(String[] args){
Display display = new Display();
Shell shell = new Shell(display);
new ExpandBarExample(shell);
shell.setMaximized(true);
shell.setLayout(new FillLayout());
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
private ExpandBar bar;
private ScrolledComposite scroll;
private Composite composite;
private SashForm sash;
private CTabFolder folder;
private Button button;
public ExpandBarExample(Composite composite){
this.composite = composite;
init();
}
public void init(){
composite.setLayout(new FillLayout());
sash = new SashForm(composite, SWT.VERTICAL);
folder = new CTabFolder(sash, SWT.None);
scroll = new ScrolledComposite(folder, SWT.V_SCROLL);
GridData data = new GridData();
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
bar = new ExpandBar(scroll, SWT.NONE);
scroll.setContent(bar);
scroll.setExpandVertical(true);
scroll.setExpandHorizontal(true);
scroll.setAlwaysShowScrollBars(true);
CTabItem tab = new CTabItem(folder, SWT.None);
tab.setControl(scroll);
tab.setText("TEST");
Composite sashFill = new Composite(sash, SWT.BORDER);
sashFill.setLayout(new FillLayout());
button = new Button(sashFill, SWT.PUSH);
button.addSelectionListener(this);
button.setText("Add block");
sash.setWeights(new int[]{3,1});
for(int a = 0; a<10; a++){
Composite composite = new Composite(bar, SWT.BORDER);
composite.setLayout(new GridLayout(2, false));
for(int b = 0; b<10; b++){
Label label = new Label(composite, SWT.BORDER);
label.setText("TEST");
}
addItem(composite, "Item " + a);
}
computeHeights();
scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scroll.layout(true);
//delete old panels
removeAll();
//trying to add more panels
for(int a = 0; a<10; a++){
Composite composite = new Composite(bar, SWT.BORDER);
composite.setLayout(new GridLayout(2, false));
for(int b = 0; b<20; b++){
Label label = new Label(composite, SWT.BORDER);
label.setText("TEST");
}
addItem(composite, "Item " + a);
}
computeHeights();
scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scroll.layout(true);
}
public void refresh(){
}
public void removeAll(){
ExpandItem[] items = bar.getItems();
System.out.println(items.length);
for(int a = items.length-1; a>=0; a--){
items[a].getControl().dispose();
items[a].dispose();
}
}
public void addItem(Composite composite, String tabName){
ExpandItem expandItem = new ExpandItem(bar, SWT.None);
expandItem.setText(tabName);
expandItem.setControl(composite);
expandItem.setExpanded(true);
}
public void computeHeights(){
for(int a = 0; a<bar.getItemCount(); a++){
bar.getItem(a).setHeight(bar.getItem(a).getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
}
}
@Override
public void widgetSelected(SelectionEvent e) {
if(e.getSource() == button){
Composite comp = new Composite(bar, SWT.BORDER);
comp.setLayout(new GridLayout(2, false));
for(int a = 0; a<10; a++){
Label label = new Label(comp, SWT.BORDER);
label.setText("TEST");
}
comp.layout(true);
addItem(comp, "ADDED");
computeHeights();
scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
scroll.layout(true, true);
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
}
I have tried to run this on Ubuntu and OpenSuse, both give the same Gtk error. I hope somebody can help me!
Alright, I found a workaround. It's not really a solution that fixes the problem, but rather something that avoids it.
Your problem seems to be caused by a delay that occurs when expanding the ExpandItem
. Consequently, your code updating the minSize uses the unexpanded size. Adding a delay before setting the minSize solves your issue. Try playing around with the delay value, but 300ms seems to be working just fine and doesn't really interfere with usability:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
new ExpandBarExample(shell);
shell.setMaximized(true);
shell.setLayout(new FillLayout());
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
private ExpandBar bar;
private ScrolledComposite scroll;
private Composite composite;
private SashForm sash;
private CTabFolder folder;
private Button button;
public ExpandBarExample(Composite composite)
{
this.composite = composite;
init();
}
public void init()
{
composite.setLayout(new FillLayout());
sash = new SashForm(composite, SWT.VERTICAL);
folder = new CTabFolder(sash, SWT.None);
scroll = new ScrolledComposite(folder, SWT.V_SCROLL);
GridData data = new GridData();
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
bar = new ExpandBar(scroll, SWT.NONE);
scroll.setContent(bar);
scroll.setExpandVertical(true);
scroll.setExpandHorizontal(true);
scroll.setAlwaysShowScrollBars(true);
CTabItem tab = new CTabItem(folder, SWT.None);
tab.setControl(scroll);
tab.setText("TEST");
Composite sashFill = new Composite(sash, SWT.BORDER);
sashFill.setLayout(new FillLayout());
button = new Button(sashFill, SWT.PUSH);
button.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
Composite comp = new Composite(bar, SWT.BORDER);
comp.setLayout(new GridLayout(2, false));
for (int a = 0; a < 10; a++)
{
Label label = new Label(comp, SWT.BORDER);
label.setText("TEST");
}
comp.layout(true);
addItem(comp, "ADDED");
scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
scroll.layout(true, true);
}
});
button.setText("Add block");
sash.setWeights(new int[] { 3, 1 });
scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scroll.layout(true);
}
public void refresh()
{
}
public void removeAll()
{
ExpandItem[] items = bar.getItems();
System.out.println(items.length);
for (int a = items.length - 1; a >= 0; a--)
{
items[a].getControl().dispose();
items[a].dispose();
}
}
public void addItem(Composite composite, String tabName)
{
final ExpandItem expandItem = new ExpandItem(bar, SWT.None);
expandItem.setText(tabName);
expandItem.setControl(composite);
expandItem.setExpanded(true);
expandItem.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
Display.getDefault().timerExec(300, new Runnable()
{
@Override
public void run()
{
scroll.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
scroll.layout(true, true);
}
});
}