Pickerwheel question

Good day,

I just wanted to know if anyone can assist me with the following issue:

I’m testing a very simple single column picker wheel that consists of number string values like 1,2,3,4,5, e.t.c

I’ve successfully created the wheel and it works fine with no issues.

However I want this picker wheel to print to the console the number string that is focused on or selected by the user.

So far I’ve done a lot of research to see how to achieve this to no avail.

I’ve used the documentation at: https://docs.coronalabs.com/api/library/widget/newPickerWheel.html for the construction of the picker wheel.

I suspect there is something I’m missing out.

How do I get the picker wheel to print to the console the value that is highlighted or selected.

Many Thanks

In Solar2D’s widget.newPickerWheel, there isn’t a built-in “on change” event. You read the current selection with pickerWheel:getValues()—either when the user taps a “Done” button, or by polling while it’s being scrolled.

Here are two simple patterns:

local widget = require("widget")

local picker = widget.newPickerWheel{
    x = display.contentCenterX,
    y = display.contentCenterY,
    columns = {
        {
            -- numbers as strings
            labels = { "1","2","3","4","5" },
            startIndex = 3, -- optional
            align = "center",
            width = 150
        }
    },
    style = "spinner"  -- or "resizable"
}

local function onDoneTap()
    local values = picker:getValues()
    local current = values[1]      -- single column
    print("Selected:", current.value, " (index:", current.index .. ")")
end

local doneBtn = display.newText({
    text = "Done",
    x = display.contentCenterX,
    y = picker.y + 140
})
doneBtn:addEventListener("tap", onDoneTap)

local lastIndex = picker:getValues()[1].index

local function onEnterFrame()
    local v = picker:getValues()[1]
    if v.index ~= lastIndex then
        lastIndex = v.index
        print("Focused:", v.value) -- prints as you scroll to a new row
    end
end
Runtime:addEventListener("enterFrame", onEnterFrame)
1 Like

Hello !

And thanks so much for responding to my question.
I will try this solution later today and get back to you.

It seems having a done button is the thing that is missing.

Regards

Hello and Good day !

I wanted to let you know that your solution worked perfectly, and the focused items on the pickerwheel are now printing out to the console.

Thanks a lot for your help. Now I will extend the pickerwheel to see if it can change the textbox values.

Hello there !

Just wanted to give you an update.

So I’ve been playing around with the picker wheel, and now it also prints the value ‘on focus’ to the textbox beside it (I’m working on it working in tandem with a text box). Here’s how I did it using your code:

local function onEnterFrame()
    local v = pickerWheel:getValues()[1]
    if v.index ~= lastIndex then
        lastIndex = v.index
        textBox.text = v.value -- I added this line for the text box I created
        print("Focused:", v.value) -- prints as you scroll to a new row
    end
end
Runtime:addEventListener("enterFrame", onEnterFrame)

This is what it looks like:

The text in the text box change dynamically with the picker wheel.

What I was trying to do was have a scenario where the user selects an row from the picker wheel, and the textBox (which I set to non-editable) will display the corresponding value according to the picker wheel index.

Basically the picker wheel is languages, and the text displayed on the textbox beside it will change according to the current selected language.

i wouldn’t use a picker wheel for this

picker wheels are mostly for dates or related info inside the same table

it is better to use a scroll view … and it has all the events you need …

just paste this to main.lua and see:

local widget = require("widget")

local lbl1= display.newText({
        text ="Label",
        x =  display.contentCenterX,
        y =  display.contentCenterY-200,
        fontSize = 18
    })
 lbl1:setFillColor(1) -- highlight selected


local items = { "Option 1", "Option 2", "Option 3", "Option 4", "Option 5" }

-- Create scrollView
local scrollView = widget.newScrollView({
    x = display.contentCenterX,
    y = display.contentCenterY,
    width = 200,
    height = 120,
    hideBackground = false,
    horizontalScrollDisabled = true,
})

-- Variables to track selection
local selectedIndex = nil
local selectedLabel = nil

-- Tap handler for each label
local function onItemTap(event)
    local tappedLabel = event.target
    selectedIndex = tappedLabel.index

    -- Optional: visually mark selection
    if selectedLabel then
        selectedLabel:setFillColor(0) -- reset old selection to white
    end
    tappedLabel:setFillColor(0, 0.6, 1) -- highlight selected
    selectedLabel = tappedLabel
	lbl1.text = tappedLabel.text

    print("Tapped:", tappedLabel.text, "(index:", tappedLabel.index .. ")")
    return true
end

-- Insert items into scrollView
local itemHeight = 30
for i = 1, #items do
    local lbl = display.newText({
        text = items[i],
        x = scrollView.width * 0.5,
        y = (i - 0.5) * itemHeight,
        fontSize = 18
    })
    lbl.index = i
    lbl.text = items[i]
 lbl:setFillColor(0) -- highlight selected

    scrollView:insert(lbl)
    lbl:addEventListener("tap", onItemTap)
