• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Formatting button labels

I need to change the font size, style and color of button labels.
Thnx in advance for your help.
0
rvaldivia
Asked:
rvaldivia
  • 4
  • 4
  • 2
  • +1
1 Solution
 
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
 
rvaldiviaAuthor Commented:
I doing applets,I'm not using html.
0
Technology Partners: 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!

 
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
 
OviCommented:
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
 
rvaldiviaAuthor Commented:
Thanx, just what I needed.
0
 
OviCommented:
me too :)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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