Avadhani Y
Avadhani Y

Reputation: 7626

How to change the String Style

Can we change the String Style in java from normal to bold...

Example: I wish to change the String "Name" from normal Style to bold Style. Any default method included in java?

Upvotes: 0

Views: 7832

Answers (6)

Theron084
Theron084

Reputation: 155

If you are using a GUI System like a JLabel, JTextField etc, then you can go two ways:

eg:

JLabel myLabel = new JLabel("<html><b>My Text");

or

Font F = new Font("Calibre",Font.BOLD,12);
myLabel.setFont(f);

Hope that answers you Question.

Upvotes: 1

COD3BOY
COD3BOY

Reputation: 12092

There are two ways,

Either create a font by yourself ,

something like, Font abc = new Font("Arial",font.BOLD,15) and for any of the components you can use the setFont() method to set the font for the component.

See the javadoc for more

Use html,

Java supports html formating on all of its swing components.

Here is How to Use HTML in Swing Components.

Any default method included in java?

So I suppose for you its better to use a font, while setting the style via html means, if only for that text ,its not default for the component.

This is a generalized answer, for more specific answer,ask more specific question :)

Upvotes: 0

Costis Aivalis
Costis Aivalis

Reputation: 13728

The question is a bit vague, but in Swing you can use HTML in order to do format the way your strings appear in components:

jLabel1.setText("<html><b>Bold text</b></html>");
jButton1.setText("<html><b><i>Bold and Italic text</i></b></html>");

Upvotes: 3

Drona
Drona

Reputation: 7234

A String object in Java just holds the data. It does not have any styling associated with it. Display styles are associated with the presentation components. You sure are missing something.

Upvotes: 1

gprathour
gprathour

Reputation: 15333

It depends where do you want to change its style ?

If you are using Swings' components or AWT or something like that then surely you can do it.

Upvotes: 1

hage
hage

Reputation: 6153

A string is just the representation of some text. Applying some style information depends on the GUI framework you use: Swing, AWT, JSP, etc...

Upvotes: 2

Related Questions