Solved

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

Posted on 2013-06-19
1
281 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
nestparen challenge 4 56
pairs challenge 5 44
compre toata in where clue oracle 4 41
Java Jpanels and Jframe 8 20
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now