1

Okay, I'll admit - the title is not the most descriptive/helpful but I couldn't really think of a better way to put it, which is probably also why I couldn't find an answer when I was searching.

Basically, I'm making a basic MS Notepad like text editor and I want to add two JButton's to make a JTextArea's font Bold and Italic. I need someway of indicating whether the text is currently bold and/or italic like in MS Office programs where the background of the buttons are orange when text is bold, italic or underlined - only I can't seem to change the background of the button. I think this is due to the fact that I am using the operating system's look and feel, but that information still doesn't solve my problem.

So does anyone have any suggestions on how I can provide some feedback as to whether the text is bold and/or italic through the JButton like in Microsoft Office Word? Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Andy
  • 3,600
  • 12
  • 53
  • 84

3 Answers3

2

Hmm...sounds like you'll want to make use of the JToggleButton class.

mre
  • 43,520
  • 33
  • 120
  • 170
  • Yes! `JToggleButton`'s do the trick perfectly. Thank you! Just of curiosity though, is there anyway to change the colour when it is selected? – Andy Feb 13 '12 at 20:06
  • @Andy, I'm glad my answer helped. With regard to the question of setting the color of this button when selected, please see [this](http://stackoverflow.com/questions/5808022/changing-the-background-color-of-a-selected-jtogglebutton). – mre Feb 13 '12 at 20:08
  • Thanks for the link and thanks again for your answer - I can do what I need to do now... – Andy Feb 13 '12 at 20:25
0

You can set the background color of a JButton object with the method setBackground.

So:

JButtonName.setBackground(Color.ORANGE);
  • Unfortunately I've already tried that hence "I can't seem to change the background of the button" ( I really should have worded that fact better) but thank you anyway... – Andy Feb 13 '12 at 20:09
0

In addition to David's and mre's comments, JButtons and by extension JToggleButtons are not opaque by default, so the background is not painted. In addition to setting the background color, you also need:

jButtonName.setOpaque(true);
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
  • The `Background` isn't actually the colour of the JButton though, it's the colour of the area of the JPanel behind the JButton so therefore this line still doesn't do what I want unfortunately. Thanks anyway. – Andy Feb 13 '12 at 20:33