Anj78
asked on
java ButtonUI error
i am creating custom look and feel in java.I want to change the color of button as red and put it as default in table i did like this:It blows out all buttons and get runtime error:
public void paint(Graphics g, JComponent c)
{
//Invoke the original UI delegate
buttonUI.paint( g, c );
Rectangle r = c.getBounds();
g.setColor( Color.red );
g.drawLine( 0, 0, r.width, r.height );
g.drawLine( 0, r.height, r.width, 0 );
super.paint(g, c);
}
public void update(Graphics g, JComponent c) {
paint(g, c);
}
public void uninstallUI(JComponent c) {
super.uninstallUI(c);
}
public void installUI(JComponent c) {
super.installUI(c);
}
public static ComponentUI createUI (JComponent c) {
if (buttonUI == null) {
buttonUI = new DefaultButtonUI();
}
return buttonUI;
}
And this is customLookAndFeel
has :
protected void initClassDefaults(UIDefaul ts table) {
super.initClassDefaults(ta ble);
Object[] defaults = { "ButtonUI", "plaf.DefaultButtonUI" } ;
table.putDefaults(defaults );
}
public void paint(Graphics g, JComponent c)
{
//Invoke the original UI delegate
buttonUI.paint( g, c );
Rectangle r = c.getBounds();
g.setColor( Color.red );
g.drawLine( 0, 0, r.width, r.height );
g.drawLine( 0, r.height, r.width, 0 );
super.paint(g, c);
}
public void update(Graphics g, JComponent c) {
paint(g, c);
}
public void uninstallUI(JComponent c) {
super.uninstallUI(c);
}
public void installUI(JComponent c) {
super.installUI(c);
}
public static ComponentUI createUI (JComponent c) {
if (buttonUI == null) {
buttonUI = new DefaultButtonUI();
}
return buttonUI;
}
And this is customLookAndFeel
has :
protected void initClassDefaults(UIDefaul
super.initClassDefaults(ta
Object[] defaults = { "ButtonUI", "plaf.DefaultButtonUI" } ;
table.putDefaults(defaults
}
ASKER
I tried the above code.There is no error but button are not changed with Red color.
Can we have to set something?
Thanks...
Can we have to set something?
Thanks...
have you overiden the below method in your LAF class,
public boolean isNativeLookAndFeel(){
return false ;
}
public boolean isNativeLookAndFeel(){
return false ;
}
ASKER
yes i did.Here is code for L&F
public class DefaultLookAndFeel extends WindowsLookAndFeel{
////////////////////////// ////////// ////////// ////////// ////////// ////////// //
// Constructors
////////////////////////// ////////// ////////// ////////// ////////// ////////// //
// <------------------------- ---- PUBLIC -------------------------- -------> //
public DefaultLookAndFeel() {
super();
try {
javax.swing.UIManager.setL ookAndFeel (this);
} catch (Exception e) {
}
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// //
// Instance Methods - Getters and Setters
////////////////////////// ////////// ////////// ////////// ////////// ////////// //
// <------------------------- ---- PUBLIC -------------------------- ----> //
public String getID() {
return "My";
}
public String getName() {
return "Default Look and Feel";
}
public String getDescription() {
return "The Default Look and Feel";
}
public boolean isNativeLookAndFeel() {
return false;
}
public boolean isSupportedLookAndFeel() {
return true;
}
protected void initClassDefaults(UIDefaul ts table) {
super.initClassDefaults(ta ble);
// Object[] defaults = { "ButtonUI", "plaf.DefaultButtonUI" } ;
// table.putDefaults(defaults );
}
protected void initComponentDefaults(UIDe faults table) {
ColorUIResource buttonBackground =
new ColorUIResource(4, 108, 2);
super.initComponentDefault s(table);
Object[] defaults = {"Button.background", buttonBackground} ;
table.putDefaults(defaults );
}
public class DefaultLookAndFeel extends WindowsLookAndFeel{
//////////////////////////
// Constructors
//////////////////////////
// <-------------------------
public DefaultLookAndFeel() {
super();
try {
javax.swing.UIManager.setL
} catch (Exception e) {
}
}
//////////////////////////
// Instance Methods - Getters and Setters
//////////////////////////
// <-------------------------
public String getID() {
return "My";
}
public String getName() {
return "Default Look and Feel";
}
public String getDescription() {
return "The Default Look and Feel";
}
public boolean isNativeLookAndFeel() {
return false;
}
public boolean isSupportedLookAndFeel() {
return true;
}
protected void initClassDefaults(UIDefaul
super.initClassDefaults(ta
// Object[] defaults = { "ButtonUI", "plaf.DefaultButtonUI" } ;
// table.putDefaults(defaults
}
protected void initComponentDefaults(UIDe
ColorUIResource buttonBackground =
new ColorUIResource(4, 108, 2);
super.initComponentDefault
Object[] defaults = {"Button.background", buttonBackground} ;
table.putDefaults(defaults
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
//overidden Look and feel method
protected void initComponentDefaults(UIDe
super.initComponentDefault
Object[] defaults = {"Button.background", Color.RED} ;
table.putDefaults(defaults
}