When used in a Windows 7
JToolBar
, a JToggleButton
sometimes truncates its label text.
See an example in the code below. A toggle button that begins with an upper-case 'W'
will be truncated; one that begins with a space (or even lower-case 'w'
) will not.
Does this happen only under Windows
? Can someone explain why this happens?
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6386636
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/////////////////////// new class
public class Truncation_Example extends JToolBar {
private static final long serialVersionUID = 1L;
/////////////////////// object attributes
JToggleButton toggle_Good;
JToggleButton toggle_Bad;
/////////////////////// constructors
public Truncation_Example() {
toggle_Good = new JToggleButton(new Action_Good());
toggle_Bad = new JToggleButton(new Action_Bad());
this.add(toggle_Good);
this.add(toggle_Bad);
}
/////////////////////// inner classes
public class Action_Good extends AbstractAction {
private static final long serialVersionUID = 1L;
public Action_Good() {
putValue(Action.NAME, " Wrap Good "); // note added space to prevent truncation
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Toggle: " + toggle_Good.getText());
}
}
public class Action_Bad extends AbstractAction {
private static final long serialVersionUID = 1L;
public Action_Bad() {
putValue(Action.NAME, "Wrap Bad"); // label will be truncated if it begins with 'W'
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Toggle: " + toggle_Bad.getText());
}
}
/////////////////////// main
public static void main(String[] args) {
UIManager.put("ToggleButton.select", Color.GREEN);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Truncation_Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolBar = new Truncation_Example();
frame.add(toolBar, BorderLayout.NORTH);
frame.setSize(500, 400);
frame.setVisible(true);
}
});
}
}
setMinimumSize()
/setPreferredSize()
on theJToggleButton
help protect against the text truncation? – Guanabanajavax.swing.plaf.basic.BasicGraphicsUtils.getPreferredButtonSize(AbstractButton, int)
– Pinwormpack()
the frame beforesetSize()
. – Pantspack()
has no effect. – Granitewarepack()
trick too on my Windows 7 and it didn't change a thing. I think there might be a real problem in Oracle's JVM here. – Pinworm