在 ScrolledComposite 中有两个复合材料
Having Two Composites inside of a ScrolledComposite
我正在尝试创建一个图例,我希望这样做的方式是有两个单独的复合材料,一个在滚动复合材料内部的另一个之上。顶部复合子项是标签和 canvas 的另一个复合项。该复合材料是从属性文件中填充的。第二个复合体与第一个相同,子代是同一种复合体。我将如何实现这一目标。
下面的代码所做的是在读取文件时创建新的合成并填充主合成。我希望最后三个图例项与第一个图例项分开并充当第一个组合。
@Override
public void createPartControl(Composite parent)
{
display = parent.getDisplay();
parent.setLayout(new FillLayout());
// This enables the display to be able to be scrollable when needed
scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
// making a new composite inside of the scrolledComposite to be to add labels and canvases
stepComposite = new Composite(scrolledComposite, SWT.NONE);
blockedComposite = new Composite(scrolledComposite, SWT.NONE);
// making the layout for the composite a row so when the the children reach the end of the shell
// the children will wrap down to the next level
RowLayout layout = new RowLayout();
layout.pack = false;
layout.spacing = 5;
stepComposite.setLayout(layout);
blockedComposite.setLayout(layout);
}
/**
* Adding a new composite for a rectangle and label to be added to the legend
* @param legendMessage
*/
public static void addNewComposite(String legendMessage, int compositePosition)
{
final String message = legendMessage;
final String[] s = message.split(",");
final int position = compositePosition;
if (display != null)
{
display.syncExec(new Runnable()
{
@Override
public void run()
{
Composite com;
if (position == 1)
{
com = new Composite(blockedComposite, SWT.NONE);
}
else
{
com = new Composite(stepComposite, SWT.NONE);
}
// Composite com = new Composite(stepComposite, SWT.NONE);
com.setLayout(new GridLayout(2, false));
// Creating the color using the RBG values
final Color color =
new Color(display, Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2]));
// Creating a canvas for which the rectangle can be drawn on
Canvas canvas = new Canvas(com, SWT.NONE);
canvas.setLayoutData(new GridData(50,40));
// TODO Maybe set the bounds of the canvas
canvas.addPaintListener(new PaintListener()
{
public void paintControl(PaintEvent e)
{
e.gc.drawRectangle(1, 1, 48, 38);
e.gc.setBackground(color);
e.gc.fillRectangle(2, 2, 47, 37);
}
});
// Creating a label and setting the font
Label label = new Label(com, SWT.NULL);
final Font boldFont = new Font(label.getDisplay(), new FontData("Arial", 12, SWT.BOLD));
label.setFont(boldFont);
label.setText(s[3]);
// Adding a color and font disposer to the composite
com.addDisposeListener(new DisposeListener()
{
public void widgetDisposed(DisposeEvent e)
{
color.dispose();
boldFont.dispose();
}
});
// Adding the composite into a map to be able to be deleted when model stops
comps.put(s[3], com);
if (position == 1)
{
// scrolledComposite.setContent(blockedComposite);
// scrolledComposite.setMinSize(blockedComposite.computeSize(1, 1000));
}
else
{
scrolledComposite.setContent(stepComposite);
scrolledComposite.setMinSize(stepComposite.computeSize(1, 1000));
}
// Adding the composite to the scrolledComposite to be able to appear
// scrolledComposite.setContent(stepComposite);
// Setting the size of the scrolledComposite
// scrolledComposite.setMinSize(stepComposite.computeSize(1, 1000));
}
});
}
}
此代码用第二个复合材料替换了第一个复合材料,我希望有新的眼光帮助我找出问题所在。
ScrolledComposite
仅显示您在 setContent
上指定的 Composite
,您不能在该级别有多个组合。
使用一个 Composite
作为 ScrolledComposite
的直接子项,并将您的其他组合添加为该单个组合的子项。不要忘记在所有合成上设置布局。
我正在尝试创建一个图例,我希望这样做的方式是有两个单独的复合材料,一个在滚动复合材料内部的另一个之上。顶部复合子项是标签和 canvas 的另一个复合项。该复合材料是从属性文件中填充的。第二个复合体与第一个相同,子代是同一种复合体。我将如何实现这一目标。 下面的代码所做的是在读取文件时创建新的合成并填充主合成。我希望最后三个图例项与第一个图例项分开并充当第一个组合。
@Override
public void createPartControl(Composite parent)
{
display = parent.getDisplay();
parent.setLayout(new FillLayout());
// This enables the display to be able to be scrollable when needed
scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
// making a new composite inside of the scrolledComposite to be to add labels and canvases
stepComposite = new Composite(scrolledComposite, SWT.NONE);
blockedComposite = new Composite(scrolledComposite, SWT.NONE);
// making the layout for the composite a row so when the the children reach the end of the shell
// the children will wrap down to the next level
RowLayout layout = new RowLayout();
layout.pack = false;
layout.spacing = 5;
stepComposite.setLayout(layout);
blockedComposite.setLayout(layout);
}
/**
* Adding a new composite for a rectangle and label to be added to the legend
* @param legendMessage
*/
public static void addNewComposite(String legendMessage, int compositePosition)
{
final String message = legendMessage;
final String[] s = message.split(",");
final int position = compositePosition;
if (display != null)
{
display.syncExec(new Runnable()
{
@Override
public void run()
{
Composite com;
if (position == 1)
{
com = new Composite(blockedComposite, SWT.NONE);
}
else
{
com = new Composite(stepComposite, SWT.NONE);
}
// Composite com = new Composite(stepComposite, SWT.NONE);
com.setLayout(new GridLayout(2, false));
// Creating the color using the RBG values
final Color color =
new Color(display, Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2]));
// Creating a canvas for which the rectangle can be drawn on
Canvas canvas = new Canvas(com, SWT.NONE);
canvas.setLayoutData(new GridData(50,40));
// TODO Maybe set the bounds of the canvas
canvas.addPaintListener(new PaintListener()
{
public void paintControl(PaintEvent e)
{
e.gc.drawRectangle(1, 1, 48, 38);
e.gc.setBackground(color);
e.gc.fillRectangle(2, 2, 47, 37);
}
});
// Creating a label and setting the font
Label label = new Label(com, SWT.NULL);
final Font boldFont = new Font(label.getDisplay(), new FontData("Arial", 12, SWT.BOLD));
label.setFont(boldFont);
label.setText(s[3]);
// Adding a color and font disposer to the composite
com.addDisposeListener(new DisposeListener()
{
public void widgetDisposed(DisposeEvent e)
{
color.dispose();
boldFont.dispose();
}
});
// Adding the composite into a map to be able to be deleted when model stops
comps.put(s[3], com);
if (position == 1)
{
// scrolledComposite.setContent(blockedComposite);
// scrolledComposite.setMinSize(blockedComposite.computeSize(1, 1000));
}
else
{
scrolledComposite.setContent(stepComposite);
scrolledComposite.setMinSize(stepComposite.computeSize(1, 1000));
}
// Adding the composite to the scrolledComposite to be able to appear
// scrolledComposite.setContent(stepComposite);
// Setting the size of the scrolledComposite
// scrolledComposite.setMinSize(stepComposite.computeSize(1, 1000));
}
});
}
}
此代码用第二个复合材料替换了第一个复合材料,我希望有新的眼光帮助我找出问题所在。
ScrolledComposite
仅显示您在 setContent
上指定的 Composite
,您不能在该级别有多个组合。
使用一个 Composite
作为 ScrolledComposite
的直接子项,并将您的其他组合添加为该单个组合的子项。不要忘记在所有合成上设置布局。