Re: JSpinner in hex
To: comp.lang.java.gui
On Wed, 15 Aug 2007 16:39:30 +0200, Thomas Fritsch
<i.dont.like.spam@invalid.com> wrote, quoted or indirectly quoted
someone who said :
Here is what I have. I've decided not to build it on class Format, but
instead on JFormattedTextField's formatter.
Here is my variant that uses fixed width with lz padding. It handles
numbers 0..fffffff;
package com.mindprod.jcolourchooser;
import javax.swing.JFormattedTextField;
import javax.swing.JSpinner;
/**
* @author Roedy Green, Canadian Mind Products
* @version 1.0, 2007-08-15 Created with IntelliJ IDEA. Based on
Thomas
* Fritsch's version posted in comp.lang.java.gui.
*/
public class HexNumberEditor extends JSpinner.NumberEditor {
// -------------------------- PUBLIC INSTANCE METHODS
--------------------------
/**
* constructor
*
* @param spinner JSpinner this editor is attached to.
* @param width how many chars wide the field is.
*/
public HexNumberEditor( JSpinner spinner, int width )
{
super( spinner );
JFormattedTextField ftf = getTextField();
ftf.setEditable( true );
ftf.setFormatterFactory( new HexNumberFormatterFactory( width
) );
}
}
---------------------------------------------------------------------------------------------
package com.mindprod.jcolourchooser;
import javax.swing.text.DefaultFormatterFactory;
/**
* @author Roedy Green, Canadian Mind Products
* @version 1.0, 2007-08-15 Created with IntelliJ IDEA. Based on
Thomas
* Fritsch's version posted in comp.lang.java.gui.
*/
public class HexNumberFormatterFactory extends DefaultFormatterFactory
{
// -------------------------- PUBLIC INSTANCE METHODS
--------------------------
/**
* constructor
*
* @param width how many chars wide the field is.
*/
public HexNumberFormatterFactory( int width )
{
super( new HexNumberFormatter( width ) );
}
}
----------------------------------------------------------------------
package com.mindprod.jcolourchooser;
import javax.swing.text.DefaultFormatter;
import java.text.ParseException;
/**
* hex formatter for use in JSpinner. handles 0..fffffff.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0, 2007-08-15 Created with IntelliJ IDEA.
*/
public class HexNumberFormatter extends DefaultFormatter {
// max chars with in hex digits.
private final int width;
// -------------------------- PUBLIC INSTANCE METHODS
--------------------------
/**
* constructor
*
* @param width how many chars wide is the field.
*/
public HexNumberFormatter( int width )
{
if ( width > 7 )
{
throw new IllegalArgumentException( "HexFormat width > 7"
);
}
this.width = width;
}
/**
* Converts the passed in String into an instance of
* <code>getValueClass</code> by way of the constructor that takes
a String
* argument. If <code>getValueClass</code> returns null, the Class
of the
* current value in the <code>JFormattedTextField</code> will be
used. If
* this is null, a String will be returned. If the constructor
thows an
* exception, a <code>ParseException</code> will be thrown. If
there is no
* single argument String constructor, <code>string</code> will be
* returned.
*
* @param string String to convert
*
* @return Object representation of text, namely Integer
*
* @throws java.text.ParseException if there is an error in the
conversion
*/
public Object stringToValue( String string ) throws ParseException
{
try
{
return Integer.valueOf( string, 16 );
}
catch ( NumberFormatException nfe )
{
throw new ParseException( string, 0 );
}
}
/**
* Converts the passed in Object into a String by way of the
* <code>toString</code> method.
*
* @param value Value to convert, Integer
*
* @return String representation of value, padded with left zeroes
to
* width.
*
* @throws ParseException if there is an error in the conversion
*/
public String valueToString( Object value ) throws ParseException
{
final int asInt = (Integer) value;
final String hex = Integer.toHexString( asInt );
// apply lead zeroes as needed.
final int lz = width - hex.length();
if ( lz <= 0 )
{
return hex;
}
else
{
return "00000000".substring( 0, lz ) + hex;
}
}
}
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24