Login  Register

Re: addSlider question

Posted by Jan Eglinger on Dec 02, 2014; 10:35am
URL: http://imagej.273.s1.nabble.com/addSlider-question-tp5010711p5010717.html

Hi Xavier,

On 01.12.14 17:54, Xavier Colomé wrote:

> Hello, I'm testing with dialogs and I want to create three sliders that
> interact with each other. The sum of the three slider must total 100%. By
> example:
>
> X [----*-----] [5]
> Y [--*-------] [3]
> Z [-*--------] [2]
>
> By example, if I move one of te three slider to 100% then we can not move
> the other two slider above 0%
>
> By example, this is a not permited configuration:
>
> X [---*------] [4]
> Y [------*---] [7]
> Z [-*--------] [2]
>
> Is it possible?
>

You can use the ij.gui.DialogListener class [1] for this. Your plugin
should implement `DialogListener` and include a `dialogItemChanged`
method that set the slider values according to your constraints.

Below I paste the code of a python script that has three sliders (X,Y,Z)
whose values always sum up to 100. Just paste it into Fiji's script
editor and press "Run". It has limited functionality (the Z slider
cannot be adjusted on its own), but you might get the idea.

Hope that helps,
Jan

[1]: http://javadoc.imagej.net/ImageJ1/index.html?ij/gui/DialogListener.html


********* Python script to demonstrate slider listeners ***********

from ij import IJ
from ij.gui import DialogListener
from ij.gui import GenericDialog

class MyDL (DialogListener):
        def dialogItemChanged(self, dialog, event):
                x = dialog.getNextNumber()
                y = dialog.getNextNumber()
                z = dialog.getNextNumber()
                sliders = dialog.getSliders()
                numFields = dialog.getNumericFields()
                zSlider = sliders.get(2)
                zField = numFields.get(2)
                zSlider.setValue((int)(100-(x+y)))
                zField.setText(IJ.d2s(100-(x+y), 0))
                return 1

gd = GenericDialog("Slider Listener")
gd.addSlider("X", 0, 100, 50)
gd.addSlider("Y", 0, 100, 20)
gd.addSlider("Z", 0, 100, 30)
gd.addDialogListener(MyDL())
gd.showDialog()

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html