Reputation: 71
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);
}
...
}
Upvotes: 1
Views: 5015
Reputation: 205775
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.
Upvotes: 3
Reputation: 44240
IMHO, I would use a modified JTextArea
(i.e. disabled, wrapped, and opaque). No scrolling, resizing, or font metric calculations required!
Upvotes: 2
Reputation: 13259
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...
Upvotes: 1