Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: AWT
Expertise: Beginner
Mar 19, 1997

Highlighting/Making Bold Button Text

Question:
How do I highlight or make bold the text in a button or a list?

Answer:
Java Buttons, Lists, Labels, TextAreas, Canvases, etc, are all subclasses of the Java Component class which in turn defines generic methods for modifying the look and feel of Java's UI elements. The following are some of the methods in java.awt.Component that allow you to query or change a component's appearance on the screen:


        getFont()

        setFont()

        getForeground()

        setForeground()

        getBackground()

        setBackground()

        
For example, the applet below constructs a row of buttons, some with altered fonts, some with altered colors (to produce the high-lighted effect), and a list item with non-default fonts and colors:

import java.awt.*;

import java.io.*;

import java.applet.*;



//

// Create a number of buttons with different fonts and colors.

//

public class ButtonTest extends Applet {



        public void init() {

                Button b1 = new Button("Normal");

                add(b1);



                Button b2 = new Button("Bold");

                b2.setFont(new Font("TimesRoman", Font.BOLD, 14));

                add(b2);



                Button b3 = new Button("Italic");

                b3.setFont(new Font("TimesRoman", Font.ITALIC, 14));

                add(b3);



                Button b4 = new Button("Bold-italic");

                b4.setFont(new Font("TimesRoman", Font.ITALIC + Font.BOLD, 14));

                add(b4);



                Button b5 = new Button("High-lighted");

                b5.setForeground(Color.blue);

                b5.setBackground(Color.yellow);

                add(b5);



                List l = new List(4, false);

                l.setForeground(Color.black);

                l.setBackground(Color.red);

                l.setFont(new Font("TimesRoman", Font.ITALIC, 14));

                l.addItem("This is a test");

                l.addItem("This is also a test");

                add(l);

        }



        //

        // Defining a main() routine allows the applet to be

        // run as a standalone java application

        // using:  java ButtonTest

        //

        public static void main(String argv[]) {

                Frame f = new Frame("ButtonTest");



                Applet a = new ButtonTest();

                a.init();

                a.start();



                f.setLayout(new BorderLayout());

                f.add("Center", a);

                f.pack();

                f.show();

        }

}



DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date