end

-- Done button
local function onDoneTap()
    if selectedIndex then
        print("Selected:", items[selectedIndex], "(index:", selectedIndex .. ")")
    else
        print("No item selected")
    end
end

local doneBtn = display.newText({
    text = "Done",
    x = display.contentCenterX,
    y = scrollView.y + 100,
    fontSize = 20
})
doneBtn:addEventListener("tap", onDoneTap)
1 Like

Hello !

And thank you so much for your feedback. I will be trying this out and getting back to you on how it works.

Thanks !

Good day !

And of course I’m back ! :grin:

Your solution worked like a charm. I pasted it into a blank new project and ran it, and yes, it just makes sense using this method as opposed to what I was trying to achieve.

This is just way more logical.

I’m using this for a language selection screen for an app I’ve already published years ago which was developed by AGK. I’m converting it to Solar2D because it seems S2D will allow me to achieve what I originally envisioned for the app.

I will now build upon this code you’ve sent and get back to you.

Once again, thanks a lot.

Ok !

So I decided add a very simple function to your code which will enable the textbox in the image above to be populated by some text depending on the option which is selected.

So I added:

if selectedIndex == 1 then

        textBox.text = "This is option 1"

end

And let’s just say that it does exactly what I was hoping to achieve in the first place.

In fact, this solution can be easily adapted to other aspects of the app I’m making (It’s actually a remake of an app I’d already published and developed years ago, but I’m remaking it in S2D).

As a newbie to S2D, this is very refreshing.

I’m now going to learn composer for the rest of my scenes :cold_face:

One again, many thanks !

you are most welcome

my advice for you is to ask Chat GPT about what you want to achieve in solar2d, by giving it small requests, it will give you precise latest updated method and code on how to do it, it barely makes mistakes …

you can actually ask for a full project, and it will give it to you…
this way you will make sure that you learn the best way to do anything, and always ask it to use classes in solar2d to do it the proper professional way

Good Luck

1 Like

:rofl: :joy: I’m a triceratops from the Late Cretaceous period that comes from the world of Pascal, BASIC, and Java. My brain always prefers to ignore the Chat GPT things because I feel like it’s cheating.

I love the idea of working it out gradually, bit by bit.

I feel Chat GPT will take the fun away from building.

But thanks for the recommendation though !

i truly understand what you mean, but Ai will only do what you ask it for, and it will give you the best cleanest shortest way to do it

I have been programming since 1997
and on solar2d since 2013

i have written and sold hundreds of solutions which are still running Today in many servers of my clients … but i was shocked of how wrong i was doing many things … although it is working perfectly with great performance and without bugs, but it is still was not the correct way to do it… since i started with Ai, i barely write code, but i know exactly how and what to ask for, and believe me it is not easy to ask it sometimes, but when you start using it, you realize that you can’t go back to walking the long stairs when you have an elevator, unless you are exercising…

the 2 code blocks i gave you are Ai generated, and it was generated based on your message, i didn’t do anything more, just copy paste your message and got back the code … it took me around 10 seconds to do this … if i had to do it on my own it would have taken much longer, with bugs that i had to test and fix

the second code block i asked it to change pickerwheel to scrollview … it had one small mistake which i fixed immediately, and the mistake was because i did not ask properly

Ai is taking over whether we like it or not, and i believe within a few years coding will be different for everyone. so i hope we won’t be out of business soon :slight_smile:

1 Like

100% agree with you.

Ai tools like ChatGPT definitely make life much easier, and in the case of producing the code that resolved this problem = amazing.

However, the truth about it is that it will eventually destroy the sacred art of critical thinking, and eventually the art of creativity.

This is my just my POV.

People will cease to be creative and think critically because ChatGPT can do it all. Eventually. It’s slowly but surely heading there.

People will not appreciate the sacred art of building and making mistakes which leads to self realization and a learning that Ai couldn’t possibly replicate.

IQ’s will suffer.

I even spoke to someone a few weeks ago that mirrored exactly what you said about Ai taking all the jobs of software engineers and programmers.

I believe this is the inevitable road that things are going down, and it comes at a great cost.

Which is why I think that now more than ever is a time to create a distinction between Ai created content and human generated content.

It’s the problem that is plaguing the games industry too: games were better, more fun, and more optimized back in the PS1, PS2, and PS3, XBOX, XB360 days than what they are now. This is because those games were created by human effort, human skill, and human creativity as opposed to Ai generated content.

This is why there is a huge demand for retro games. Especially retro 3D games.

I will never let Ai take over my job, skills, profession and passion.

It can try but it will fail very hard :rofl: