Solved

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

Posted on 2013-06-19
1
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

751 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