Link to home
Start Free TrialLog in
Avatar of mitchguy
mitchguy

asked on

Compile error cannot find symbol: method initClassDefaults(javax.swing.UIDefaults)

I've been trying to get an example program I found about making your own PLAF to work, but can't seem to get past this error


C:\Documents and Settings\sean\Desktop\BluePlaf>BlueButtonUI.java:165: cannot find symbol
symbol  : method initClassDefaults(javax.swing.UIDefaults)
location: class javax.swing.plaf.basic.BasicButtonUI
    super.initClassDefaults(table);
         ^
1 error

The code I'm trying to compile is below
I've tried adding all of the import statements that i now commented out since it didn't help
The error is on Line 165 and is 9 lines up from the bottom

//package william.swing.plaf.blue;

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.text.View;
//import javax.swing.plaf.ButtonUI;
//import javax.swing.plaf.basic.BasicLookAndFeel;
//import javax.swing.UIDefaults;
//import javax.swing.plaf.metal.MetalLookAndFeel;


public class BlueButtonUI extends BasicButtonUI {

  //The singleton istance of BlueButtonUI
  static BlueButtonUI b = new BlueButtonUI();
  //Default background and foreground
  Color background;
  Color foreground;
  //There will be only one font for this these buttons
  Font font;

  public BlueButtonUI() {
    super();
  }


 //The factory method returns the singleton
 public static ComponentUI createUI(JComponent c) {
      return b;
  }
public void installUI(JComponent c) {
    //Since we know this is a JButton it is safe to cast as an AbstractButton
    AbstractButton b = (AbstractButton)c;

    //Setting the default values from the UIDefaults table
    background = UIManager.getColor("Button.background");
    foreground = UIManager.getColor("Button.foreground");
    font       = UIManager.getFont("Button.font");

    //Checking for user set values for foreground and background before setting them
    //Note that the font compnonent is not checked therefore the value from the UIDefaults table will
    //override the user’s values (This is not recommended) further not all the defaults are set
    if(c.getBackground()==null || (c.getBackground() instanceof UIResource))
      c.setBackground(background);

    if(c.getForeground()==null || (c.getForeground() instanceof UIResource))
      c.setForeground(foreground);
   
    //Using BasicButtonUI installListeners method to install listeners
    super.installListeners(b);
   
    /*Note that there are no keyboard registations, therefore hit any of the keys will not invoke an event*/
  }
//Paints a rounded button that is semi-transparent with lines

public void paint(Graphics g, JComponent c) {
   //Once again it is safe to cast as an AbstractButton because we know it is a JButton
    AbstractButton b = (AbstractButton)c;
   //The ButtonModel holds a lot of the functional state of the button
    ButtonModel model = b.getModel();
   //Casting to a Graphics2D for convenience, this is safew because we know that the g object is really a Graphics2D object
    Graphics2D g2 = (Graphics2D)g;

    //Sets the arcs widths and heights
    int arc_w = (int)c.getHeight()/2;
    int arc_h = arc_w;

    Insets i = c.getInsets();
    //Gets the area for the text and icon to be painted in with respects to the insets
    Rectangle viewRect = new Rectangle(i.left,i.top,b.getWidth()-(i.right+i.left),b.getHeight() - (i.bottom + i.top));
    //the area that the text will be drawn in that will be defined
    //by SwingUtilities.layoutCompoundLabel
    Rectangle textRect = new Rectangle(0,0,0,0);
    //the area that the icon will be drawn in that will be defined
    //by SwingUtilities.layoutCompoundLabel
    Rectangle iconRect = new Rectangle(0,0,0,0);

    //I have opted to set the base font size on the size of the button this will cause the font size to skrink or grow with respect to the button size
    int fontSize = (int)c.getHeight()/3;
    if(fontSize<8)
      fontSize = 8;
    g2.setFont(new Font(font.getName(),font.getStyle(),fontSize));
    //modify text for display, will add ... if clipped and
    //determine the text area and icon area
    String text = SwingUtilities.layoutCompoundLabel(
            c, g2.getFontMetrics(), b.getText(), b.getIcon(),
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
            viewRect, iconRect, textRect,
          b.getText() == null ? 0 : b.getIconTextGap());

    //Starting with a BufferedImage because the graphics object from a BufferedImage respects composite overlay directives
    //NOTE the Graphics object passed in to this method does not respect these directives
    BufferedImage buffImg = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();
    //Retrieving the state of the colors from the component which were set in the installUI method
    Color back = c.getBackground();
    Color fore = c.getForeground();

    //creating a semi-transparent background for the button
    Color bg = new Color(back.getRed(),back.getGreen(),back.getBlue(),127);
    //Defining the color of my borders
    Color wh = Color.WHITE;
    Color gr = Color.GRAY;
    //if button is pressed change the background to dark and switch the border colors (this makes it appear that the button is pressed in)
    if (model.isArmed() && model.isPressed()) {
      Color d = back.darker().darker().darker();
      bg = new Color(d.getRed(),d.getGreen(),d.getBlue(),127);
      wh = Color.GRAY;
      gr = Color.WHITE;
    }

    //set background color
    gbi.setColor(bg);
    gbi.fillRoundRect(0,0,c.getWidth(),c.getHeight(),arc_w,arc_h);
    //lay in the strips
    gbi.setColor(Color.BLACK);
    gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN,1.0f));
    gbi.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    for(int j=0;j<c.getHeight();) {
      gbi.fillRect(0,j,c.getWidth(),2);
      j=j+4;
    }

