2012-10-29 18 views
2

JSpinner中顯示一系列帶符號的HexBinary數字的最佳方式是什麼?JSpinner:顯示一系列帶符號的HexBinary值

例如從0x80000x7ffe

我曾嘗試以下解決方案,沒有多少運氣:

  1. 使用JSpinnerNumberModel與默認格式由Int至hexBinary類型轉換[無法顯示範圍的負部分]
  2. 使用JSpinnerListModel並傳遞一個構建的HexBinary值列表(落在該範圍內)(使用不必要代碼進行設計的解決方案無法完美工作)。

是否有更好的通用解決方案?

+2

[一些提示爲十六進制的JSpinner的可能重複?我的方法是正確的](http://stackoverflow.com/questions/9758469/some-hints-for-hex-jspinner-i-s-the-approach-correct) – trashgod

+0

我嘗試了上述方法,在試驗解決方案沒有。 1.上面的鏈接只能解決十六進制格式問題,不能顯示負值。我面臨着一個不同的問題,其中包括以負HexBinary表示啓動JSpinner,並向上滾動直到達到上限。 – schinoy

+0

我設法最終通過使用JSpinnerListModel來完成任務,並將其傳遞給由實用程序類構造的範圍內的六進制字符串值列表。當然,我不喜歡這種解決方案,並認爲可能有辦法做到這一點,而無需構建整個範圍的實際列表。 – schinoy

回答

4

我認爲可能有辦法做到這一點,而無需構建整個範圍的實際列表。

一種方法是擴展AbstractSpinnerModel以創建LongNumberModel,如下所示。另見相關的example

Hex Spinner Test image

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.HeadlessException; 
import java.text.ParseException; 
import javax.swing.AbstractSpinnerModel; 
import javax.swing.JFormattedTextField; 
import javax.swing.JFormattedTextField.AbstractFormatter; 
import javax.swing.JFrame; 
import javax.swing.JSpinner; 
import javax.swing.text.DefaultFormatter; 
import javax.swing.text.DefaultFormatterFactory; 

/** 
* @see https://stackoverflow.com/a/13121724/230513 
* @see https://stackoverflow.com/a/9758714/230513 
*/ 
public class HexSpinnerTest { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       display(); 
      } 
     }); 
    } 

    private static void display() throws HeadlessException { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JSpinner sp = new JSpinner(new LongNumberModel(0x8000L, 0x8000L, 0xFFFFL, 1L)); 
     JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) sp.getEditor(); 
     JFormattedTextField tf = editor.getTextField(); 
     tf.setFormatterFactory(new MyFormatterFactory()); 
     f.getContentPane().add(sp, BorderLayout.NORTH); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private static class LongNumberModel extends AbstractSpinnerModel { 

     private Long value, stepSize; 
     private Comparable<Long> minimum, maximum; 

     public LongNumberModel(Long value, Long minimum, Long maximum, Long stepSize) { 
      this.value = value; 
      this.minimum = minimum; 
      this.maximum = maximum; 
      this.stepSize = stepSize; 
     } 

     @Override 
     public Object getValue() { 
      return value; 
     } 

     @Override 
     public void setValue(Object value) { 
      this.value = (Long) value; 
      fireStateChanged(); 
     } 

     @Override 
     public Object getNextValue() { 
      long v = value.longValue() + stepSize.longValue(); 
      return bounded(v); 
     } 

     @Override 
     public Object getPreviousValue() { 
      long v = value.longValue() - stepSize.longValue(); 
      return bounded(v); 
     } 

     private Object bounded(long v) { 
      if ((maximum != null) && (maximum.compareTo(v) < 0)) { 
       return null; 
      } 
      if ((minimum != null) && (minimum.compareTo(v) > 0)) { 
       return null; 
      } 
      return Long.valueOf(v); 
     } 
    } 

    private static class MyFormatterFactory extends DefaultFormatterFactory { 

     @Override 
     public AbstractFormatter getDefaultFormatter() { 
      return new HexFormatter(); 
     } 
    } 

    private static class HexFormatter extends DefaultFormatter { 

     @Override 
     public Object stringToValue(String text) throws ParseException { 
      try { 
       return Long.valueOf(text, 16); 
      } catch (NumberFormatException nfe) { 
       throw new ParseException(text, 0); 
      } 
     } 

     @Override 
     public String valueToString(Object value) throws ParseException { 
      return Long.toHexString(
       ((Long) value).intValue()).toUpperCase(); 
     } 
    } 
} 
+0

不過,您不需要子類DefaultFormatterFactory。只需使用文本字段的formatterFactory的setDefaultFormatter方法將HexFormatter實例設置爲默認值即可。 – phobic