Link to home
Create AccountLog in
Avatar of Anj78
Anj78

asked on

java look and feel

I am creating custom Look and Feel.Ihave Custom Look and feel calss.one custom ButtonUI class where i have change the color of button.and i hooked up the L&F with Configuration file.also i set look and feel with custom L&F. like
setLookAndFeel(
            "com.pjx.gui.view.laf.plaf.defaultlaf.DefaultLookAndFeel")//My Custom L&F
But i am getting runtime error that
fail to get UIManager.getUI().
Does it mean that i do not set or hook something?
I searched on google .i didn't get good results.
Avatar of bpmurray
bpmurray
Flag of Ireland image

That's not the full error message: if you post the full error message, it's much easier to answer. Anyway,  did you set the ID using the following structure:
 static  {
    UIManager.put(uiClassID, "com.pjx.gui.view.laf.plaf.defaultlaf.DefaultLookAndFeel");
  }

also might be helpful if you post your LAF impl!
Avatar of Anj78
Anj78

ASKER

The following are basic implementation of CuatomL&F and CUstom ButtonUI.I tried to  take out adding the custom ButtonUI from "initClassDefaults" method it works with out errorand  giving me WindowsL&F.


package com.pjx.gui.view.laf.plaf.defaultlaf;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.UIDefaults;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

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);

  }


HERE IS BUTTONUI IMPLEMENTATION>WHERE I want to change the color of button.I suspect that the following implementation does need more....

package com.pjx.gui.view.laf.plaf;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.ComponentUI;

/**
 * @
 *
 */
public class DefaultButtonUI extends ButtonUI{

  /**
   *
   */
  public DefaultButtonUI() {
    // TODO Auto-generated constructor stub
    super();
  }
 


 
 
  protected static DefaultButtonUI buttonUI;
 
  //////////////////////////////////////////////////////////////////////////////
  // Instance Methods - Getters and Setters
  //////////////////////////////////////////////////////////////////////////////

  // <----------------------------- PUBLIC ------------------------------> //
 
 
  public void installUI(JComponent c) {
    super.installUI(c);
  }

  public void uninstallUI(JComponent c) {
    super.uninstallUI(c);
 }
 
  public static ComponentUI createUI (JComponent c) {
    if (buttonUI == null) {
      buttonUI = new DefaultButtonUI();
    }
    return buttonUI;
  }
  public void paint(Graphics g, JComponent c)
  {
   //Invoke the original UI delegate
    buttonUI.paint( g, c );
 
    //   Now do the custom painting
   
  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);
}
Avatar of Anj78

ASKER

I did not have the followinh static code.will it be in my custom L&F class...?

static  {
    UIManager.put(uiClassID, "com.pjx.gui.view.laf.plaf.defaultlaf.DefaultLookAndFeel");
  }
try externding BasicButtonUI!
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Anj78

ASKER


I put this in CustomL&F.Declaration shows me error....

static  {
    UIManager.put(DEFAULTCLASSID, "com.pjx.gui.view.laf.plaf.defaultlaf.DefaultLookAndFeel");
  }

Declaration:


private static final Object DEFAULTCLASSID           = "com.pjx.gui.view.laf.plaf.defaultlaf.DefaultLookAndFeel";//$NON-NLS-1$
Avatar of Anj78

ASKER

Hi ksivananth,
i think i declaired it after use.Now i chaged it in the static block.
static  {
String DEFAULTCLASSID="com.pjx.gui.view.laf.plaf.defaultlaf.DefaultLookAndFeel";
    UIManager.put(DEFAULTCLASSID, "com.pjx.gui.view.laf.plaf.defaultlaf.DefaultLookAndFeel");
  }

But i don't get customizable button as Red.
Please guide me.