//paint button image
    g2.drawImage(buffImg,0,0,c);
    //Draw borders (NOTE a better implementation would have created a borders object)
    g2.setColor(wh);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(2.0f));
    Arc2D.Double  ar1;
    ar1 = new Arc2D.Double(0,0,arc_w,arc_h,90,90,Arc2D.OPEN);
    g2.draw(ar1);
    ar1 = new Arc2D.Double(c.getWidth()-arc_w,1,arc_w,arc_h,0,90,Arc2D.OPEN);
    g2.draw(ar1);
    g2.fillRect(arc_w/2-2,0,c.getWidth()-arc_w+2,2);
    g2.fillRect(0,arc_h/2-2,2,c.getHeight()-arc_h+2);

    g2.setColor(gr);
    ar1 = new Arc2D.Double(c.getWidth()-arc_w,c.getHeight()-arc_h,arc_w,arc_h,270,90,Arc2D.OPEN);
    g2.draw(ar1);
    ar1 = new Arc2D.Double(0,c.getHeight()-arc_h,arc_w,arc_h,180,90,Arc2D.OPEN);
    g2.draw(ar1);
    g2.fillRect(c.getWidth()-1,arc_h/2-2,1,c.getHeight()-arc_h+8);
    g2.fillRect(arc_w/2-8,c.getHeight()-2,c.getWidth()-arc_w+16,2);

    //painting text
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setColor(fore);
    //draw the text at the x of the textRect and the y textRect plus the font ascent.
    //"The font ascent is the distance from the font's baseline to the top of most
    //alphanumeric characters."(From Java API Doc on java.awt.FontMetrics.getAscent())
    g2.drawString(text,(int)textRect.getX(),(int)textRect.getY()+g2.getFontMetrics().getAscent());
    //If there is an icon paint it at the x and y of the iconRect
    if(b.getIcon()!=null)
      b.getIcon().paintIcon(c,g,(int)iconRect.getX(),(int)iconRect.getY());
  }

//Then you just add it to the UIDefaults table
 protected void initClassDefaults(UIDefaults table) {
    super.initClassDefaults(table);
    //package that the ComponentUI classes belong too
    String pkg       = "william.swing.plaf.blue.";
    Object[] classes = {
         "ButtonUI"  , pkg + "BlueButtonUI"
    };
    table.putDefaults(classes);
  }
}

ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mitchguy
mitchguy

ASKER

Well I found the function in the 1.4.2 API
but cannot find it in the Java 5.0 API
in the former it is listed in
javax.swing.plaf.basic.BasicLookAndFeel, which is why I tried using that import statement once

So I guess it's deprecated?

Do you know a replacement function to get it to compile?
>>This means that the superclass' initClassDefaults either does not get any parameters or the parameter you are passing is of the wrong type.

It actually doesn't exist at all. See if the method you mean exists in another package. If it does, you'll need to declare something like

public class BlueButtonUI extends x.y.z.BasicButtonUI
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CEHJ,
> It actually doesn't exist at all. See if the method you mean exists in
> another package. If it does, you'll need to declare something like

Yes you are right, it does not exist at all. It exists in another class, as you showed.

mitchguy I suggest you either extend the BasicLookAndFeel class or try to use another technique to do it.


mitchguy,
> Do you know a replacement function to get it to compile?

The issue is not to compile, you could just get rid of this line and it would compile. The issue is to get it working. What happens if you do not initialize it (i.e. get rid of the line)?
Well with it commented out I get this error

C:\Documents and Settings\sean\Desktop\BluePlaf>java BlueButtonUI
Exception in thread "main" java.lang.NoClassDefFoundError: BlueButtonUI (wrong name: william/swing/plaf/blue/BlueButtonUI)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

I've uncommented the package statement at the top,
package william.swing.plaf.blue;
but it didn't help
>>I've uncommented the package statement at the top,

That's the reason for the error probably
I guess I left out some information there, sorry
I originally commented it out, it was the first error I got after downloading it, something
in reference to the package, so I commented it out, until now when the error
was looking for it. So i tried restoring it to it's uncommented original form.
You need the classes all to be in the correct file system hierarchy according to their package specification

http://mindprod.com/jgloss/package.html
Make sure your class-path is set up correctly too:

http://www.mindprod.com/jgloss/classpath.html
Well as it turns out the example code i obtained was somebody's hacked up version. I found the original, which when looking at the code shows the function with my error in a whole other class i didn't even have. One that implements
BasicLookAndFeel as expected by comments above
Here is the link to the correct  example code
http://www17.homepage.villanova.edu/william.pohlhaus/is/lewis.htm



   
:-)

The accepted answer was actually incorrect (no offence i hope g. ;-))
Actually, technically it was correct :)