[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java ButtonUI error

Posted on 2007-10-04
5
Medium Priority
?
270 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);
  }
 
0
Comment
Question by:Anj78
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 20019680
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);
}

0
 

Author Comment

by:Anj78
ID: 20021284
I tried the above code.There is no error but button are not changed with Red color.
Can we have to set something?
Thanks...
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 20021394
have you overiden the below method in your LAF class,

public boolean isNativeLookAndFeel(){
  return false ;
}
0
 

Author Comment

by:Anj78
ID: 20021490
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);
  }
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 1500 total points
ID: 20021619
below code works for me!

package test;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

public class TestGridBagLayout extends JFrame{

      private void buildGUI(){
            JPanel pnlMain = new JPanel( new GridBagLayout() ) ;
            JLabel lblTitle = new JLabel( "Title" ) ;
            JTextField txtTitle = new JTextField( 10 ) ;
            JLabel lblDescription = new JLabel( "Description" ) ;
            JTextArea jtaDescription = new JTextArea() ;
            
            pnlMain.add( lblTitle, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
            pnlMain.add( txtTitle, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
            pnlMain.add( lblDescription, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 10 ), 0, 0 ) ) ;
            pnlMain.add( new JScrollPane( jtaDescription ), new GridBagConstraints( 0, 2, 2, 1, 1.0, 1.0,
                        GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
            pnlMain.add( new JButton( "Button" ), new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
            
            getContentPane().add( pnlMain ) ;
            setSize( 400, 400 ) ;
            setVisible( true ) ;
      }
      
      public static void main( String[] args ){
            final TestGridBagLayout test = new TestGridBagLayout() ;
            
            try{
                  UIManager.setLookAndFeel( "test.DefaultLookAndFeel" ) ;
                  EventQueue.invokeAndWait( new Runnable(){
                        public void run(){
                              test.buildGUI() ;
                        }
                  }) ;
            }catch( Exception e ){
                  e.printStackTrace() ;
            }
      }
}


0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question