Welcome to Dream.In.Code
Become a Java Expert!

Join 150,063 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,747 people online right now. Registration is fast and FREE... Join Now!




overriding paintComponent() in JButton

 
Reply to this topicStart new topic

overriding paintComponent() in JButton, Nothing is happening due to overrided paintComponent()

cybernaut09
14 Jun, 2008 - 12:46 PM
Post #1

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 20


My Contributions
I tried to override the paintComponent() method in JButton to give it a different look . But the program is not working , simple button is showing with the image I have provided as argument .Nothing is happening due to overrided paintComponent() method. Please help me understand where I am wrong.
I have a program where I have used:
CODE
myButton stop= new myButton("stop.png");


and the coding for this buttons is:
CODE

import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.geom.RoundRectangle2D;
import java.awt.Color;
import java.awt.Image;
import java.awt.GradientPaint;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.Paint;
import java.awt.*;
//import javax.swing.*;

@SuppressWarnings("serial")

class myButton extends JButton
{
    public myButton(String txt)
     {
        super(new ImageIcon(txt));
        setPreferredSize(new Dimension(50, 30));
     }
    public void paintComponent(Graphics g)
        {  
            
        Graphics2D g2 = (Graphics2D) g;
                GradientPaint p;
                p = new GradientPaint(0, 0, Color.RED, 0, getHeight(),Color.BLUE);

        Paint oldPaint = g2.getPaint();
        g2.setPaint(p);
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.setPaint(oldPaint);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        super.paintComponent(g);
        }
}


User is online!Profile CardPM
+Quote Post

Geez
RE: Overriding PaintComponent() In JButton
14 Jun, 2008 - 01:52 PM
Post #2

New D.I.C Head
*

Joined: 5 May, 2008
Posts: 23


My Contributions
QUOTE(cybernaut09 @ 14 Jun, 2008 - 01:46 PM) *

I tried to override the paintComponent() method in JButton to give it a different look . But the program is not working , simple button is showing with the image I have provided as argument .Nothing is happening due to overrided paintComponent() method. Please help me understand where I am wrong.
I have a program where I have used:
CODE
myButton stop= new myButton("stop.png");




someone correct me if I'm wrong, but I believe that in order for an icon to appear on your button you have to do this..

java


ImageIcon stopIcon = new ImageIcon("stop.png");

//then when you construct your button like this:

JButton stopButton = new stopButton("stop", stopIcon);

// this should work

java

super.paintComponent(g); // should be on top



also, I'm missing a setVisible(true); at the end to show your button
cheers

This post has been edited by Geez: 14 Jun, 2008 - 02:04 PM
User is offlineProfile CardPM
+Quote Post

cybernaut09
RE: Overriding PaintComponent() In JButton
14 Jun, 2008 - 02:05 PM
Post #3

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 20


My Contributions
Hey
got that working...just add :

[code]setContentAreaFilled(false);[/coode]

in myButton constructor.

This post has been edited by cybernaut09: 14 Jun, 2008 - 02:05 PM
User is online!Profile CardPM
+Quote Post

pbl
RE: Overriding PaintComponent() In JButton
14 Jun, 2008 - 02:07 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
CODE

public void paintComponent(Graphics g)
        {  

// what ever you do here   <---------------------------------------------------------            
        Graphics2D g2 = (Graphics2D) g;
                GradientPaint p;
                p = new GradientPaint(0, 0, Color.RED, 0, getHeight(),Color.BLUE);

        Paint oldPaint = g2.getPaint();
        g2.setPaint(p);
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.setPaint(oldPaint);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

//  will be override by that  
        super.paintComponent(g);    <--------------------------------
        }


You'll have to call super.paintComponent(g) BEFORE doing your customization
Also, I won't bet a lot on that, but I think it is the paint() method that you have to overload not the paintComponent() one


User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month