1

i have a little issue with jlabel. When the label displays doubles which are to large for the screen, I would like to have a scrollbar to see them anyway. I have just added a scrollbar to the whole panel, but it does not check, when a overlong double is displayed.

here is my code

 public class OverviewPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private JLabel textNoNodes = new JLabel();
    private JLabel textNoEdges = new JLabel();
    private JLabel textInitial = new JLabel();
    private JLabel textTargets = new JLabel();
    private JLabel textFilename = new JLabel();
    private JLabel textProbabilityModelCheck = new JLabel();
    private JLabel textProbabilityCounterExample = new JLabel();
    private JLabel textNoSteps = new JLabel();

    public OverviewPanel() {
        super(new SpringLayout());
        addRow("Number states", textNoNodes);
        addRow("Number edges", textNoEdges);
        addRow("Initial", textInitial);
        addRow("Targets", textTargets);
        addRow("Filename", textFilename);
        addRow("Prob. model check", textProbabilityModelCheck);
        addRow("Prob. counter example", textProbabilityCounterExample);
        addRow("Number steps", textNoSteps);
        SpringUtilities.makeCompactGrid(this, 8, 2, // rows, cols
                6, 6, // initX, initY
                6, 6); // xPad, yPad
        setDefault();
    }

    private void addRow(String text, JComponent component) {
        JLabel l = new JLabel(text);
        add(l);
        l.setLabelFor(component);       
        add(component);
    }
   ...
   }
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user1140737
  • 81
  • 1
  • 2
  • 4
  • are you re_create OverviewPanel on Runtime or OverviewPanel is created once times and you want to resize JComponets depends of its contents, converting lenght of text to the pixels – mKorbel Jan 10 '12 at 13:22
  • It is initially created with default values and reloads when an action did happen, but not like a listener. the reload function is explicitly called. – user1140737 Jan 10 '12 at 13:45

3 Answers3

3

Use pack(), which "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents." An sscce may be helpful, too.

Addendum: An instance of DecimalFormat may be help control extreme size variability.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I do not see how pack() is applicable to the outPutPanel. In fact it works with frames, but this is a JPanel. – user1140737 Jan 10 '12 at 13:42
  • 1
    Yes, you should invoke `pack()` on the panel's enclosing `Window`. Dynamically, you can use `revalidate()`, typically followed by `repaint()`. – trashgod Jan 10 '12 at 13:47
  • Moreover, pack() will de-maximize a window... better use revalidate()+repaint() – gotch4 Jan 10 '12 at 17:38
2

IMHO, I would use a modified JTextArea (i.e. disabled, wrapped, and opaque). No scrolling, resizing, or font metric calculations required!

mre
  • 43,520
  • 33
  • 120
  • 170
1

I'm not sure, because I don't know springlayout, anyway I think you have to set the size of the label to the size of the text. To get it you can use:

FontMetrics fm = someComponent.getFontMetrics(someFont);
  int width = fm.stringWidth(someString);

maybe adding some extra space. Then with the label at the actual width the container should scroll...

Of course I'm talking of minimumSize or it is ineffective...

gotch4
  • 13,093
  • 29
  • 107
  • 170
  • 4
    This seems needlessly laborious; the `JLabel` itself can calculate its own preferred size on validation. – trashgod Jan 10 '12 at 13:25