Skip to content
Advertisement

How to make SWT button Text BOLD

I am creating a checkbox button with text in SWT.

org.eclipse.swt.widgets.Button button= new Button(composite, SWT.CHECK);
button.setText(Messages.AnswerDialog_answer);
button.setSelection(true);

In messages.properties, I have the value

AnswerDialog_answer=Answer

How can i show the text(ie. Answer) of this button in BOLD?

Advertisement

Answer

You can convert the currently assigned font for the button to be bold using something like:

FontData [] data = button.getFont().getFontData();

for (FontData datum : data) {
   datum.setStyle(SWT.BOLD);
}

Font boldFont = new Font(button.getDisplay(), data);
button.setFont(boldFont);

Note: You must call boldFont.dispose() when you are done with the font (such as when the dialog closes).

I’m not sure that all platforms support changing the button font. The above code works on macOS.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement