Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Select all text inside a Spinner(Select all text inside a Spinner on keyPressed)
icon5.gif  Select all text inside a Spinner [message #642696] Thu, 02 December 2010 12:14 Go to next message
Albert Pikus is currently offline Albert PikusFriend
Messages: 70
Registered: October 2009
Member
Hi,

I'm looking for a way to select all text (digits) inside a Spinner every time when a digit is entered using keyboard. Spinner widget doesn't have a selectAll() method as e.g. Text widget.

Is there any way to achieve this?


Best regards,

Albert

[Updated on: Thu, 02 December 2010 12:41]

Report message to a moderator

Re: Select all text inside Spinner [message #642703 is a reply to message #642696] Thu, 02 December 2010 12:50 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 12/2/2010 13:14, Albert wrote:
> Hi,
>
> I'm looking for a way to select all text (digits) inside a Spinner every
> time when a digit is entered using keyboard. Spinner widget doesn't have
> a selectAll() method as e.g. Text widget.
>
> Is there any way to achieve this?

Have you tried to call the setSelection(int) method? AFAIK this will
also select the complete text.

If this does not help you may need to use non-portable means, e.g. on
Windows as described in

http://dev.eclipse.org/newslists/news.eclipse.platform.swt/m sg40030.html

HTH & Greetings from Bremen,

Daniel Krügler
Re: Select all text inside Spinner [message #642715 is a reply to message #642703] Thu, 02 December 2010 13:40 Go to previous messageGo to next message
Albert Pikus is currently offline Albert PikusFriend
Messages: 70
Registered: October 2009
Member
Hi, Daniel,

thank you for your quick answer.

I tried with setting selection to spinner, but only last typed digit stays, overwriting any previous value. Besides, digit is not selected this way. Inside keyPressed(KeyEvent event) I have a line Spinner.setSelection(Spinner.getSelection()) - is this a proper way of doing this?

I also tried with adding focus listener to a Spinner as you suggested, but this doesn't have any effect either.


Best regards,

Albert
Re: Select all text inside Spinner [message #642721 is a reply to message #642715] Thu, 02 December 2010 13:53 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 12/2/2010 14:40, Albert wrote:
> I also tried with adding focus listener to a Spinner as you suggested,
> but this doesn't have any effect either.

The focus listener is a red herring (it was probably necessary for the
OP of the quoted message). The relevant experiment is to call the OS API:

int hwnd = OS.GetWindow (spinner.handle, OS.GW_CHILD);
int hwndText = OS.GetWindow (hwnd, OS.GW_HWNDNEXT);
OS.SendMessage (hwndText, OS.EM_SETSEL, 0, -1);

to select the spinner - try this!

HTH & Greetings from Bremen,

Daniel Krügler
Re: Select all text inside Spinner [message #642725 is a reply to message #642721] Thu, 02 December 2010 14:10 Go to previous messageGo to next message
Albert Pikus is currently offline Albert PikusFriend
Messages: 70
Registered: October 2009
Member
Hi,

I tried this:

spinner.addKeyListener(new KeyListener()
{
    @Override
    public void keyPressed(KeyEvent event)
    {   
        long hwnd = OS.GetWindow (spinner.handle, OS.GW_CHILD);
        long hwndText = OS.GetWindow (hwnd, OS.GW_HWNDNEXT);
        OS.SendMessage (hwndText, OS.EM_SETSEL, 0, -1);
     }
     @Override
     public void keyReleased(KeyEvent event)
     {   
                
     }
});


but it has the exactly same effect (only digit that was last entered stays, overwriting previously entered digit) as the following:

spinner.addKeyListener(new KeyListener()
{
    @Override
    public void keyPressed(KeyEvent event)
    {   
        spinner.setSelection(spinner.getSelection());
    }
    @Override
    public void keyReleased(KeyEvent event)
    {   
                
    }
});



Best regards,

Albert
Re: Select all text inside Spinner [message #642741 is a reply to message #642725] Thu, 02 December 2010 15:17 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
There isn't API on Spinner that does what you want. You can get the effect
you want on Windows and Cocoa by adding the listener below.

spinner.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
// hack to make the spinner's text become selected
spinner.setSelection(spinner.getSelection());
}
});

I don't think there's another way to do this, short of creating your own
emulated Spinner (a Text + 2 buttons). If you want to consider this then
there's a sample implementation available at
http://www.eclipse.org/articles/Article-Writing%20Your%20Own %20Widget/Writing%20Your%20Own%20Widget_files/AppendixE.htm
as part of Eclipse Corner article
http://www.eclipse.org/articles/Article-Writing%20Your%20Own %20Widget/Writing%20Your%20Own%20Widget.htm .

Grant


"Albert" <albert.pikus@gmail.com> wrote in message
news:id8946$bfs$1@news.eclipse.org...
> Hi,
>
> I tried this:
>
>
> spinner.addKeyListener(new KeyListener()
> {
> @Override
> public void keyPressed(KeyEvent event)
> { long hwnd = OS.GetWindow (spinner.handle, OS.GW_CHILD);
> long hwndText = OS.GetWindow (hwnd, OS.GW_HWNDNEXT);
> OS.SendMessage (hwndText, OS.EM_SETSEL, 0, -1);
> }
> @Override
> public void keyReleased(KeyEvent event)
> { }
> });
>
>
> but it has the exactly same effect (only digit that was last entered
> stays, overwriting previously entered digit) as the following:
>
>
> spinner.addKeyListener(new KeyListener()
> {
> @Override
> public void keyPressed(KeyEvent event)
> { spinner.setSelection(spinner.getSelection());
> }
> @Override
> public void keyReleased(KeyEvent event)
> { }
> });
>
>
>
> Best regards,
>
> Albert
>
Re: Select all text inside Spinner [message #642751 is a reply to message #642741] Thu, 02 December 2010 15:34 Go to previous messageGo to next message
Albert Pikus is currently offline Albert PikusFriend
Messages: 70
Registered: October 2009
Member
Hi, Grant,

I tried your suggestion too and this really selects text inside a Spinner, but as in previous cases, all digits are overwritten with the last one.

It's really unfortunate that this API is not available, it would be great if Spinner had a similar behavior as DateTime widget (created with SWT.DATE | SWT.LONG); only digits are allowed to be entered and selected element (day, month or year) is reselected once it was modified using the keyboard. This way, selected text is always overwritten when a digit is input and there is no need to edit it using backspace or delete keys, nor to manually select digits in order to overwrite them.

It really seems that I will have to fake a Spinner somehow.


Thank you for your answer, best regards,

Albert
Re: Select all text inside Spinner [message #642770 is a reply to message #642751] Thu, 02 December 2010 15:55 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 12/2/2010 16:34, Albert wrote:
> It's really unfortunate that this API is not available, it would be
> great if Spinner had a similar behavior as DateTime widget (created with
> SWT.DATE | SWT.LONG); only digits are allowed to be entered and selected
> element (day, month or year) is reselected once it was modified using
> the keyboard. This way, selected text is always overwritten when a digit
> is input and there is no need to edit it using backspace or delete keys,
> nor to manually select digits in order to overwrite them.
>
> It really seems that I will have to fake a Spinner somehow.

Why aren't you opening a feature request for it?

- Daniel Krügler
Re: Select all text inside Spinner [message #642881 is a reply to message #642770] Fri, 03 December 2010 08:04 Go to previous messageGo to next message
Albert Pikus is currently offline Albert PikusFriend
Messages: 70
Registered: October 2009
Member
Hi,

I opened a feature request this morning, please see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=331742


Best regards,

Albert
Re: Select all text inside a Spinner [message #642925 is a reply to message #642696] Fri, 03 December 2010 11:35 Go to previous messageGo to next message
Albert Pikus is currently offline Albert PikusFriend
Messages: 70
Registered: October 2009
Member
Hi,

perhaps this can be achieved after all. I managed to produce a snippet that demonstrates desired behavior. Perhaps my description wasn't clear enough: with every new character (digit) entered, all text should be selected, but when typing in subsequent characters, existing text inside a Spinner should not be overwritten, except if some limit is exceeded; suppose I want to set a limit for a number that is entered, e.g. number cannot be less than 1 or greater than 20. In this case behavior should be like this, for example:

* "1" is entered and selected,
* after that "2" is entered, so we have "12" selected inside a Spinner (the
first "1" is NOT overwritten),
* but once I add, say "3", this would result in "123", which exceeds the 20
limit - so "12" is overwritten with "3" and selected.

Basically, this is the same behavior as DateTime widget features. Here is the snippet:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

public class SpinnerTest
{  
    SpinnerTest()
    {
        Display display = new Display();
        Shell shell = new Shell(display);    
        
        shell.setLayout(new FormLayout());
        
        final Spinner spinner = new Spinner (shell, SWT.BORDER);
        
        FormData formData = new FormData();
        formData.top = new FormAttachment(0, 10);            
        formData.left = new FormAttachment(10, 0);
        formData.right = new FormAttachment(10, 70);            
        spinner.setLayoutData(formData);
        
        shell.setSize(200, 100);
        
        spinner.setMinimum(1);
        spinner.setMaximum(20);
        spinner.setTextLimit(3);
        
        spinner.addKeyListener(new KeyListener()
        {   
            String oldSpinnerText;
            String newSpinnerText;
            
            @Override
            public void keyPressed(KeyEvent event)
            {   
                try
                {
                    oldSpinnerText = String.valueOf(spinner.getSelection());                    
                    String newText = String.valueOf(event.character);                    
                    newSpinnerText = oldSpinnerText + newText;
                    
                    if (Integer.valueOf(newSpinnerText) >= spinner.getMaximum())
                    {
                        oldSpinnerText = String.valueOf(spinner.getSelection());
                        spinner.setSelection(Integer.valueOf(newText));                        
                        newSpinnerText = newText;
                    }
                }
                catch (NumberFormatException e)
                {
                    e.printStackTrace();
                }
                
            }
            @Override
            public void keyReleased(KeyEvent event)
            {   
                spinner.setSelection(Integer.valueOf(newSpinnerText));
            }
        });
        
        // Never deselect digits in spinner when clicking inside digits field
        spinner.addMouseListener(new MouseListener()
        {   
            @Override
            public void mouseUp(MouseEvent arg0)
            {   
                spinner.setSelection(spinner.getSelection());
            }
            
            @Override
            public void mouseDown(MouseEvent arg0)
            {
                spinner.setSelection(spinner.getSelection());                
            }
            
            @Override
            public void mouseDoubleClick(MouseEvent arg0)
            {   
                
            }
        });
        
        shell.open();
       
        while(!shell.isDisposed())
        {
            if(!display.readAndDispatch())
            {
                display.sleep();
            }   
       }
       display.dispose();
    }
    
    public static void main(String[] argv)
    {
      new SpinnerTest();
    }
    
}



Best regards,

Albert

[Updated on: Fri, 03 December 2010 11:36]

Report message to a moderator

Re: Select all text inside a Spinner [message #642952 is a reply to message #642696] Fri, 03 December 2010 13:41 Go to previous message
Albert Pikus is currently offline Albert PikusFriend
Messages: 70
Registered: October 2009
Member
The above snippet works on Windows and Mac but not on GTK - digits are not selected.

Previous Topic:Increasing the size of a drop down of a combo
Next Topic:Can I make a Wysiwyg editor based on printing result with SWT?
Goto Forum:
  


Current Time: Fri Apr 10 10:51:38 GMT 2020

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

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

Back to the top