Extending SpinnerListModel

If you want functionality for looping the values of the JSpinner use this class:

package mseries.utils;

import java.util.List;

import javax.swing.SpinnerListModel;

import java.util.Arrays;

/**

*

Title: RespinnerListModel

*

Description: Extends SpinnerListModel with looping functionality.

*

* @author Victor Tsonkov

* @version 11.12.2006

*/

public class RespinnerListModel extends SpinnerListModel

{

/**

* Indicates if the control supports wrapping.

*/

private boolean isWrap = false;

/**

* Create a reference to the list because the base list is private.

*/

private List values = null;

/**

* The index of the current element.

*/

private int curIndex = 0;

/**

* The index of the previous element.

*/

private int prevIndex = -1;

/**

* Initializes a new instance of the RespinnerListModel class.

*/

public RespinnerListModel()

{

}

/**

* Initializes a new instance of the RespinnerListModel class.

*/

public RespinnerListModel(List<?> values)

{

super(values);

this.values = values;

}

/**

* Initializes a new instance of the RespinnerListModel class.

*/

public RespinnerListModel(Object[] values)

{

super(values);

this.values = Arrays.asList(values);

}

/**

* Sets the wrapping of values in the list.

* @param isWrap boolean

*/

public void setWrap(boolean isWrap)

{

this.isWrap = isWrap;

}

/**

* Gets the wrapping of values in the list.

* @param isWrap boolean

*/

public boolean isWrap()

{

return isWrap;

}

/**

* Returns the next legal value of the underlying sequence or

* <code>null</code> if value is already the last element.

*

* @return the next legal value of the underlying sequence or

*<code>null</code> if value is already the last element

* @see SpinnerModel#getNextValue

* @see #getPreviousValue

*/

public Object getNextValue()

{

Object result = null;

result = super.getNextValue();

if (isWrap)

{

if (result == null)

result = values.get(0);

}

return result;

}

/**

* Returns the previous element of the underlying sequence or

* <code>null</code> if value is already the first element.

*

* @return the previous element of the underlying sequence or

*<code>null</code> if value is already the first element

* @see SpinnerModel#getPreviousValue

* @see #getNextValue

*/

public Object getPreviousValue()

{

Object result = null;

result = super.getPreviousValue();

if (isWrap)

{

if (result == null)

result = values.get(values.size()-1);

}

return result;

}

/**

* Gets the first value of the list.

* @return Object

*/

public Object getFirstValue()

{

return values.get(0);

}

/**

* Gets the last value of the list.

* @return Object

*/

public Object getLastValue()

{

return values.get(values.size()-1);

}

/**

* Changes the list that defines this sequence and resets the index

* of the models <code>value</code> to zero. Note that <code>list</code>

* is not copied, the model just stores a reference to it.

*

* This method fires a <code>ChangeEvent</code> if <code>list</code> is

* not equal to the current list.

*

* @param list the sequence that this model represents

* @throws IllegalArgumentException if <code>list</code> is

*<code>null</code> or zero length

* @see #getList

*/

public synchronized void setList(List<?> values)

{

super.setList(values);

this.values = values;

this.prevIndex = 0;

}

/**

* Gets the index of the previous element.

* @return Object

*/

public Object getPreviouslySetValue()

{

Object result = null;

if (prevIndex > -1)

result = values.get(prevIndex);

return result;

}

/**

* Sets the value of the list.

* @param value Object

*/

public void setValue(Object value)

{

int index = values.indexOf(value);

if (index > -1 && index != this.curIndex)

{

prevIndex = this.curIndex;

this.curIndex = index;

}

super.setValue(value);

}

}

[4882 byte] By [Victor_Tsonkov] at [2007-11-26 12:16:38]
# 1
Yes, stunning, if you would only use code tags and a decent format.
PhHein at 2007-7-7 14:53:01 > top of Java-index,Archived Forums,Socket Programming...
# 2
Don't tell me You were really able to read that PhHein.
hellbinder at 2007-7-7 14:53:01 > top of Java-index,Archived Forums,Socket Programming...
# 3

Sorry dude. This is a copy/paste from my IDE. I' ll fix it.

package mseries.utils;

import java.util.List;

import javax.swing.SpinnerListModel;

import java.util.Arrays;

/**

*

Title: RespinnerListModel

*

Description: Extends SpinnerListModel with looping functionality.

*

* @author Victor Tsonkov

* @version 11.12.2006

*/

public class RespinnerListModel extends SpinnerListModel

