0

I just want to set different style for JButton. So I overridden the paintComponent to achieve. It works successful but this button doesn't support the HTML format.

could you please suggest me

paintComponent()
~~~~~~~~~~~~~~~~
this is below my override code:
// prep for painting.  
      Graphics2D g2D = (Graphics2D)g;
      if(buttonStyle != 0 && buttonStyle != IMAGES){
        if(g == null)
            return;
        if(getBackground() == null)
          setBackground(acrcolor);
        if(getFadedBackgroundColor() == null)
          setFadedBackgroundColor(Color.white);

        g2D.clearRect(0, 0, getWidth()+getX(), getHeight()+getY());
        // end prep for painting.
      }
      switch (buttonStyle){
      case SKINZ:
        paintSkinz(g2D);
        return;
      case IMAGES:
        paintImages(g2D);
        break;
      case ROLLOVER:
        System.err.println("Rollover look of FuelButton not yet implemented.");
        break;
      case JAVA_LIKE:
        paintJavaLike(g2D);
        return;
      case GRADIENCE:
        paintGradience(g2D);
      case CLASSIC:
      case JAVA_DEFAULT:
      default:          
        super.paintComponent(g);
        m_originalborder = getBorder();
        m_originalfont = getFont();
        return;
      }
      paintText(g2D,0,0,getWidth(),getHeight());


paintJavaLike(g2D):
~~~~~~~~~~~~~~~~~~~~
g2D.setColor(getBackground());
      g2D.fill3DRect(0,0,getWidth(),getHeight(),true);
      if(getIcon()==null)
        paintText(g2D,0,0,getWidth(),getHeight());
      else if (getText() == null || getText().length() == 0)
        this.paintCenteredIcon(g2D, ((ImageIcon)getIcon()).getImage());
      else {
        int w = getWidth() - getIconTextGap() - getIcon().getIconWidth() - (borderWidth*2)-4;
        int h = getHeight()-(borderWidth*2);
        g2D.drawImage(((ImageIcon)getIcon()).getImage(), 2, (getHeight()/2)-(getIcon().getIconHeight()/2), this);
        paintText(g2D,2+getIcon().getIconWidth()+this.getIconTextGap(),0,w,h);
      }

thanks Palanisamy

kiheru
  • 6,588
  • 25
  • 31
user1280096
  • 137
  • 1
  • 1
  • 9

1 Answers1

1

You could provide a custom ButtonUI, and use that for the buttons, instead of overriding paintComponent(). Swing uses TextLayout for drawing the strings, and that can get complicated. However, BasicButtonUI has a protected paintText() method that can do what you want (you don't necessarily need to call it manually. The UI's paint() calls it, unless you override that too). So if you extend BasicButtonUI you can simply let it draw the string.

kiheru
  • 6,588
  • 25
  • 31
  • 1
    [for example](http://stackoverflow.com/questions/5751311/creating-a-custom-button-in-java-with-jbutton/5755124#5755124) – mKorbel Oct 07 '13 at 18:36