Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to put a background image on Group.
How to put a background image on Group. [message #453931] Thu, 14 April 2005 06:56 Go to next message
Chris is currently offline ChrisFriend
Messages: 97
Registered: July 2009
Member
Hi,
I've created a small login modal window, but I'm just not able
to put an image on the background....

Below the class source; I'm using 'org.eclipse.swt.widgets.Group'
and 'org.eclipse.swt.layout.GridLayout' objects ... Maybe it could be a
problem for what I'm trying to do?? Any hints?

Thanks, Chris.


******************************************************
Simply set an image path and the launch the 'main'.



import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Foo {

public static void main(String[] args) {
new Foo();
}

boolean result = false;

boolean canExecutePwd = false;

boolean canExecuteUser = false;


public Foo() {

final Shell shell = new Shell();

final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL | SWT.ON_TOP);
dialog.setLayout(new FillLayout());

Font font = new Font(dialog.getDisplay(), "Arial", 10, SWT.BOLD);
Font font2 = new Font(dialog.getDisplay(), "Arial", 10,
SWT.NORMAL);

// Set image
// ***LOOK HERE*** <---------------
// You have to set your image path!
final Image imageIcon = new Image(shell.getDisplay(),
"icons/thetis.bmp");

// Set size and location
dialog.setLocation(334, 246);
dialog.setSize(300, 200);

// Add dispose Listener
dialog.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
if (!result)
shell.close();
}
});

Group group = new Group(dialog, SWT.CENTER);
group.setText("Login");
group.setFont(font2);

GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.makeColumnsEqualWidth = true;
group.setLayout(layout);

Label labelUser = new Label(group, SWT.NONE);
labelUser.setText("User ");
labelUser.setFont(font);

final Text textUser = new Text(group, SWT.SINGLE | SWT.BORDER);
textUser.setTextLimit(255);
textUser.setFont(font);

Label labelPwd = new Label(group, SWT.NONE);
labelPwd.setText("Password ");
labelPwd.setFont(font);

final Text textPwd = new Text(group, SWT.SINGLE | SWT.BORDER);
textPwd.setEchoChar('*');
textPwd.setTextLimit(255);
textPwd.setFont(font);

new Label(group, SWT.NONE);
new Label(group, SWT.NONE);

final Button cancel = new Button(group, SWT.PUSH);
cancel.setText(" Cancel ");
cancel.setFont(font);

final Button login = new Button(group, SWT.PUSH);
login.setText(" Login ");
login.setFont(font);
login.setEnabled(false);

// Create listener for login button
Listener listener = new Listener() {
public void handleEvent(Event event) {

if (textPwd.getText() == null
|| textPwd.getText().trim().length() == 0
|| textUser.getText() == null
|| textUser.getText().trim().length() == 0) {
result = false;
} else
result = true;

if (result) {
// Do auth. job here
}
}
};
login.addListener(SWT.Selection, listener);

// Add listener to cancel button
cancel.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
dialog.close();
}
});

// Add a listener to user inputs
final StringBuffer textHelpUser = new StringBuffer("");
textUser.addListener(SWT.Verify, new Listener() {
public void handleEvent(Event e) {
if (e.keyCode != 8 && e.keyCode != 127)
textHelpUser.append(e.text);
else
textHelpUser.deleteCharAt(textHelpUser.length() - 1);

if (textHelpUser != null && textHelpUser.length() > 0)
canExecuteUser = true;
else
canExecuteUser = false;

if (canExecuteUser && canExecutePwd)
login.setEnabled(true);
else
login.setEnabled(false);
}
});

final StringBuffer textHelpPwd = new StringBuffer("");
textPwd.addListener(SWT.Verify, new Listener() {
public void handleEvent(Event e) {
if (e.keyCode != 8 && e.keyCode != 127)
textHelpPwd.append(e.text);
else
textHelpPwd.deleteCharAt(textHelpPwd.length() - 1);

if (textHelpPwd != null && textHelpPwd.length() > 0)
canExecutePwd = true;
else
canExecutePwd = false;

if (canExecutePwd && canExecuteUser)
login.setEnabled(true);
else
login.setEnabled(false);
}
});
// end user input listeners

