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

Consider adding SwingUtilities.getUIComponents

    XMLWordPrintable

    Details

      Description

      A DESCRIPTION OF THE REQUEST :
      There are times when the developer needs to get access to the components added to JComponent from the UI. A couple of examples:
      http://forums.java.net/jive/thread.jspa?threadID=17748&tstart=0
      here the user needed to get to the JTextField to disable it so that it would be read-only. Though traversing the component hierarchy is an option, using reflection to get the field is more accurate.

      Consider this bug report/rfe:
      http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4232728

      In the case of the ComboBox, the user wanted to get access to the button. One way of doing this would be to use reflection to grab it.

      But as we can see Reflection requires a bunch of code that requires security permissions to use. One way to ease the users burden in these situations is to add to SwingUtilities a method called:
      public static Map<String,Component> getUIComponents(ComponentUI ui)

      that would return all the Components and their field name that are stashed in the ComponentUI. Then the user could simply find the field name of the widget in question and work with the Component.

      A negative to all this is that it violates encapsulation and may result in code that breaks from version to version. Though, given that the user is already doing this he is already relying on code that will break from version to version. The method should mention this if created.

      Dangerous? Well, yes. Useful? Well, yes.

      JUSTIFICATION :
      users need to get at the components that make up a composite.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      users can get at the components that make up a composite
      ACTUAL -
      users have to write their own access mechanisms to get at the composite components

      ---------- BEGIN SOURCE ----------
      Run this code and see what the user has to do to make the text field 'read-only':

      import javax.swing.*;
      import java.lang.reflect.*;
      import javax.swing.plaf.*;

      public class UneditableFileName{


          public static void makeUneditable(JFileChooser chooser){
      try{
      FileChooserUI ui = chooser.getUI();
                  Class c = ui.getClass();
                  Field f = null;
                  try{
                      f = c.getDeclaredField("filenameTextField");
                  } catch(Exception x){}
                  if(f == null)
                     f = c.getDeclaredField("fileNameTextField");
                  f.setAccessible(true);
                  JTextField jtf = (JTextField)f.get(ui);
                  jtf.setEditable(false);
              }
              catch(Exception x){
      x.printStackTrace();
              }
          }

          public static void main(String ... args){
              JFileChooser jfc = new JFileChooser();
              makeUneditable(jfc);
              jfc.showOpenDialog(null);

         
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      the executable case is an example of a workaround

        Attachments

          Activity

            People

            Assignee:
            svioletsunw Scott Violet (Inactive)
            Reporter:
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: