Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5103247

Win L&F: Disabled JRadioButtonMenuItem & JCheckBoxMenuItem not painted correctly

    XMLWordPrintable

Details

    • Fix Understood
    • x86
    • windows_xp

    Description

      Name: rv122619 Date: 09/16/2004

      Disabled JRadioButtonMenuItems and JCheckBoxMenuItems are not drawn correctly by Swing. There are actually two problems. The first problem is the text is not drawn with the 3-d disabled look. The selected icon is also not drawn disabled.
       The first problem is caused because the paintText method in
      WindowsRadioButtonMenuItemUI and WindowsCheckBoxMenuItemUI do not override the paintText method like WindowsMenuItemUI does.

      Sample Code:

      import java.awt.Dimension;
      import java.awt.Point;
      import java.awt.event.ActionEvent;
      import java.awt.event.WindowEvent;

      import javax.swing.AbstractAction;
      import javax.swing.JCheckBoxMenuItem;
      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.JOptionPane;
      import javax.swing.JRadioButtonMenuItem;
      import javax.swing.UIManager;
      import javax.swing.UnsupportedLookAndFeelException;

      public class MainFrame extends JFrame
      {
         private AbstractAction m_actFile;
         private AbstractAction m_actExit;
         private AbstractAction m_actSave;
         private AbstractAction m_actSaveAs;
         private AbstractAction m_actPrint;
         private AbstractAction m_actProperties;
         private AbstractAction m_actNew;
         private AbstractAction m_actZoo;

         private AbstractAction m_actView;
         
         public MainFrame()
         {
            setDefaultCloseOperation( EXIT_ON_CLOSE );
            initialize();
            setLocation( new Point( 200, 200 ) );
            setSize( new Dimension( 550, 250 ) );
         }

         private void initialize()
         {
            setTitle( "MenuTest" );

            createActions();
            createMainMenu();
            createContents();
         }
         
         private void createActions()
         {
            m_actFile = new cFileAction();
            m_actExit = new cExitAction();
            m_actPrint = new cPrintAction();
            m_actProperties = new cPropertiesAction();
            m_actNew = new cNewAction();
            m_actZoo = new cZooAction();
            m_actSave = new cSaveAction();
            m_actSaveAs = new cSaveAsAction();
            
            m_actSave.setEnabled( false );
         }
         
         private void createMainMenu()
         {
            JMenuBar menuMain = new JMenuBar();

            JMenuItem miZoo = new JMenuItem( m_actZoo );
            JMenuItem miPrint = new JMenuItem( m_actPrint );
            miZoo .setVisible( false );
            miPrint.setVisible( false );
                  
            JMenu menuFile = new JMenu( m_actFile );
            menuFile.add( m_actNew );
            menuFile.add( miZoo );
            menuFile.add( m_actSave );
            menuFile.add( m_actSaveAs );
            menuFile.add( miPrint );
            menuFile.add( m_actProperties );
            menuFile.addSeparator();
            menuFile.add( m_actExit );
            
            JMenu menuView = new JMenu( "View" );
            menuView.setMnemonic( 'V' );
            JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem( "Disabled Check" );
            cbmi.setEnabled( false );
            cbmi.setMnemonic( 'C' );
            menuView.add( cbmi );
            cbmi = new JCheckBoxMenuItem( "Disabled Selected Check" );
            cbmi.setEnabled( false );
            cbmi.setSelected( true );
            cbmi.setMnemonic( 'k' );
            menuView.add( cbmi );
            
            JRadioButtonMenuItem rbmi = new JRadioButtonMenuItem( "Disabled Radio" );
            rbmi.setEnabled( false );
            rbmi.setMnemonic( 'R' );
            menuView.add( rbmi );
            rbmi = new JRadioButtonMenuItem( "Disabled Selected Radio" );
            rbmi.setEnabled( false );
            rbmi.setSelected( true );
            rbmi.setMnemonic( 'o' );
            menuView.add( rbmi );
            
            JMenuItem mi = new JMenuItem( "Disabled Normal" );
            mi.setEnabled( false );
            mi.setMnemonic( 'N' );
            menuView.add( mi );

            
            menuMain.add( menuFile );
            menuMain.add( menuView );
            
            this.setJMenuBar( menuMain );
         }

         private void createContents()
         {
         }
         
         public static void main( String[] saArgs )
         {
            // set the UI to the system UI
            try
            {
               UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
            }
            catch (ClassNotFoundException e) {}
            catch (IllegalAccessException e) {}
            catch (InstantiationException e) {}
            catch (UnsupportedLookAndFeelException e) {}

            // create the main frame and show it
            MainFrame f = new MainFrame();
            f.setVisible( true );
         }

            
         protected class cFileAction extends AbstractAction
         {
            public cFileAction()
            {
               putValue( AbstractAction.NAME, "File" );
               putValue( AbstractAction.SHORT_DESCRIPTION, "Files" );
               putValue( AbstractAction.MNEMONIC_KEY, new Integer( 'F' ) );
               putValue( AbstractAction.SMALL_ICON, null );
            }
            
            public void actionPerformed( ActionEvent e )
            {
            }
         } // cFileAction
         
         protected class cExitAction extends AbstractAction
         {
            public cExitAction()
            {
               putValue( AbstractAction.NAME, "Exit" );
               putValue( AbstractAction.SHORT_DESCRIPTION, "Exits" );
               putValue( AbstractAction.MNEMONIC_KEY, new Integer( 'x' ) );
               putValue( AbstractAction.SMALL_ICON, null );
            }
            
            public void actionPerformed( ActionEvent e )
            {
               // there should be a better way to do this but there is no close
               // and this seems to be what the system exit menu does, so ...
               MainFrame.this.dispatchEvent( new WindowEvent( MainFrame.this, WindowEvent.WINDOW_CLOSING ) );
            }
         } // cExitAction
         
         protected class cNewAction extends AbstractAction
         {
            public cNewAction()
            {
               putValue( AbstractAction.NAME, "New" );
               putValue( AbstractAction.SHORT_DESCRIPTION, "News" );
               putValue( AbstractAction.MNEMONIC_KEY, new Integer( 'n' ) );
               putValue( AbstractAction.SMALL_ICON, null );
            }
            
            public void actionPerformed( ActionEvent e )
            {
               JOptionPane.showMessageDialog( MainFrame.this, "New" );
            }
         } // cNewAction
         
         protected class cZooAction extends AbstractAction
         {
            public cZooAction()
            {
               putValue( AbstractAction.NAME, "Zoo" );
               putValue( AbstractAction.SHORT_DESCRIPTION, "Zoo" );
               putValue( AbstractAction.MNEMONIC_KEY, new Integer( 'z' ) );
               putValue( AbstractAction.SMALL_ICON, null );
            }
            
            public void actionPerformed( ActionEvent e )
            {
               JOptionPane.showMessageDialog( MainFrame.this, "Zoo" );
            }
         } // cZooAction
         
         protected class cSaveAction extends AbstractAction
         {
            public cSaveAction()
            {
               putValue( AbstractAction.NAME, "Save" );
               putValue( AbstractAction.SHORT_DESCRIPTION, "Saves" );
               putValue( AbstractAction.MNEMONIC_KEY, new Integer( 's' ) );
               putValue( AbstractAction.SMALL_ICON, null );
            }
            
            public void actionPerformed( ActionEvent e )
            {
               JOptionPane.showMessageDialog( MainFrame.this, "Save" );
            }
         } // cSaveAction
         
         protected class cSaveAsAction extends AbstractAction
         {
            public cSaveAsAction()
            {
               putValue( AbstractAction.NAME, "Save as" );
               putValue( AbstractAction.SHORT_DESCRIPTION, "Saves as" );
               putValue( AbstractAction.MNEMONIC_KEY, new Integer( 's' ) );
               putValue( AbstractAction.SMALL_ICON, null );
            }
            
            public void actionPerformed( ActionEvent e )
            {
               JOptionPane.showMessageDialog( MainFrame.this, "Save as" );
            }
         } // cSaveAsAction
         
         protected class cPrintAction extends AbstractAction
         {
            public cPrintAction()
            {
               putValue( AbstractAction.NAME, "Print" );
               putValue( AbstractAction.SHORT_DESCRIPTION, "Prints" );
               putValue( AbstractAction.MNEMONIC_KEY, new Integer( 'p' ) );
               putValue( AbstractAction.SMALL_ICON, null );
            }
            
            public void actionPerformed( ActionEvent e )
            {
               JOptionPane.showMessageDialog( MainFrame.this, "Print" );
            }
         } // cPrintAction
         
         protected class cPropertiesAction extends AbstractAction
         {
            public cPropertiesAction()
            {
               putValue( AbstractAction.NAME, "Properties" );
               putValue( AbstractAction.SHORT_DESCRIPTION, "Properties" );
               putValue( AbstractAction.MNEMONIC_KEY, new Integer( 'p' ) );
               putValue( AbstractAction.SMALL_ICON, null );
            }
            
            public void actionPerformed( ActionEvent e )
            {
               JOptionPane.showMessageDialog( MainFrame.this, "Properties" );
            }
         } // cPrropertiesAction
         
      }
      ======================================================================

      Attachments

        Activity

          People

            Unassigned Unassigned
            rverabel Raghu Verabelli (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Imported:
              Indexed: