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\Blue Plaf>BlueB uttonUI.ja va:165: cannot find symbol
symbol : method initClassDefaults(javax.sw ing.UIDefa ults)
location: class javax.swing.plaf.basic.Bas icButtonUI
super.initClassDefaults(ta ble);
^
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.Bas icLookAndF eel;
//import javax.swing.UIDefaults;
//import javax.swing.plaf.metal.Met alLookAndF eel;
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 .backgroun d");
foreground = UIManager.getColor("Button .foregroun d");
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.g etWidth()- (i.right+i .left),b.g etHeight() - (i.bottom + i.top));
//the area that the text will be drawn in that will be defined
//by SwingUtilities.layoutCompo undLabel
Rectangle textRect = new Rectangle(0,0,0,0);
//the area that the icon will be drawn in that will be defined
//by SwingUtilities.layoutCompo undLabel
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.g etStyle(), fontSize)) ;
//modify text for display, will add ... if clipped and
//determine the text area and icon area
String text = SwingUtilities.layoutCompo undLabel(
c, g2.getFontMetrics(), b.getText(), b.getIcon(),
b.getVerticalAlignment(), b.getHorizontalAlignment() ,
b.getVerticalTextPosition( ), b.getHorizontalTextPositio n(),
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_ARG B);
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.g etGreen(), back.getBl ue(),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().dar ker();
bg = new Color(d.getRed(),d.getGree n(),d.getB lue(),127) ;
wh = Color.GRAY;
gr = Color.WHITE;
}
//set background color
gbi.setColor(bg);
gbi.fillRoundRect(0,0,c.ge tWidth(),c .getHeight (),arc_w,a rc_h);
//lay in the strips
gbi.setColor(Color.BLACK);
gbi.setComposite(AlphaComp osite.getI nstance(Al phaComposi te.SRC_IN, 1.0f));
gbi.setRenderingHint(Rende ringHints. KEY_ANTIAL IASING,Ren deringHint s.VALUE_AN TIALIAS_ON );
for(int j=0;j<c.getHeight();) {
gbi.fillRect(0,j,c.getWidt h(),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(Render ingHints.K EY_ANTIALI ASING, RenderingHints.VALUE_ANTIA LIAS_ON);
g2.setStroke(new BasicStroke(2.0f));
Arc2D.Double ar1;
ar1 = new Arc2D.Double(0,0,arc_w,arc _h,90,90,A rc2D.OPEN) ;
g2.draw(ar1);
ar1 = new Arc2D.Double(c.getWidth()- arc_w,1,ar c_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.getHeigh t()-arc_h+ 2);
g2.setColor(gr);
ar1 = new Arc2D.Double(c.getWidth()- arc_w,c.ge tHeight()- arc_h,arc_ w,arc_h,27 0,90,Arc2D .OPEN);
g2.draw(ar1);
ar1 = new Arc2D.Double(0,c.getHeight ()-arc_h,a rc_w,arc_h ,180,90,Ar c2D.OPEN);
g2.draw(ar1);
g2.fillRect(c.getWidth()-1 ,arc_h/2-2 ,1,c.getHe ight()-arc _h+8);
g2.fillRect(arc_w/2-8,c.ge tHeight()- 2,c.getWid th()-arc_w +16,2);
//painting text
g2.setRenderingHint(Render ingHints.K EY_TEXT_AN TIALIASING ,Rendering Hints.VALU E_TEXT_ANT IALIAS_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.getAs cent())
g2.drawString(text,(int)te xtRect.get X(),(int)t extRect.ge tY()+g2.ge tFontMetri cs().getAs cent());
//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)iconR ect.getX() ,(int)icon Rect.getY( ));
}
//Then you just add it to the UIDefaults table
protected void initClassDefaults(UIDefaul ts table) {
super.initClassDefaults(ta ble);
//package that the ComponentUI classes belong too
String pkg = "william.swing.plaf.blue." ;
Object[] classes = {
"ButtonUI" , pkg + "BlueButtonUI"
};
table.putDefaults(classes) ;
}
}
C:\Documents and Settings\sean\Desktop\Blue
symbol : method initClassDefaults(javax.sw
location: class javax.swing.plaf.basic.Bas
super.initClassDefaults(ta
^
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.Bas
//import javax.swing.UIDefaults;
//import javax.swing.plaf.metal.Met
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
foreground = UIManager.getColor("Button
font = UIManager.getFont("Button.
//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.setBackground(background
if(c.getForeground()==null
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.g
//the area that the text will be drawn in that will be defined
//by SwingUtilities.layoutCompo
Rectangle textRect = new Rectangle(0,0,0,0);
//the area that the icon will be drawn in that will be defined
//by SwingUtilities.layoutCompo
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.g
//modify text for display, will add ... if clipped and
//determine the text area and icon area
String text = SwingUtilities.layoutCompo
c, g2.getFontMetrics(), b.getText(), b.getIcon(),
b.getVerticalAlignment(), b.getHorizontalAlignment()
b.getVerticalTextPosition(
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()
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.g
//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().dar
bg = new Color(d.getRed(),d.getGree
wh = Color.GRAY;
gr = Color.WHITE;
}
//set background color
gbi.setColor(bg);
gbi.fillRoundRect(0,0,c.ge
//lay in the strips
gbi.setColor(Color.BLACK);
gbi.setComposite(AlphaComp
gbi.setRenderingHint(Rende
for(int j=0;j<c.getHeight();) {
gbi.fillRect(0,j,c.getWidt
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(Render
g2.setStroke(new BasicStroke(2.0f));
Arc2D.Double ar1;
ar1 = new Arc2D.Double(0,0,arc_w,arc
g2.draw(ar1);
ar1 = new Arc2D.Double(c.getWidth()-
g2.draw(ar1);
g2.fillRect(arc_w/2-2,0,c.
g2.fillRect(0,arc_h/2-2,2,
g2.setColor(gr);
ar1 = new Arc2D.Double(c.getWidth()-
g2.draw(ar1);
ar1 = new Arc2D.Double(0,c.getHeight
g2.draw(ar1);
g2.fillRect(c.getWidth()-1
g2.fillRect(arc_w/2-8,c.ge
//painting text
g2.setRenderingHint(Render
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.getAs
g2.drawString(text,(int)te
//If there is an icon paint it at the x and y of the iconRect
if(b.getIcon()!=null)
b.getIcon().paintIcon(c,g,
}
//Then you just add it to the UIDefaults table
protected void initClassDefaults(UIDefaul
super.initClassDefaults(ta
//package that the ComponentUI classes belong too
String pkg = "william.swing.plaf.blue."
Object[] classes = {
"ButtonUI" , pkg + "BlueButtonUI"
};
table.putDefaults(classes)
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
> 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)?
> 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)?
ASKER
Well with it commented out I get this error
C:\Documents and Settings\sean\Desktop\Blue Plaf>java BlueButtonUI
Exception in thread "main" java.lang.NoClassDefFoundE rror: BlueButtonUI (wrong name: william/swing/plaf/blue/Bl ueButtonUI )
at java.lang.ClassLoader.defi neClass1(N ative Method)
at java.lang.ClassLoader.defi neClass(Un known Source)
at java.security.SecureClassL oader.defi neClass(Un known Source)
at java.net.URLClassLoader.de fineClass( Unknown Source)
at java.net.URLClassLoader.ac cess$100(U nknown Source)
at java.net.URLClassLoader$1. run(Unknow n Source)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.net.URLClassLoader.fi ndClass(Un known Source)
at java.lang.ClassLoader.load Class(Unkn own Source)
at sun.misc.Launcher$AppClass Loader.loa dClass(Unk nown Source)
at java.lang.ClassLoader.load Class(Unkn own Source)
at java.lang.ClassLoader.load ClassInter nal(Unknow n Source)
I've uncommented the package statement at the top,
package william.swing.plaf.blue;
but it didn't help
C:\Documents and Settings\sean\Desktop\Blue
Exception in thread "main" java.lang.NoClassDefFoundE
at java.lang.ClassLoader.defi
at java.lang.ClassLoader.defi
at java.security.SecureClassL
at java.net.URLClassLoader.de
at java.net.URLClassLoader.ac
at java.net.URLClassLoader$1.
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.ClassLoader.load
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
That's the reason for the error probably
ASKER
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.
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
http://mindprod.com/jgloss/package.html
ASKER
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
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. ;-))
The accepted answer was actually incorrect (no offence i hope g. ;-))
Actually, technically it was correct :)
ASKER
but cannot find it in the Java 5.0 API
in the former it is listed in
javax.swing.plaf.basic.Bas
So I guess it's deprecated?
Do you know a replacement function to get it to compile?