Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2238 > unrolled thread

JSpinner in hex

Started by"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
First post2011-04-27 15:37 +0000
Last post2011-04-27 15:38 +0000
Articles 4 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  JSpinner in hex "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:37 +0000
    Re: JSpinner in hex "Thomas Fritsch" <thomas.fritsch@THRWHITE.remove-dii-this> - 2011-04-27 15:38 +0000
      Re: JSpinner in hex "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:38 +0000
      Re: JSpinner in hex "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:38 +0000

#2238 — JSpinner in hex

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:37 +0000
SubjectJSpinner in hex
Message-ID<8el5c31o7dge63q2n5kkdhcmr5c9vtpbrs@4ax.com>
  To: comp.lang.java.gui
I have written a HexFormat derived from java.text.Format that behaves
like a simplified DecimalFormat, however I don't see a simple way to
hook it up to a JSpinner to display values in hex. Is there a trick,
or must I write my own NumberEditor cloning a lot of code?

-- 
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

[toc] | [next] | [standalone]


#2246

From"Thomas Fritsch" <thomas.fritsch@THRWHITE.remove-dii-this>
Date2011-04-27 15:38 +0000
Message-ID<f9v371$m4t$00$1@news.t-online.com>
In reply to#2238
  To: comp.lang.java.gui
Roedy Green wrote:
> I have written a HexFormat derived from java.text.Format that behaves
> like a simplified DecimalFormat, however I don't see a simple way to
> hook it up to a JSpinner to display values in hex. Is there a trick,
> or must I write my own NumberEditor cloning a lot of code?
> 
Here is what I have. I've decided not to build it on class Format, but
instead on JFormattedTextField's formatter.
Editing and stepping up/down seems to work. But stepping below zero (to
FFFFFFFF) doesn't.


import java.text.ParseException;
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;

public class Test {
   public static void main(String[] args) {
     JFrame frame = new JFrame();
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     JSpinner spinner = new JSpinner(new SpinnerNumberModel());
     spinner.setEditor(new HexNumberEditor(spinner));
     frame.add(spinner);
     frame.pack();
     frame.setVisible(true);
   }
}

class HexNumberEditor extends JSpinner.NumberEditor {
   public HexNumberEditor(JSpinner spinner) {
     super(spinner);
     JFormattedTextField ftf = getTextField();
     ftf.setEditable(true);
     ftf.setFormatterFactory(new HexNumberFormatterFactory());
   }
}

class HexNumberFormatterFactory extends DefaultFormatterFactory {
   public HexNumberFormatterFactory() {
     super(new HexNumberFormatter());
   }
}

class HexNumberFormatter extends JFormattedTextField.AbstractFormatter {
   /** Converts a hex-coded String to an Integer */
   @Override
   public Object stringToValue(String text) throws ParseException {
     int n = Integer.parseInt(text, 16);
     return new Integer(n);
   }

   /** Converts a Number to a hex-coded String (8 digits long) */
   @Override
   public String valueToString(Object value) throws ParseException {
     Number number = (Number) value;
     String s = Integer.toHexString(number.intValue());
     while (s.length() < 8)
       s = "0" + s;
     return s;
   }
}

-- 
Thomas

---
 * 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

[toc] | [prev] | [next] | [standalone]


#2251

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:38 +0000
Message-ID<5m86c39fv120mon2hume5cnujuoc9vm943@4ax.com>
In reply to#2246
  To: comp.lang.java.gui
>Here is what I have. I've decided not to build it on class Format, but
>instead on JFormattedTextField's formatter.
Thanks.  I had made a second attempt based on extending
DefaultFormatter.  That factory bit is the magic I was missing.
-- 
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

[toc] | [prev] | [next] | [standalone]


#2253

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:38 +0000
Message-ID<dla6c3l1btmcclposcr532m8r9r664p0c8@4ax.com>
In reply to#2246
  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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web