{

/**

* Indicates if the control supports wrapping.

*/

private boolean isWrap = false;

/**

* Create a reference to the list because the base list is private.

*/

private List values = null;

/**

* The index of the current element.

*/

private int curIndex = 0;

/**

* The index of the previous element.

*/

private int prevIndex = -1;

/**

* Initializes a new instance of the RespinnerListModel class.

*/

public RespinnerListModel()

{

}

/**

* Initializes a new instance of the RespinnerListModel class.

*/

public RespinnerListModel(List<?> values)

{

super(values);

this.values = values;

}

/**

* Initializes a new instance of the RespinnerListModel class.

*/

public RespinnerListModel(Object[] values)

{

super(values);

this.values = Arrays.asList(values);

}

/**

* Sets the wrapping of values in the list.

* @param isWrap boolean

*/

public void setWrap(boolean isWrap)

{

this.isWrap = isWrap;

}

/**

* Gets the wrapping of values in the list.

* @param isWrap boolean

*/

public boolean isWrap()

{

return isWrap;

}

/**

* Returns the next legal value of the underlying sequence or

* <code>null</code> if value is already the last element.

*

* @return the next legal value of the underlying sequence or

*<code>null</code> if value is already the last element

* @see SpinnerModel#getNextValue

* @see #getPreviousValue

*/

public Object getNextValue()

{

Object result = null;

result = super.getNextValue();

if (isWrap)

{

if (result == null)

result = values.get(0);

}

return result;

}

/**

* Returns the previous element of the underlying sequence or

* <code>null</code> if value is already the first element.

*

* @return the previous element of the underlying sequence or

*<code>null</code> if value is already the first element

* @see SpinnerModel#getPreviousValue

* @see #getNextValue

*/

public Object getPreviousValue()

{

Object result = null;

result = super.getPreviousValue();

if (isWrap)

{

if (result == null)

result = values.get(values.size()-1);

}

return result;

}

/**

* Gets the first value of the list.

* @return Object

*/

public Object getFirstValue()

{

return values.get(0);

}

/**

* Gets the last value of the list.

* @return Object

*/

public Object getLastValue()

{

return values.get(values.size()-1);

}

/**

* Changes the list that defines this sequence and resets the index

* of the models <code>value</code> to zero. Note that <code>list</code>

* is not copied, the model just stores a reference to it.

*

* This method fires a <code>ChangeEvent</code> if <code>list</code> is

* not equal to the current list.

*

* @param list the sequence that this model represents

* @throws IllegalArgumentException if <code>list</code> is

*<code>null</code> or zero length

* @see #getList

*/

public synchronized void setList(List<?> values)

{

super.setList(values);

this.values = values;

this.prevIndex = 0;

}

/**

* Gets the index of the previous element.

* @return Object

*/

public Object getPreviouslySetValue()

{

Object result = null;

if (prevIndex > -1)

result = values.get(prevIndex);

return result;

}

/**

* Sets the value of the list.

* @param value Object

*/

public void setValue(Object value)

{

int index = values.indexOf(value);

if (index > -1 && index != this.curIndex)

{

prevIndex = this.curIndex;

this.curIndex = index;

}

super.setValue(value);

}

}

Victor_Tsonkov at 2007-7-7 14:53:01 > top of Java-index,Archived Forums,Socket Programming...
# 4
Pfft, I didn't even bother to try it. I usually ignore unformatted code, but this joker is editing since yesterday and and I wanted to put him out of his misery by replying :P
PhHein at 2007-7-7 14:53:01 > top of Java-index,Archived Forums,Socket Programming...
# 5
It's my first post on this site, *******. Sorry for the mistake not to format the code. Don't joke with my coding skill though.
Victor_Tsonkov at 2007-7-7 14:53:01 > top of Java-index,Archived Forums,Socket Programming...
# 6
Victor,if you get rid of the (unused) <?> pre Java5 users could use it too. I don't see why you need the autoboxing for undefined list element types here.
PhHein at 2007-7-7 14:53:01 > top of Java-index,Archived Forums,Socket Programming...
# 7
> It's my first post on this site, *******. Sorry for> the mistake not to format the code. Don't joke with> my coding skill though.Did I joke about your coding skill somewhere? And don't start calling me names .
PhHein at 2007-7-7 14:53:01 > top of Java-index,Archived Forums,Socket Programming...
# 8
I added to delegate all of the base class features. At first i wanted to make my own implementation on request but the JSpinner control throws exception when you use other than SpinnerListModel. If you have any suggestions please tell.Regards, Victor
Victor_Tsonkov at 2007-7-7 14:53:01 > top of Java-index,Archived Forums,Socket Programming...
# 9
Sorry for the misunderstanding PhHein. I guess the weather is very rainy here and I'm on too much coffee :)
Victor_Tsonkov at 2007-7-7 14:53:01 > top of Java-index,Archived Forums,Socket Programming...