Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JToggleButton truncates label text

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);
         }
      });
   }
}
like image 958
Dennis O'Neill Avatar asked Oct 31 '12 16:10

Dennis O'Neill


1 Answers

This problem is LookAndFeel-dependant, let me explain why...

This example indeed truncates text if MetalLookAndFeel is installed (it is by default). With any other L&F (Basic, Windows, Nimbus, even on my own L&F) i don't see this problem. Seems that MetalLookAndFeel has some kind of bug in the MetalButtonUI or in the L&F constants that forces incorrect button text rendering.

I am not sure what it could be - you can simply debug MetalButtonUI class to see what happens in the sizes calculations. Anyway, i doubt you will change anything even if you find the root of this problem.

like image 104
Mikle Garin Avatar answered Sep 29 '22 13:09

Mikle Garin