Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[albireo-dev] setComponentFont comes too late

Hi Gordon,

While writing a view which creates 4 JLabels, each in a separate SwingControl,
I see the attached result. The code to create the 4 labels is identical.
Yet the preferred size of the first label, after creation, is 64x15 pixels,
and the one of the later labels is 70x17 pixels.

The reason is that setComponentFont is called after the first label has
been created. IMO it comes too late. There's no reason to wait for the first
component to be created before setting the - system-wide - look&feel parameters.

One could argue that the bug is in the sample: It should override the
updateAwtFont method to handle changes of the font. But this is a fit far-
fetched, since the method is not abstract and none of our existing samples
overrides it. The user is meant to implement this method if he wants good
reaction to system font changes, not also just for a good initial display.

Applying this:

Bruno


Index: src/org/eclipse/albireo/core/SwingControl.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.core/src/org/eclipse/albireo/core/SwingControl.java,v
retrieving revision 1.46
diff -c -3 -r1.46 SwingControl.java
*** src/org/eclipse/albireo/core/SwingControl.java	27 Feb 2008 13:56:35 -0000	1.46
--- src/org/eclipse/albireo/core/SwingControl.java	27 Feb 2008 15:55:36 -0000
***************
*** 207,218 ****
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  rootPaneContainer = addRootPaneContainer(frame);
                  swingComponent = createSwingComponent();
                  if (swingComponent != null) {
                      rootPaneContainer.getRootPane().getContentPane().add(swingComponent);
                  }
!                 setComponentFont();
!                 setComponentColors(foreground, background);
                  // Invoke hooks, for use by the application.
                  afterComponentCreatedAWTThread();
                  try {
--- 207,224 ----
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  rootPaneContainer = addRootPaneContainer(frame);
+ 
+                 // Customize the UI look&feel before calling
+                 // createSwingComponent(), so that preferred sizes will be
+                 // correctly computed during createSwingComponent().
+                 setComponentFont();
+                 setComponentColors(foreground, background);
+ 
                  swingComponent = createSwingComponent();
                  if (swingComponent != null) {
                      rootPaneContainer.getRootPane().getContentPane().add(swingComponent);
                  }
! 
                  // Invoke hooks, for use by the application.
                  afterComponentCreatedAWTThread();
                  try {
***************
*** 789,795 ****
          assert currentSystemFont != null;
          assert EventQueue.isDispatchThread();    // On AWT event thread
  
!         if ((swingComponent != null) && !currentSystemFont.getDevice().isDisposed()) {
              FontData fontData = currentSystemFont.getFontData()[0];
  
              // AWT font sizes assume a 72 dpi resolution, always. The true screen resolution must be
--- 795,801 ----
          assert currentSystemFont != null;
          assert EventQueue.isDispatchThread();    // On AWT event thread
  
!         if (!currentSystemFont.getDevice().isDisposed()) {
              FontData fontData = currentSystemFont.getFontData()[0];
  
              // AWT font sizes assume a 72 dpi resolution, always. The true screen resolution must be
***************
*** 807,816 ****
              // Allow subclasses to react to font change if necessary.
              updateAwtFont(awtFont);
  
!             // Allow components to update their UI based on new font
!             // TODO: should the update method be called on the root pane instead?
!             Container contentPane = swingComponent.getRootPane().getContentPane();
!             SwingUtilities.updateComponentTreeUI(contentPane);
          }
      }
  
--- 813,824 ----
              // Allow subclasses to react to font change if necessary.
              updateAwtFont(awtFont);
  
!             // Allow components to update their UI based on new font.
!             if (swingComponent != null) {
!                 // TODO: should the update method be called on the root pane instead?
!                 Container contentPane = swingComponent.getRootPane().getContentPane();
!                 SwingUtilities.updateComponentTreeUI(contentPane);
!             }
          }
      }
  

PNG image


Back to the top