Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1092
  • Last Modified:

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.
0
Anj78
Asked:
Anj78
  • 4
  • 3
1 Solution
 
bpmurrayCommented:
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");
  }

0
 
ksivananthCommented:
also might be helpful if you post your LAF impl!
0
 
Anj78Author Commented:
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);
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Anj78Author Commented:
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");
  }
0
 
ksivananthCommented:
try externding BasicButtonUI!
0
 
ksivananthCommented:
>>I did not have the followinh static code.will it be in my custom L&F class...?

This can be anywhere, you just make sure that get executed before initializing any UI component!
0
 
Anj78Author Commented:

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$
0
 
Anj78Author Commented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now