本文介紹了調整ScroldComposite中擴展欄SWT的大小的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在構建一個java SWT獨立應用程序,在滾動組合中有一個展開欄。在某些情況下,我將向其中一個組件添加一個字段。因此,項目的大小將會增加。首先,我銷毀擴展欄中的所有當前項目:
public void closeAll(){
for(int a = bar.getItemCount()-1; a>=0; a--){
bar.getItem(a).getControl().dispose();
bar.getItem(a).dispose();
}
}
然后我將把它們全部再讀一遍(我將組合存儲在其他地方)。我重新計算每一項的高度:
for(int a = 0; a<bar.getItemCount(); a++){
bar.getItem(a).setHeight(bar.getItem(a).getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
}
當我執行所有這些操作時,滾動的組合將不會進一步向下滾動(展開欄的一部分現在位于ScrolledComposite的工作區之外)。
當我調整窗口本身的大小時,所有問題都解決了。那么,我如何才能在沒有代碼的情況下強制執行此通常與調整窗口大小相關的更新?
提前感謝大家!
//編輯
我已經添加了我所說的類:
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;
}
}
我試著做一個例子,但是這個例子開箱即用。我看不出有什么不同。InputBlock是保存有關復合中所需字段的數據的類,如果調用它們將生成復合。
包含ScrolledComposite的復合體當前處于SashForm狀態,我不知道這是否會有什么不同?
我收到此錯誤(僅在Linux上),但不是在我嘗試的示例中:
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
更新
我把我的問題做了一個更好的例子。此代碼正在運行并為我生成相同的錯誤:
(SWT:6765): Gtk-CRITICAL **: IA__gtk_widget_map: assertion `gtk_widget_get_visible (widget)' failed
我使用過以下代碼:
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
}
}
我曾嘗試在Ubuntu和openSUSE上運行此程序,兩者都給出了相同的GTK錯誤。我希望有人能幫助我!
推薦答案
好的,我找到了解決辦法。這并不是真正修復問題的解決方案,而是避免問題的方法。
您的問題似乎是由展開ExpandItem
時出現的延遲引起的。因此,更新minSize的代碼使用未展開的大小。在設置minSize之前添加延遲可以解決您的問題。嘗試調整延遲值,但300ms似乎工作正常,并不會真正影響可用性:
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);
}
});
}
這篇關于調整ScroldComposite中擴展欄SWT的大小的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,