final Canvas canvas = new Canvas(group, SWT.NONE);

// ***LOOK HERE*** <---------------
// Basically, the problem is I'm trying to put
// an image on the background
// but I'm not able to do that..
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
if (dialog == null || dialog.isDisposed())
return;

Rectangle clientArea = dialog.getClientArea();
// event.gc.drawImage(imageIcon, -40, -40);
System.out.println("imageIcon.getBounds().width "
+ imageIcon.getBounds().width);
System.out.println("imageIcon.getBounds().height "
+ imageIcon.getBounds().height);
System.out.println("clientArea.width " + clientArea.width);
System.out.println("clientArea.height " +
clientArea.height);
event.gc.drawImage(imageIcon, 0, 0,
imageIcon.getBounds().width,
imageIcon.getBounds().height, 5, 5,
clientArea.width,
clientArea.height);
}
});

// Set default botton and open dialog window
dialog.setDefaultButton(login);
dialog.open();
dialog.forceFocus();
textUser.setFocus();

while (!dialog.isDisposed()) {
if (!shell.getDisplay().readAndDispatch())
shell.getDisplay().sleep();
}

// Clean resources
dialog.dispose();
imageIcon.dispose();
font.dispose();
font2.dispose();

}

}
Re: How to put a background image on Group. [message #453945 is a reply to message #453931] Thu, 14 April 2005 14:43 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
You wont' be able to do that. The operating system theme engine draws the
background for native controls.

"Chris" <velinux//@gmail//.com> wrote in message
news:189af9005ce27fb08bb0fdc8f03a1519$1@www.eclipse.org...
> Hi,
> I've created a small login modal window, but I'm just not able
> to put an image on the background....
>
> Below the class source; I'm using 'org.eclipse.swt.widgets.Group'
> and 'org.eclipse.swt.layout.GridLayout' objects ... Maybe it could be a
> problem for what I'm trying to do?? Any hints?
>
> Thanks, Chris.
>
>
> ******************************************************
> Simply set an image path and the launch the 'main'.
>
>
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.DisposeEvent;
> import org.eclipse.swt.events.DisposeListener;
> import org.eclipse.swt.events.PaintEvent;
> import org.eclipse.swt.events.PaintListener;
> import org.eclipse.swt.graphics.Font;
> import org.eclipse.swt.graphics.Image;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Canvas;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.Group;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.Listener;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Text;
>
> public class Foo {
>
> public static void main(String[] args) {
> new Foo();
> }
>
> boolean result = false;
>
> boolean canExecutePwd = false;
>
> boolean canExecuteUser = false;
>
>
> public Foo() {
>
> final Shell shell = new Shell();
>
> final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM
> | SWT.APPLICATION_MODAL | SWT.ON_TOP);
> dialog.setLayout(new FillLayout());
>
> Font font = new Font(dialog.getDisplay(), "Arial", 10, SWT.BOLD);
> Font font2 = new Font(dialog.getDisplay(), "Arial", 10,
> SWT.NORMAL);
>
> // Set image
> // ***LOOK HERE*** <---------------
> // You have to set your image path!
> final Image imageIcon = new Image(shell.getDisplay(),
> "icons/thetis.bmp");
>
> // Set size and location
> dialog.setLocation(334, 246);
> dialog.setSize(300, 200);
>
> // Add dispose Listener
> dialog.addDisposeListener(new DisposeListener() {
> public void widgetDisposed(DisposeEvent e) {
> if (!result)
> shell.close();
> }
> });
>
> Group group = new Group(dialog, SWT.CENTER);
> group.setText("Login");
> group.setFont(font2);
>
> GridLayout layout = new GridLayout();
> layout.numColumns = 2;
> layout.makeColumnsEqualWidth = true;
> group.setLayout(layout);
>
> Label labelUser = new Label(group, SWT.NONE);
> labelUser.setText("User ");
> labelUser.setFont(font);
>
> final Text textUser = new Text(group, SWT.SINGLE | SWT.BORDER);
> textUser.setTextLimit(255);
> textUser.setFont(font);
>
> Label labelPwd = new Label(group, SWT.NONE);
> labelPwd.setText("Password ");
> labelPwd.setFont(font);
>
> final Text textPwd = new Text(group, SWT.SINGLE | SWT.BORDER);
> textPwd.setEchoChar('*');
> textPwd.setTextLimit(255);
> textPwd.setFont(font);
>
> new Label(group, SWT.NONE);
> new Label(group, SWT.NONE);
>
> final Button cancel = new Button(group, SWT.PUSH);
> cancel.setText(" Cancel ");
> cancel.setFont(font);
>
> final Button login = new Button(group, SWT.PUSH);
> login.setText(" Login ");
> login.setFont(font);
> login.setEnabled(false);
>
> // Create listener for login button
> Listener listener = new Listener() {
> public void handleEvent(Event event) {
>
> if (textPwd.getText() == null
> || textPwd.getText().trim().length() == 0
> || textUser.getText() == null
> || textUser.getText().trim().length() == 0) {
> result = false;
> } else
> result = true;
>
> if (result) {
> // Do auth. job here
> }
> }
> };
> login.addListener(SWT.Selection, listener);
>
> // Add listener to cancel button
> cancel.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event event) {
> dialog.close();
> }
> });
>
> // Add a listener to user inputs
> final StringBuffer textHelpUser = new StringBuffer("");
> textUser.addListener(SWT.Verify, new Listener() {
> public void handleEvent(Event e) {
> if (e.keyCode != 8 && e.keyCode != 127)
> textHelpUser.append(e.text);
> else
> textHelpUser.deleteCharAt(textHelpUser.length() - 1);
>
> if (textHelpUser != null && textHelpUser.length() > 0)
> canExecuteUser = true;
> else
> canExecuteUser = false;
>
> if (canExecuteUser && canExecutePwd)
> login.setEnabled(true);
> else
> login.setEnabled(false);
> }
> });
>
> final StringBuffer textHelpPwd = new StringBuffer("");
> textPwd.addListener(SWT.Verify, new Listener() {
> public void handleEvent(Event e) {
> if (e.keyCode != 8 && e.keyCode != 127)
> textHelpPwd.append(e.text);
> else
> textHelpPwd.deleteCharAt(textHelpPwd.length() - 1);
>
> if (textHelpPwd != null && textHelpPwd.length() > 0)
> canExecutePwd = true;
> else
> canExecutePwd = false;
>
> if (canExecutePwd && canExecuteUser)
> login.setEnabled(true);
> else
> login.setEnabled(false);
> }
> });
> // end user input listeners
>
> final Canvas canvas = new Canvas(group, SWT.NONE);
>
> // ***LOOK HERE*** <---------------
> // Basically, the problem is I'm trying to put
> // an image on the background
> // but I'm not able to do that..
> canvas.addPaintListener(new PaintListener() {
> public void paintControl(PaintEvent event) {
> if (dialog == null || dialog.isDisposed())
> return;
>
> Rectangle clientArea = dialog.getClientArea();
> // event.gc.drawImage(imageIcon, -40, -40);
> System.out.println("imageIcon.getBounds().width "
> + imageIcon.getBounds().width);
> System.out.println("imageIcon.getBounds().height "
> + imageIcon.getBounds().height);
> System.out.println("clientArea.width " +
clientArea.width);
> System.out.println("clientArea.height " +
> clientArea.height);
> event.gc.drawImage(imageIcon, 0, 0,
> imageIcon.getBounds().width,
> imageIcon.getBounds().height, 5, 5,
> clientArea.width,
> clientArea.height);
> }
> });
>
> // Set default botton and open dialog window
> dialog.setDefaultButton(login);
> dialog.open();
> dialog.forceFocus();
> textUser.setFocus();
>
> while (!dialog.isDisposed()) {
> if (!shell.getDisplay().readAndDispatch())
> shell.getDisplay().sleep();
> }
>
> // Clean resources
> dialog.dispose();
> imageIcon.dispose();
> font.dispose();
> font2.dispose();
>
> }
>
> }
>
>
Previous Topic:In every platform, can SWT display MS's icon file?
Next Topic:TableViewer: radiobutton behaviour
Goto Forum:
  


Current Time: Tue Mar 02 14:58:06 GMT 2021

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

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

Back to the top