Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Custom widget
Custom widget [message #461271] Tue, 20 September 2005 08:56
Henrik Skovgaard is currently offline Henrik SkovgaardFriend
Messages: 84
Registered: July 2009
Member
Hi

I've made a custom widget that consists of a Text and a Label.

I followed the example from the page
http://www.eclipse.org/articles/Article-Writing%20Your%20Own %20Widget/Writing%20Your%20Own%20Widget.htm

Now when I set the widthHint to fx 200 (n) on the layout, I would like the
Text widget inside my custom widget to stretch to 184 (n-16) and the label
should constantly be 16. How do I do this?

Here is my code:

=== begin
import gui.shell.J6101;

import java.io.IOException;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;

import com.cloudgarden.resource.SWTResourceManager;

class RdTextLinkLayout extends Layout {
Point iExtent, tExtent; // the cached sizes

protected Point computeSize(Composite composite, int wHint, int hHint,
boolean changed) {
Control [] children = composite.getChildren();
if (changed || iExtent == null || tExtent == null) {
iExtent = children[0].computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
tExtent = children[1].computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
}

int width = iExtent.x + 5 + tExtent.x;
int height = Math.max(iExtent.y, tExtent.y);
return new Point(width + 2, height + 2);
}

protected void layout(Composite composite, boolean changed) {
Control [] children = composite.getChildren();
if (changed || iExtent == null || tExtent == null) {
iExtent = children[0].computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
tExtent = children[1].computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
}
children[0].setBounds(1, 1, iExtent.x, iExtent.y);
children[1].setBounds(iExtent.x + 5, 1, tExtent.x, tExtent.y);
}
}

public class RdTextLink extends Composite {
Label image;
Text text;
private Image defaultImage =
SWTResourceManager.getImage("gui/icons/www.gif");


public RdTextLink(Composite parent, int style) {
super(parent, style);
text = new Text(this, 0);
image = new Label(this, 0);
image.setImage(defaultImage);
image.addMouseListener(new MouseListener() {
public void mouseDown(MouseEvent e) {
try {
Runtime.getRuntime().exec("rundll32.exe
url.dll,FileProtocolHandler " + text.getText());
} catch (IOException e1) {
J6101.getStatusBar().setErrText("Unable to open website '" +
text.getText() + "'");
}
}
public void mouseDoubleClick(MouseEvent e) {}
public void mouseUp(MouseEvent e) {}
});

addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
RdTextLink.this.widgetDisposed(e);
}
});
setLayout(new RdTextLinkLayout());
}

void widgetDisposed(DisposeEvent e) {
}

public Image getImage() {
return image.getImage();
}

public void setImage(Image image) {
this.image.setImage(image);
layout(true);
}

public String getText() {
return text.getText();
}

public void setText(String text) {
this.text.setText(text);
layout(true);
}
}
=== end
Previous Topic:linux: browser - what part of mozilla is needed?
Next Topic:Rotate a figure around the center
Goto Forum:
  


Current Time: Fri Sep 20 22:16:19 GMT 2019

Powered by FUDForum. Page generated in 0.01353 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top