Changeset 364:15840004a095 in finroc_tools_gui-java


Ignore:
Timestamp:
07.05.2020 15:59:35 (5 months ago)
Author:
Max Reichardt <max.reichardt@…>
Branch:
default
Phase:
public
Tags:
tip
Message:

Adapts Slider widget to step sizes below 0.01: Chooses suitable number of fraction digits from 'step size' property.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • widgets/Slider.java

    r322 r364  
    203203        valueLabel.setText("                                              "); 
    204204        FontMetrics metrics = minLabel.getFontMetrics(minLabel.getFont()); 
    205         invisibleLabel.setPreferredSize(new Dimension(Math.max(metrics.stringWidth(createValueLabelString(min)), metrics.stringWidth(createValueLabelString(max))) + 5, 0)); 
     205        invisibleLabel.setPreferredSize(new Dimension(Math.max(metrics.stringWidth(createValueLabelString(min, step)), metrics.stringWidth(createValueLabelString(max, step))) + 5, 0)); 
    206206        labelTable.clear(); 
    207207        labelTable.put(0, minLabel); 
     
    221221 
    222222    public void updateValueLabel() { 
    223         valueLabel.setText(createValueLabelString(min + getValue() * step)); 
    224     } 
    225  
    226     public static String createValueLabelString(double value) { 
    227         return "(" + String.format("%.2f", value) + ")"; 
     223        valueLabel.setText(createValueLabelString(min + getValue() * step, step)); 
     224    } 
     225 
     226    public static String createValueLabelString(double value, double stepSize) { 
     227        // Determine format 
     228        double stepSizeTemp = stepSize; 
     229        int fractionDigits = 0; 
     230        for (; fractionDigits < 12; fractionDigits++) { 
     231            stepSizeTemp *= 10; 
     232            int stepSizeTempInt = (int)stepSizeTemp; 
     233            if (stepSizeTempInt != 0 && (stepSizeTempInt % 10) == 0) { 
     234                break; 
     235            } 
     236        } 
     237 
     238        return "(" + String.format("%." + fractionDigits + "f", value) + ")"; 
    228239    } 
    229240 
Note: See TracChangeset for help on using the changeset viewer.