I have a class which extends JDialog which by default has some text on it and a couple of buttons. Most of the instance of the buttons are "Ok" and "Cancel" so the two buttons have their bounds set. If I try to change the label on the Ok button to something longer such "To Engage Plan" the text is truncated since the default size is too small. I want to be able to set the size of button based on its current insets, margins etc and use the new string to calculate the button width. I have tried:
okButton.setText(okLabel); Graphics g = okButton.getGraphics(); FontMetrics f = g.getFontMetrics(); Insets inset = okButton.getMargin(); Dimension d = okButton.getSize(); int size = f.stringWidth(okLabel); okButton.setSize(size+inset.left+inset.right, d.height);
This doesn't work as the default width is 120 and the calculation size+inset.left+inset.right = 99 in the case of the string "To Engage Plan". Any idea how to increase the button size?