[Ldsoss] AntiAlias Java

Tom Welch welchtc at ldschurch.org
Thu Aug 24 10:20:59 EDT 2006


Many java applications if using swing tend to have really ugly UI's.  
The fonts look terrible.  I wrote some code that can make them look a 
whole lot better.  This code requires jre 1.5.  Simply put this method 
into some type of utilities class and then call it whenever you want to 
make a component anti-aliased.  It will find all child components and 
anti-alias them as well.  You typically  just call this method from each 
form or dialog and all components on the form will get fixed.  I've 
tested this under both Windows and Linux and it works well.

*Disclaimer: * I'm not a long time java developer so if you see obvious 
problems or could offer suggestions, please do so.

public static void AntiAliasComponent(JComponent j) {
        
j.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY,Boolean.TRUE);
       
        int y = j.getComponentCount();
        for (int x=0;x<y;x++) {
            java.awt.Component comp = j.getComponent(x);
            if (comp instanceof JComponent) {
                AntiAliasComponent((JComponent)comp);
            }
            // Additional checks for certain types of controls.  These 
require special handling
            if (comp instanceof JList) {
                JList jl = (JList)comp;
                DefaultListCellRenderer 
d=(DefaultListCellRenderer)jl.getCellRenderer();
                
d.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY,Boolean.TRUE);
            }
            else if (comp instanceof JComboBox) {
                JComboBox cb = (JComboBox)comp;
                BasicComboBoxRenderer 
d=(BasicComboBoxRenderer)cb.getRenderer();
                
d.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, 
Boolean.TRUE);
            }
            else if (comp instanceof JTable) {
                JTable tab = (JTable)comp;
                DefaultTableCellRenderer d;
                d = 
(DefaultTableCellRenderer)tab.getDefaultRenderer(JLabel.class);
                
d.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, 
Boolean.TRUE);
               
                // Do the headers
                d = 
(DefaultTableCellRenderer)tab.getTableHeader().getDefaultRenderer();
                
d.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, 
Boolean.TRUE);
            }
        }
    }

Here is a sibling function to anti-alias the menus of a form.  Simply 
call this for your main menu bar and it will fix all sub-menus.

public static void AntiAliasMenu(JMenuBar j)
    {
        int a = j.getMenuCount();

        for (int b=0;b<a;b++) {
            int x=j.getMenu(b).getItemCount();
            
j.getMenu(b).putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY,Boolean.TRUE);

            for (int y=0;y<x;y++) {
                JMenuItem jmi = j.getMenu(b).getItem(y);
                if (jmi!=null)
                    
iii.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, 
Boolean.TRUE);
            }
        }
    }

Enjoy.

Tom

-- 
Tom Welch
welchtc at ldschurch.org
(801) 240-1609
(858) 829-4614 - Cell

------------------------------------------------------------------------------

 
NOTICE: This email message is for the sole use of the
 intended recipient(s) and may contain confidential and
 privileged information. Any unauthorized review, use,
 disclosure or distribution is prohibited. If you are not the
 intended recipient, please contact the sender by reply email
 and destroy all copies of the original message.

------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ldsoss.org/pipermail/ldsoss/attachments/20060824/ab287c91/attachment.htm


More information about the Ldsoss mailing list