We help IT Professionals succeed at work.

java ButtonUI error

Anj78
Anj78 asked
on
282 Views
Last Modified: 2013-11-23
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);
  }
 
Comment
Watch Question

ksivananthVice President

Commented:
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);
}

Author

Commented:
I tried the above code.There is no error but button are not changed with Red color.
Can we have to set something?
Thanks...
ksivananthVice President

Commented:
have you overiden the below method in your LAF class,

public boolean isNativeLookAndFeel(){
  return false ;
}

Author

Commented:
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);
  }
Vice President
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.