Show user that something is on from JButton - java

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.

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

You can set the background color of a JButton object with the method setBackground.
So:
JButtonName.setBackground(Color.ORANGE);

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);

Related

JTextArea - are 2 different highlight colors possible?

I want to highlight the text in a JTextComponent with two different colors. Is this possible with JTextArea (and if yes how?) - or do I need to use JTextPane or JEditorPane (if latter: which one is to recommend if the only thing I really want to 'style' are different background colors)?
thanks in advance

How to give different - 2 color to text written in JTextArea?

I want to change color of text written in JTextArea. For e.g. I am pasting Java code in JTextArea.
I want to give the different-2 color to Java keywords, variables and classes written in code.
Is it possible?
See Rob Camick's Message Console. It seems perfect for this use case.
Actually '2 color' might not be enough for this.
See How to Use Editor Panes and Text Panes for more details on the formatted text components that might replace JTextArea.
it's not possible
if you want to set different colors in the same area, i think JTextPane could help you
i'm not sure how to use that, but i found this
http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

Is it possible to change the background of a jspinner using the nimbus laf?

I'm fairly confident that I have done my research before coming to you for help, but it's possible I have overlooked something.
I'm writing a java UI using the Nimbus l-a-f. I wish to change the background colour of a JSpinner on state-change, ie, when either the up or down button is pressed the background colour of the textfield within the jspinner changes colour to signify that the value has been altered.
I am aware this is possible using OTHER lookandfeels but not as easy with Nimbus, eg:
((JSpinner.NumberEditor)jSpinner1.getEditor()).getTextField().setBackground(color.yellow);
I have also looked into actually changing the colour theme of the UI manager, but I only want to change the colour when an action occurs, not just overall by default.
here or here
Is this at all possible and where do I start?
Failing this, I was hoping to just change a button's colour:
jButton.setBackground(Color.yellow);
This is easy enough but since the default colour is a painted gradient, how do I change it back to that?
Really hope someone out there knows or can help.
Much appreciated in advance!
Yes, it is possible. See this example (i.e. SpinnerDemo4) from The Java Tutorials for more detail. And yes, I did set the LaF to Nimbus when testing the code therein.

Make a text field highlighted when tabbed to in NetBeans (Java)

I'm trying to make it so when I tab to some text fields on in my JFrame the data in the text field will be highlighted. I thought I had done this in the properties before but I am not seeing the option now. Does anyone know how to do this?
You could use a FocusListener to work out when your field has focus (tutorial here).
Decide whether you want to select the text in the field, or whether you just want to change the background color. It's not quite clear what you mean by highlight.

Using <html><u> tags to make button text underlined, caused button to take up entire JToolBar

I need to have a button whose text is underlined and the only way I could find to do this in Java was to use and tags, but when I do this, it causes the button to take up as much room as is left in the JToolBar even though the text is short and it should only take up a small amount of space. Here is how I create the Button.
String buttonText = new String("<html><u>Lesson Plans</u></html>");
JButton lessonButton = new JButton(buttonText);
toolBar.add(lessonButton);
If I remove the tags then it takes up the right amount of space but if I have them in there is takes up the entire toolBar. Anyone know what's going on?
You might be able to fix the problem by using:
button.setMaximumSize( button.getPreferredSize() );
Otherwise you should be able to just change the font to use an underlined font. Darryl's Visual Font Designer shows how to add attributes to a font.
You can overwrite the paintComponent method of your JButton, and write on it with any style and font.
You forgot the closing "" and wrote "" instead... This may be the reason for your problems.

Categories

Resources