Reputation: 15
Is it possible to place a label on top of another label? I have a label with a specific background color and I want to place a second label on top of that with a different background color.
Is that possible? I would like smallBar to be on top of bigBar. I am changing the size of smallBar on various events so I would like it to be on top constantly.
public class GuessBarComposite extends Composite{
public GuessBarComposite(Composite shell, int style){
super(shell,style);
bigBar = new Label(this,SWT.NONE);
smallBar = new Label(this, SWT.NONE);
Color outOfRangeColor= new Color(Display.getDefault(), 139,0,0);
Color inRangeColor= new Color(Display.getDefault(), 255,140,0);
bigBar.setBounds(labelOffset,20,barWidth, barHeight);
bigBar.setBackground(outOfRangeColor);
smallBar.setBounds(labelOffset,20,barWidth-20, barHeight);
smallBar.setBackground(inRangeColor);
}
}
Upvotes: 0
Views: 703
Reputation: 36894
You can use a StackLayout
to position the Label
s on top of each other and then you can switch between them by setting StackLayout#topControl
. Here is an example:
public static void main(String args[])
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
final StackLayout stackLayout = new StackLayout();
final Composite stack = new Composite(shell, SWT.NONE);
stack.setLayout(stackLayout);
Label bottom = new Label(stack, SWT.NONE);
bottom.setText("Bottom");
Label top = new Label(stack, SWT.NONE);
top.setText("Top");
stackLayout.topControl = top;
Button button = new Button(shell, SWT.PUSH);
button.setText("Switch");
button.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
stackLayout.topControl = stackLayout.topControl.equals(top) ? bottom : top;
stack.layout(true, true);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!shell.getDisplay().readAndDispatch())
shell.getDisplay().sleep();
}
}
If you simply want them to appear one after the other (on top in terms of y position), then use a GridLayout
:
public static void main(String args[])
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
Label bottom = new Label(shell, SWT.NONE);
bottom.setText("Bottom");
Label top = new Label(shell, SWT.NONE);
top.setText("Top");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!shell.getDisplay().readAndDispatch())
shell.getDisplay().sleep();
}
}
Upvotes: 1