Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to set a button to be defualt selected
How to set a button to be defualt selected [message #438610] Mon, 28 June 2004 17:54 Go to next message
Eclipse UserFriend
Originally posted by: am560571.wcupa.edu

I created a popup form to change the title of a lable. I want the lable
to start with the text highlighted so the user can type and if they press
enter it will defualt to the ok button.

He is how I tried to set it up

protected Control createContents(final Composite parent)
{
final SashForm ChangeTitle =
new SashForm(
parent,
SWT.APPLICATION_MODAL | SWT.BORDER | SWT.VERTICAL);
Label lbl = new Label(ChangeTitle, SWT.BORDER);
lbl.setText("Enter New Title");
Font f = new Font(getShell().getDisplay(), "Courier", 14, SWT.BOLD);
lbl.setFont(f);
lbl.setAlignment(SWT.CENTER);
lbl.setSize(300, 100);
final Text text = new Text(ChangeTitle, SWT.CENTER);
text.setText(m.getTitle());
SashForm sash = new SashForm(ChangeTitle, SWT.HORIZONTAL);
text.setSize(80, 20);
Button butOk = new Button(sash, SWT.DefaultSelection);
butOk.setText("OK");
butOk.setAlignment(SWT.CENTER);
butOk.setFocus();
Button butCancel = new Button(sash, SWT.PUSH);
butCancel.setText("Cancel");
butCancel.setAlignment(SWT.CENTER);
butOk.setFont(f);
butCancel.setFont(f);
parent.setLocation(0, 0);
parent.setSize(300, 100);
text.selectAll();
butOk.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event event)
{
m.setTitle(text.getText());
parent.dispose();
}
});
butCancel.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event event)
{
parent.dispose();
}
});
return ChangeTitle;
}
}

what I am doing wrong?
Re: How to set a button to be defualt selected [message #438614 is a reply to message #438610] Mon, 28 June 2004 19:09 Go to previous messageGo to next message
Daniel Spiewak is currently offline Daniel SpiewakFriend
Messages: 263
Registered: July 2009
Senior Member
SWT.DefaultSelection is an event constant (the hint here is that it
contains lowercase characters). Use SWT.PUSH and then use
parent.getShell().setDefaultButton(butOK) to set it as the default
Button. You can also use Widget#setFocus() give a button focus, though
it won't automatically become the default. However, this is a good idea
on platforms like Windows which make the default button pass with the
button which has focus.

Daniel

whatsgoingon wrote:

>I created a popup form to change the title of a lable. I want the lable
>to start with the text highlighted so the user can type and if they press
>enter it will defualt to the ok button.
>
>He is how I tried to set it up
>
> protected Control createContents(final Composite parent)
> {
> final SashForm ChangeTitle =
> new SashForm(
> parent,
> SWT.APPLICATION_MODAL | SWT.BORDER | SWT.VERTICAL);
> Label lbl = new Label(ChangeTitle, SWT.BORDER);
> lbl.setText("Enter New Title");
> Font f = new Font(getShell().getDisplay(), "Courier", 14, SWT.BOLD);
> lbl.setFont(f);
> lbl.setAlignment(SWT.CENTER);
> lbl.setSize(300, 100);
> final Text text = new Text(ChangeTitle, SWT.CENTER);
> text.setText(m.getTitle());
> SashForm sash = new SashForm(ChangeTitle, SWT.HORIZONTAL);
> text.setSize(80, 20);
> Button butOk = new Button(sash, SWT.DefaultSelection);
> butOk.setText("OK");
> butOk.setAlignment(SWT.CENTER);
> butOk.setFocus();
> Button butCancel = new Button(sash, SWT.PUSH);
> butCancel.setText("Cancel");
> butCancel.setAlignment(SWT.CENTER);
> butOk.setFont(f);
> butCancel.setFont(f);
> parent.setLocation(0, 0);
> parent.setSize(300, 100);
> text.selectAll();
> butOk.addListener(SWT.Selection, new Listener()
> {
> public void handleEvent(Event event)
> {
> m.setTitle(text.getText());
> parent.dispose();
> }
> });
> butCancel.addListener(SWT.Selection, new Listener()
> {
> public void handleEvent(Event event)
> {
> parent.dispose();
> }
> });
> return ChangeTitle;
> }
>}
>
>what I am doing wrong?
>
>
>
>
Re: How to set a button to be defualt selected [message #438658 is a reply to message #438614] Tue, 29 June 2004 12:23 Go to previous message
Eclipse UserFriend
Originally posted by: am560571.wcupa.edu

Thank you that worked wonderfully I would have died before I figured that
out :)

later,
Aaron

Daniel Spiewak wrote:

> SWT.DefaultSelection is an event constant (the hint here is that it
> contains lowercase characters). Use SWT.PUSH and then use
> parent.getShell().setDefaultButton(butOK) to set it as the default
> Button. You can also use Widget#setFocus() give a button focus, though
> it won't automatically become the default. However, this is a good idea
> on platforms like Windows which make the default button pass with the
> button which has focus.

> Daniel

> whatsgoingon wrote:

> >I created a popup form to change the title of a lable. I want the lable
> >to start with the text highlighted so the user can type and if they press
> >enter it will defualt to the ok button.
> >
> >He is how I tried to set it up
> >
> > protected Control createContents(final Composite parent)
> > {
> > final SashForm ChangeTitle =
> > new SashForm(
> > parent,
> > SWT.APPLICATION_MODAL | SWT.BORDER | SWT.VERTICAL);
> > Label lbl = new Label(ChangeTitle, SWT.BORDER);
> > lbl.setText("Enter New Title");
> > Font f = new Font(getShell().getDisplay(), "Courier", 14, SWT.BOLD);
> > lbl.setFont(f);
> > lbl.setAlignment(SWT.CENTER);
> > lbl.setSize(300, 100);
> > final Text text = new Text(ChangeTitle, SWT.CENTER);
> > text.setText(m.getTitle());
> > SashForm sash = new SashForm(ChangeTitle, SWT.HORIZONTAL);
> > text.setSize(80, 20);
> > Button butOk = new Button(sash, SWT.DefaultSelection);
> > butOk.setText("OK");
> > butOk.setAlignment(SWT.CENTER);
> > butOk.setFocus();
> > Button butCancel = new Button(sash, SWT.PUSH);
> > butCancel.setText("Cancel");
> > butCancel.setAlignment(SWT.CENTER);
> > butOk.setFont(f);
> > butCancel.setFont(f);
> > parent.setLocation(0, 0);
> > parent.setSize(300, 100);
> > text.selectAll();
> > butOk.addListener(SWT.Selection, new Listener()
> > {
> > public void handleEvent(Event event)
> > {
> > m.setTitle(text.getText());
> > parent.dispose();
> > }
> > });
> > butCancel.addListener(SWT.Selection, new Listener()
> > {
> > public void handleEvent(Event event)
> > {
> > parent.dispose();
> > }
> > });
> > return ChangeTitle;
> > }
> >}
> >
> >what I am doing wrong?
> >
> >
> >
> >
Previous Topic:Undo method with Text composite ?
Next Topic:Please help on the AWT_SWT ....
Goto Forum:
  


Current Time: Thu Apr 25 17:01:18 GMT 2024

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

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

Back to the top