Solved

How to place an icon in the center in a toolbar in SWT?

Posted on 2013-06-19
1
287 Views
Last Modified: 2013-06-21
I am working on a GUI and an icon that I use in the toolbar is not in the center.

How can I put the icon in the center? I am sending a screen shot in the attachment.

This is the code that I use to add this icon:

        CustomToolbar custToolBar11 = new CustomToolbar(compTB1, SWT.FLAT, "Configuration");
        
        item12 = new ToolItem(custToolBar11.getToolBar(), SWT.NONE);
        item12.setImage(image_floppy_disk); item12.setText("Save"); item12.setToolTipText("Sthg");

Open in new window


And this is the CustomToolbar class that I use:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;


public class CustomToolbar extends Composite {
    private ToolBar toolBar;
    private Label caption;
       
    public CustomToolbar(Composite parent, int style, String toolBarCaption) {
        super(parent, style);
        Font font = new Font(parent.getDisplay(),"Arial",10,SWT.ITALIC);
        final Color lightgray;
        lightgray = new Color (parent.getDisplay(), 140, 140, 140);
        
        setLayout(new GridLayout());
        toolBar = new ToolBar(this, SWT.FLAT);
        caption = new Label(this, SWT.HORIZONTAL | SWT.CENTER);
        caption.setText(toolBarCaption);
        caption.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // center the caption 
        caption.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        caption.setForeground(lightgray);
        caption.setFont(font);
        toolBar.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
    }

    public ToolBar getToolBar() {
        return toolBar;
    }

    public Label getCaption() {
        return caption;
    }

}

Open in new window

icon-center.png
0
Comment
Question by:Tolgar
1 Comment
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39264337
right after instantiation of toolBar add this two lines:

GridData tbGD = new GridData(); tbGD.horizontalAlignment = SWT.CENTER;
toolBar.setLayoutData(tbGD);
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 213
couple of eclipse 5 46
Delete Android all or certain applications data programmatically 9 75
Java: anonymous class 4 29
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question