2013-02-11 46 views
5

我想使用JSlider瀏覽一些按時間順序排列的事件,因此我設置了一個自定義LabelTable,以便它顯示一些日期而不是默認的整數值。我的代碼是這樣的:JSlider使用自定義LabelTable進行渲染

JSlider slider = new JSlider(); 
... 
Date[] dates = getDates(); 
     slider.setModel(new DefaultBoundedRangeModel(0, 0, 0, dates.length - 1)); 
     Hashtable<Integer, JLabel> ht = new Hashtable<Integer, JLabel>(); 
     for (int i = 0; i < dates.length; ++i) { 
      JLabel label = new JLabel(DateFormat.getDateInstance().format(dates[i])); 
      ht.put(i, label); 
     } 
     slider.setLabelTable(ht); 
     slider.setPaintLabels(true); 
     slider.setInverted(true); 

這工作得很好,因爲你可以看到: enter image description here

但是,如果我想改變滑塊的方向,說slider.setOrientation(JSlider.HORIZONTAL);,這裏是結果: enter image description here

如果我想要在每隔一個日期的滑塊上方顯示一個日期/一個日期,該怎麼辦?另外,我可以在垂直滑塊上的滑塊左側顯示標籤嗎?

問候, 雷米

+0

我會放置正確的,請張貼和[SSCCE](http://sscce.org/),簡短,可運行,可編譯,只是'JFrame','JSlider'和硬編碼值' JSlider' – mKorbel 2013-02-11 08:56:41

回答

7

更改標籤側(簡單地套用RTL方向):

public static void main (String[] args) 
{ 
    try 
    { 
     UIManager.setLookAndFeel (new NimbusLookAndFeel()); 
    } 
    catch (UnsupportedLookAndFeelException e) 
    { 
     e.printStackTrace(); 
    } 


    JFrame frame = new JFrame(); 

    JSlider slider = new JSlider (SwingConstants.VERTICAL); 
    slider.setPaintLabels (true); 
    slider.setComponentOrientation (ComponentOrientation.RIGHT_TO_LEFT); 

    Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>(); 
    table.put (0, new JLabel ("May 2, 2000")); 
    table.put (25, new JLabel ("May 3, 2001")); 
    table.put (50, new JLabel ("May 4, 2002")); 
    table.put (75, new JLabel ("May 5, 2003")); 
    table.put (100, new JLabel ("May 6, 2004")); 
    slider.setLabelTable (table); 

    frame.add (slider); 

    frame.pack(); 
    frame.setLocationRelativeTo (null); 
    frame.setVisible (true); 
} 

結果:

enter image description here

要改變標籤的位置,你將不得不修改他們手動。 這是可以做到的例子是這樣的:

public static void main (String[] args) 
{ 
    try 
    { 
     UIManager.setLookAndFeel (new NimbusLookAndFeel()); 
    } 
    catch (UnsupportedLookAndFeelException e) 
    { 
     e.printStackTrace(); 
    } 


    JFrame frame = new JFrame(); 
    frame.getRootPane().setBorder (BorderFactory.createEmptyBorder (5, 5, 5, 5)); 

    JSlider slider = new JSlider (SwingConstants.HORIZONTAL); 
    slider.setPaintLabels (true); 

    Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>(); 
    table.put (0, new JLabel ("May 2, 2000")); 
    JLabel l2 = new JLabel ("May 3, 2001"); 
    l2.setBorder (BorderFactory.createEmptyBorder (20, 0, 0, 0)); 
    table.put (25, l2); 
    table.put (50, new JLabel ("May 4, 2002")); 
    JLabel l3 = new JLabel ("May 5, 2003"); 
    l3.setBorder (BorderFactory.createEmptyBorder (20, 0, 0, 0)); 
    table.put (75, l3); 
    table.put (100, new JLabel ("May 6, 2004")); 
    slider.setLabelTable (table); 

    frame.add (slider); 

    frame.pack(); 
    frame.setLocationRelativeTo (null); 
    frame.setVisible (true); 
} 

結果:

enter image description here

Ofcourse,你可以提高代碼並添加按照之前的標籤首選高度的邊界(從首選尺寸拍攝)在您的標籤創建週期中,每個都會運行。

+0

正是我在找的東西,謝謝:) – remi 2013-02-11 10:11:21