JSlider Bean

Hi,

I’m trying to use a JSlider bean in a navigator control. I want to use it to allow users to scroll through a FoundSet collection of records.

So far I’ve managed to get the slider position and range to be updated by other events ( such as a button press, or the user doing a new search to get a different number of records.). I did this by creating a slider update method and then calling this from the relevant button methds etc.

But what I can’t see how to do is how to get the slider to change anything else. How do I read the position of the slider if the user moves it, and then do something with that information ( such as running a method).

With other objects ( eg. buttons ) I did this calling methods by the OnAction option in the button properties. I can’t see anything similar in the JSlider.

I’m quite stuck on this now, so any help really appreciated,

I’m using Servoy 3.0 beta.

Thanks,

Nick

Callback from beans is indeed a problem. I think your problem cannot be healed. As far as I know there is no way to get the bean to for example call a method. The bean doesn’t know that it is running inside Servoy and therefore has no feature to do something Servoy specific. The only thing that could be done is alter the bean itself so that it becomes a “Servoy JSLider bean”…

Beans can call servoy methods.
You just need to write the code to do it ;)

Are you going to open-source this module or make it proprietary ?

cybersack:
Beans can call servoy methods.
You just need to write the code to do it ;)

Are you going to open-source this module or make it proprietary ?

That’s what I meant:

alter the bean itself so that it becomes a “Servoy JSLider bean”

Technically what you need to do is register a listener on the bean. When something is done with the bean, the bean checks if there are listeners for the action that is being performed. If such a listener exists, the bean calls this listener.

Since there are many different types of listeners its not really possible (as far as I’m aware) to make something generic.

But what you can do is write a (fairly simple) plugin with functions that return a listener object that you can then add to the bean. As input, the function in the plugin would take the Servoy method you want to have called by the bean.

Paul

Ps: somewhere on this forum, I’ve explained this situation more extensivly.

Hi,

I’ve written my own JavaBean wrapper to the JSlider and have got Servoy to talk to it and adjust it, eg. when a button is pressed the slider position updates.

I’ve gone through the scheduler example as best I can and come up with the code below…It compiles and runs inside Servoy, but it just doesn’t run the method…

I also get the following error when I move the slider bar:
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at ServoyJSlider.stateChanged(ServoyJSlider.java:152)
at javax.swing.JSlider.fireStateChanged(JSlider.java:338)
at javax.swing.JSlider$ModelListener.stateChanged(JSlider.java:266)
at javax.swing.DefaultBoundedRangeModel.fireStateChanged(DefaultBoundedRangeModel.java:348)
at javax.swing.DefaultBoundedRangeModel.setRangeProperties(DefaultBoundedRangeModel.java:285)
at javax.swing.DefaultBoundedRangeModel.setValueIsAdjusting(DefaultBoundedRangeModel.java:214)
at javax.swing.JSlider.setValueIsAdjusting(JSlider.java:510)
at javax.swing.plaf.basic.BasicSliderUI$TrackListener.mouseReleased(BasicSliderUI.java:1361)
at javax.swing.plaf.synth.SynthSliderUI$SynthTrackListener.mouseReleased(SynthSliderUI.java:670)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Please can anyone see why its not working?

Thanks,

Nick

import java.awt.;
import java.awt.event.
;
import javax.swing.;
import javax.swing.event.
;
import java.io.Serializable;
import java.beans.PropertyChangeEvent;
import java.util.Properties;
import com.servoy.j2db.scripting.IScriptObject;
import com.servoy.j2db.plugins.IClientPlugin;
import com.servoy.j2db.plugins.IClientPluginAccess; // use javac -classpath = …/location of j2db.jar/
import com.servoy.j2db.plugins.PluginException;
import com.servoy.j2db.preference.PreferencePanel;

public class ServoyJSlider extends JPanel implements
IClientPlugin,
ChangeListener,
Serializable {
protected int Minimum = 1;
protected int Maximum = 20;
protected int Value = 10;

JSlider Slider;
JLabel sliderLabel;

private IClientPluginAccess access;

public IScriptObject getScriptObject()
{
return null;
}

public void initialize(IClientPluginAccess access) throws PluginException
{
this.access = access;
}

public ServoyJSlider()
{

setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

//Create the label.
sliderLabel = new JLabel(“Unmoved”, JLabel.CENTER);
sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

//Create the slider.
// JSlider Slider = new JSlider(JSlider.HORIZONTAL,
// Minimum, Maximum, 1);

Slider = new JSlider(JSlider.HORIZONTAL, Minimum, Maximum, Value);

Slider.addChangeListener(this);

// Slider.setMajorTickSpacing(10);
// Slider.setMinorTickSpacing(1);
// Slider.setPaintTicks(true);
// Slider.setPaintLabels(true);
Slider.setBorder( BorderFactory.createEmptyBorder(0,0,10,0) );

// add stuff to the pannel
add(Slider);
add(sliderLabel);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

}

// Minimum Slider range
public void setMinimum(int newValue)
{
Minimum = newValue;
Slider.setMinimum(Minimum);
}

public int getMinimum()
{
return Minimum;
}

// Maximum slider range
public void setMaximum(int newValue)
{
Maximum = newValue;
Slider.setMaximum(Maximum);
}

public int getMaximum()
{
return Maximum;
}

// value - this is the slider position
public void setValue(int newValue)
{
Value = newValue;
Slider.setValue(Value);
}

public int getValue()
{
Value = Slider.getValue();
return Value;
}

/* listen for state change */
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {

// Slider has been moved
if ( Slider.getValue() != Value ) {
Value = Slider.getValue();
sliderLabel.setText(“Slider used, v=”+Value);
access.executeMethod(“stock_NAV_browse”, “update_all”, new Object[0]);
}

// Something else moved the slider
else {
sliderLabel.setText(“you pressed a button??”);
}

}

}

// orientation

// MajorTickSpacing

//MinorTickSpacing

// Override a load of methods we don’t want…
public Properties getProperties()
{
return null;
}

public void load() throws PluginException
{
}

public void unload() throws PluginException
{
}

public Icon getImage()
{
return null;
}

public PreferencePanel getPreferencePanels()
{
return null;
}

public void propertyChange(PropertyChangeEvent evt)

{
}

}

What’s on line 152. Because it there that you reference to ‘something’ that has a null value/is not initialized…

xception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at ServoyJSlider.stateChanged(ServoyJSlider.java:152)

line 152 was:

access.executeMethod("stock_NAV_browse", "update_all", new Object[0]);

But I thought I had initialised this earlier? - Apologies if I’ve done something really obvious, am not really a Java programmer…

Thanks,

Nick

What are you trying to archieve with this ```
new Object[0]


That looks like JavaScript, not Java. Do this ```
new Object[] {new Integer(0)}

Sorry, have now tried that, but still get the same error… Am I basically doing the right approach in the rest of the code?

Thanks,

Nick

Hi Nick, I don’t know.

You really have to debug this yourself.

Java gives pretty good feedback on what goes wrong.

Cheers,