Hi, I am trying to calculate JButton's width so that the text inside can be properly displayed.
The point is, that I've got one JButton which text can be changed and I want to set the width of this button, so it should not need to be resized anymore
Here is my method:
private int getButtonMaxWidth(){
JButton butt = new JButton(); //I create one sample JButton, so I can get FontMetrics of it
FontMetrics fm = butt.getFontMetrics(butt.getFont());
int maxWidth = 0;
for (String s : buttonStrings) {
maxWidth = Math.max(maxWidth, fm.stringWidth(s));
}
return maxWidth + butt.getInsets().left + butt.getInsets().right;
}
the problem is, it does not work properly: it sets jbutton's width to half as it should be...
Could you advise me some solution?
thanks