Formatting button labels

I need to change the font size, style and color of button labels.
Thnx in advance for your help.
rvaldiviaAsked:
Who is Participating?
 
OviConnect With a Mentor Commented:
About font  and font size, use button.setFont(new Font("Arial", Font.PLAIN, 11));
For color button.setForeground(Color.blue)/button.setBackground(Color.white)

About style, kwhat you are refering on ? As bold and italic you could use with the font object like button.setFont(new Font("Arial", Font.BOLD, 11)); if you are thinking on more complex behaviuor/look, objects has right, you must overwrite the paint method of your button and handle your customization there. You can do that by subclassing a normal button.
0
 
CEHJCommented:
If you're using Swing, you can do:

String labelText =
      "<html><FONT COLOR=RED>Red</FONT> and " +
      "<FONT COLOR=BLUE>Blue</FONT> Text</html>";
    JLabel coloredLabel =
      new JLabel(labelText, JLabel.CENTER);


   
0
 
CEHJCommented:
Or, more like your requirement:
JButton myButton = new JButton("<html><font color=\"red\">Hello World</font></html>");
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
rvaldiviaAuthor Commented:
I doing applets,I'm not using html.
0
 
CEHJCommented:
Doesn't matter
0
 
rvaldiviaAuthor Commented:
I doing applets,I'm not using html.
0
 
rvaldiviaAuthor Commented:
When I want to create a button I do the following

Button MyButton= new Button("Hello");

I am not using JButton.
0
 
CEHJCommented:
If you're not using Swing, you can't do this. If you are, the following will create a button with a red italic label:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

/**
 *Description of the Class
 *
 * @author     protean
 * @created    29 April 2002
 */
public class TestApplet extends JApplet {

  /**
   *Description of the Method
   *
   * @since
   */
  public void init() {
    try {
      getContentPane().setBackground(Color.WHITE);
      getContentPane().add(new JButton("<html><font color=\"red\"><em>Hello World</em></font></html>"));
      this.setSize(new Dimension(400, 300));

    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  //public void paint(Graphics g) { }

}
0
 
objectsCommented:
To do what you want with AWT you'll need to handle the painting of the button yourself.
0
 
rvaldiviaAuthor Commented:
Thanx, just what I needed.
0
 
OviCommented:
me too :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.