Question

I have a JSlider and have 3 labels in my label table, one for 0 one for 50 and one for 100. Now in my ChangeListener state Changed I set the middle label. It works for all he numbers other then 100. Then for some reason it displays ... instead of 100:

    Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
    final JLabel middleLabel = new JLabel("50");
    labelTable.put(0, new JLabel("0"));
    labelTable.put(50, middleLabel);
    labelTable.put(100, new JLabel("100"));
    final JSlider slider = new JSlider(0, 100, 50);
    slider.addChangeListener(new ChangeListener()
    {
      @Override
      public void stateChanged(ChangeEvent e)
      {
        middleLabel.setText("" + slider.getValue());
      }
    });
    slider.setLabelTable(labelTable);
    slider.setPaintLabels(true);

enter image description here

Question is that is there any way i can display the 100 instead of the ... what it is currently displaying.

Was it helpful?

Solution

My guess is that the middle label is too small to display "100" because it was initially created with the narrower text "50", and JSlider doesn't act as a container which resizes its children as their preferred size changes, it just lays out the labels once.

Try initialising the label with "100" instead of "50".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top