Link to home
Start Free TrialLog in
Avatar of Anj78
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(UIDefaults table) {
    super.initClassDefaults(table);
    Object[] defaults = { "ButtonUI", "plaf.DefaultButtonUI" } ;
   table.putDefaults(defaults);
  }
 
Avatar of ksivananth
ksivananth
Flag of United States of America image

if you just want to change the background color of the button, try the below, no need of writing custom UI!

//overidden Look and feel method
protected void initComponentDefaults(UIDefaults table) {
  super.initComponentDefaults(table);
  Object[] defaults = {"Button.background", Color.RED} ;
  table.putDefaults(defaults);
}

Avatar of Anj78
Anj78

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...
have you overiden the below method in your LAF class,

public boolean isNativeLookAndFeel(){
  return false ;
}
Avatar of Anj78

ASKER

yes i did.Here is code for L&F



public class DefaultLookAndFeel extends WindowsLookAndFeel{
 
  //////////////////////////////////////////////////////////////////////////////
  // Constructors
  //////////////////////////////////////////////////////////////////////////////

  // <----------------------------- PUBLIC ---------------------------------> //
 
    public DefaultLookAndFeel() {
    super();
    try {
      javax.swing.UIManager.setLookAndFeel(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(UIDefaults table) {
    super.initClassDefaults(table);
   
//    Object[] defaults = { "ButtonUI", "plaf.DefaultButtonUI" } ;
//   table.putDefaults(defaults);

  }

  protected void initComponentDefaults(UIDefaults table) {
    ColorUIResource buttonBackground =
                                      new ColorUIResource(4, 108, 2);
    super.initComponentDefaults(table);
    Object[] defaults = {"Button.background", buttonBackground} ;
    table.putDefaults(defaults);
  }
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America 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