Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Is it possible to create image & text button
Is it possible to create image & text button [message #370565] |
Tue, 03 June 2003 11:16  |
Eclipse User |
|
|
|
Originally posted by: mobiko.lycos.com
Hi,
I know we can create image button using the setImage(). My question is can
we also create an "image+text" button in ToolBar? There is an API
setText() in the Button class, but somehow, it doesn't work. Here's
snippet of my code.
item = new ToolItem(toolBar, SWT.FLAT);
item.setImage(ImageCache.stockImages[Constants.UNDERLINEIMAG E]);
item.setToolTipText(Resource.getI18NString("app.toolbar.tooltips.underline ",
null));
Thanks in advance.
cheers,
mobiko
|
|
|
Re: Is it possible to create image & text button [message #370593 is a reply to message #370565] |
Tue, 03 June 2003 23:13   |
Eclipse User |
|
|
|
Have you pasted the correct code?
Looking at your code, you're not calling the setText() method on the
ToolItem.
You call setToolTipText(), which defines what pops up on a mouseover (a
tooltip). Calling setText() instead would define the text to display
(depending on the ToolBar's style) beside/under the image in the ToolItem .
Cheers,
Nick Maynard
ncm99@doc.ic.ac.uk
"mobiko" <mobiko@lycos.com> wrote in message
news:bbie40$qju$1@rogue.oti.com...
> Hi,
>
> I know we can create image button using the setImage(). My question is can
> we also create an "image+text" button in ToolBar? There is an API
> setText() in the Button class, but somehow, it doesn't work. Here's
> snippet of my code.
>
> item = new ToolItem(toolBar, SWT.FLAT);
> item.setImage(ImageCache.stockImages[Constants.UNDERLINEIMAG E]);
>
item.setToolTipText(Resource.getI18NString("app.toolbar.tooltips.underline ",
> null));
>
> Thanks in advance.
>
> cheers,
> mobiko
>
|
|
|
Re: Is it possible to create image & text button [message #370617 is a reply to message #370593] |
Wed, 04 June 2003 11:12   |
Eclipse User |
|
|
|
Originally posted by: mobiko.lycos.com
Sorrie... see this code snippet. IT seems that when I set the layout for
Tool Bar using Grid Layout, the numColumns fields does not really
necessary mean NO. of COLUMNS, but it actually means no. of PIXELs.
I can't seem to align my buttons correctly, in particular the Preview
Button occupies quite a large space, it's width is equal to the width of
the widest button (e.g. Upload - a button with image and text). This is
despite me already setting "makeColumnsEqualWidth" to false. What is wrong
with my code?
Does ToolBar needs to define its layout first before we start to populate
the tool items?
Many thanks!
Code Snippet
============
toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
GridLayout layout = new GridLayout();
layout.numColumns = 400;
layout.marginHeight = 1;
layout.marginWidth = 1;
layout.horizontalSpacing = 1;
layout.makeColumnsEqualWidth = false;
toolBar.setLayout(layout);
item = new ToolItem(toolBar, SWT.NONE);
item.setText("Blogs");
// Create Combo Box - Populate with Blogs IDs
item = new ToolItem(toolBar, SWT.SEPARATOR);
Combo combo = new Combo(toolBar, SWT.READ_ONLY);
combo.setBackground(Constants.WHITE);
combo.setLayoutData(new GridData());
item.setWidth(100);
item.setControl(combo);
item = new ToolItem(toolBar, SWT.SEPARATOR);
// Upload Button
item = new ToolItem(toolBar, SWT.FLAT | SWT.PUSH);
item.setImage(ImageCache.stockImages[Constants.UPLOADIMAGE]) ;
item.setToolTipText(Resource.getI18NString("app.toolbar.tooltips.upload ",
null));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// TODO: Upload
}
});
item.setText("Upload");
// Preview Button
item = new ToolItem(toolBar, SWT.FLAT | SWT.PUSH);
item.setImage(ImageCache.stockImages[Constants.PREVIEWIMAGE] );
item.setToolTipText(Resource.getI18NString("app.toolbar.tooltips.preview ",
null));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// TODO: Preview
}
});
// Post Button
item = new ToolItem(toolBar, SWT.FLAT | SWT.PUSH);
item.setImage(ImageCache.stockImages[Constants.POSTIMAGE]);
item.setToolTipText(Resource.getI18NString("app.toolbar.tooltips.post ",
null));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// TODO: POST
}
});
item.setText("Post");
// Pack Tool Bar
toolBar.pack();
Nick Maynard wrote:
> Have you pasted the correct code?
> Looking at your code, you're not calling the setText() method on the
> ToolItem.
> You call setToolTipText(), which defines what pops up on a mouseover (a
> tooltip). Calling setText() instead would define the text to display
> (depending on the ToolBar's style) beside/under the image in the ToolItem .
|
|
|
Re: Is it possible to create image & text button [message #370672 is a reply to message #370617] |
Thu, 05 June 2003 13:30   |
Eclipse User |
|
|
|
Others please feel free to step in and correct any mistakes...
Unfortunately it seems (according to others on the newsgroup, and my own
experiments) that ToolItems that belong to a ToolBar will always take their
image width to be that of the image of the first ToolItem. The same with
text, unfortunately.
All this means is that you can't have a mix of image- and text-based
ToolItems on a ToolBar, as they'll look stupid.
Personally I think this is silly, but that's how it goes.
Also, I don't think you want to be using a Layout for your toolbar... here's
a little snippet of what I do...
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
/**
* @author Nick
*/
public class Test {
public static void main(String[] args) {
Display display = new Display();
// Create the main window (Shell)
Shell shell = new Shell(display);
shell.setText("Cows Rule");
shell.setSize(640, 480);
// Create a very basic layout to contain our canvas for now
GridLayout layout = new GridLayout();
layout.numColumns = 2;
shell.setLayout(layout);
// Funky toolbar
GridData toolbarData = new GridData(GridData.FILL_HORIZONTAL);
toolbarData.horizontalSpan = 2;
ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
ToolItem toolItem;
toolItem = new ToolItem(toolBar, SWT.PUSH);
toolItem.setText("Cows");
toolItem = new ToolItem(toolBar, SWT.PUSH);
toolItem.setText("Rule");
toolBar.pack();
// Open our window
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
As you can see, we have a GridLayout for the Shell, then we pop a ToolBar
into a new GridData, where we set the correct number of column spans (in
this case 2).
Hope this helps a little,
Nick
"mobiko" <mobiko@lycos.com> wrote in message
news:bbl29k$4v1$1@rogue.oti.com...
> Sorrie... see this code snippet. IT seems that when I set the layout for
> Tool Bar using Grid Layout, the numColumns fields does not really
> necessary mean NO. of COLUMNS, but it actually means no. of PIXELs.
>
> I can't seem to align my buttons correctly, in particular the Preview
> Button occupies quite a large space, it's width is equal to the width of
> the widest button (e.g. Upload - a button with image and text). This is
> despite me already setting "makeColumnsEqualWidth" to false. What is wrong
> with my code?
>
> Does ToolBar needs to define its layout first before we start to populate
> the tool items?
>
> Many thanks!
>
> Code Snippet
> ============
>
> toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
>
> GridLayout layout = new GridLayout();
> layout.numColumns = 400;
> layout.marginHeight = 1;
> layout.marginWidth = 1;
> layout.horizontalSpacing = 1;
> layout.makeColumnsEqualWidth = false;
>
> toolBar.setLayout(layout);
>
> item = new ToolItem(toolBar, SWT.NONE);
> item.setText("Blogs");
>
> // Create Combo Box - Populate with Blogs IDs
> item = new ToolItem(toolBar, SWT.SEPARATOR);
> Combo combo = new Combo(toolBar, SWT.READ_ONLY);
> combo.setBackground(Constants.WHITE);
> combo.setLayoutData(new GridData());
> item.setWidth(100);
> item.setControl(combo);
>
> item = new ToolItem(toolBar, SWT.SEPARATOR);
>
> // Upload Button
>
> item = new ToolItem(toolBar, SWT.FLAT | SWT.PUSH);
> item.setImage(ImageCache.stockImages[Constants.UPLOADIMAGE]) ;
> item.setToolTipText(Resource.getI18NString("app.toolbar.tooltips.upload ",
> null));
> item.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent e) {
> // TODO: Upload
> }
> });
>
> item.setText("Upload");
>
> // Preview Button
>
> item = new ToolItem(toolBar, SWT.FLAT | SWT.PUSH);
> item.setImage(ImageCache.stockImages[Constants.PREVIEWIMAGE] );
> item.setToolTipText(Resource.getI18NString("app.toolbar.tooltips.preview ",
> null));
> item.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent e) {
> // TODO: Preview
> }
> });
>
> // Post Button
>
> item = new ToolItem(toolBar, SWT.FLAT | SWT.PUSH);
> item.setImage(ImageCache.stockImages[Constants.POSTIMAGE]);
> item.setToolTipText(Resource.getI18NString("app.toolbar.tooltips.post ",
> null));
> item.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent e) {
> // TODO: POST
> }
> });
>
> item.setText("Post");
>
> // Pack Tool Bar
> toolBar.pack();
>
> Nick Maynard wrote:
>
> > Have you pasted the correct code?
> > Looking at your code, you're not calling the setText() method on the
> > ToolItem.
> > You call setToolTipText(), which defines what pops up on a mouseover (a
> > tooltip). Calling setText() instead would define the text to display
> > (depending on the ToolBar's style) beside/under the image in the
ToolItem .
>
>
|
|
| |
Re: Is it possible to create image & text button [message #370748 is a reply to message #370691] |
Mon, 09 June 2003 09:16   |
Eclipse User |
|
|
|
Originally posted by: mobiko.lycos.com
I found my solution in CLabel. Yes, you can set Text + Image button in
ToolBar... see this code snippet.
item = new ToolItem(toolBar, SWT.SEPARATOR);
CLabel label = new CLabel(toolBar, SWT.SHADOW_NONE);
label.setText(Resource.getI18NString("app.toolbar.blog", null));
label.setImage(ImageCache.stockImages[Constants.BLOGIMAGE]);
label.setAlignment(SWT.CENTER);
item.setControl(label);
item.setWidth(80);
mobiko wrote:
> This must be a bug then. Why can\'t Toolbar use GridLayout when there is
> an API provided in the class to set Layout.
> Your test reveals some interesting findings.
> I tried using GridLayout for my toolbar, thinking that I can then do a
> \"setLayoutData\" on the components to adjust the width/height.
> Nick Maynard wrote:
> > Others please feel free to step in and correct any mistakes...
> > Unfortunately it seems (according to others on the newsgroup, and my own
> > experiments) that ToolItems that belong to a ToolBar will always take their
> > image width to be that of the image of the first ToolItem. The same with
> > text, unfortunately.
> > All this means is that you can\'t have a mix of image- and text-based
> > ToolItems on a ToolBar, as they\'ll look stupid.
> > Personally I think this is silly, but that\'s how it goes.
> > Also, I don\'t think you want to be using a Layout for your toolbar...
> here\'s
> > a little snippet of what I do...
|
|
|
Re: Is it possible to create image & text button [message #370758 is a reply to message #370748] |
Mon, 09 June 2003 11:38  |
Eclipse User |
|
|
|
I'm glad you managed to solve your problem!
"mobiko" <mobiko@lycos.com> wrote in message
news:bc21bc$8gq$1@rogue.oti.com...
> I found my solution in CLabel. Yes, you can set Text + Image button in
> ToolBar... see this code snippet.
>
> item = new ToolItem(toolBar, SWT.SEPARATOR);
> CLabel label = new CLabel(toolBar, SWT.SHADOW_NONE);
> label.setText(Resource.getI18NString("app.toolbar.blog", null));
> label.setImage(ImageCache.stockImages[Constants.BLOGIMAGE]);
> label.setAlignment(SWT.CENTER);
> item.setControl(label);
> item.setWidth(80);
>
> mobiko wrote:
>
> > This must be a bug then. Why can\'t Toolbar use GridLayout when there is
> > an API provided in the class to set Layout.
>
> > Your test reveals some interesting findings.
>
> > I tried using GridLayout for my toolbar, thinking that I can then do a
> > \"setLayoutData\" on the components to adjust the width/height.
>
> > Nick Maynard wrote:
>
> > > Others please feel free to step in and correct any mistakes...
>
> > > Unfortunately it seems (according to others on the newsgroup, and my
own
> > > experiments) that ToolItems that belong to a ToolBar will always take
their
> > > image width to be that of the image of the first ToolItem. The same
with
> > > text, unfortunately.
>
> > > All this means is that you can\'t have a mix of image- and text-based
> > > ToolItems on a ToolBar, as they\'ll look stupid.
> > > Personally I think this is silly, but that\'s how it goes.
>
> > > Also, I don\'t think you want to be using a Layout for your toolbar...
> > here\'s
> > > a little snippet of what I do...
>
>
>
>
>
|
|
|
Goto Forum:
Current Time: Sun Sep 21 18:48:53 EDT 2025
Powered by FUDForum. Page generated in 0.03861 seconds
|