Kraagenskul
Kraagenskul

Reputation: 464

In Swing, what is the best listener to use in a textarea

I am trying to change the font of the text in a textarea in Swing. Which listener should I use on textarea to trigger an action that lets the program initiate the font code.

All the examples have all the swing in the same class which lets you access the textarea directly, but I have multiple classes; I know I can pass the textarea in and in and in, but this is sloppy.

I just cannot figure out which listener to initiate.

Upvotes: 1

Views: 1949

Answers (2)

camickr
camickr

Reputation: 324098

I am trying to change the font of the text in a textarea in Swing.

Well a JTextArea can only have a single Font, so if you want to change the Font you would have some other component, maybe a "Change Font" button that you would click. In this case you would add an ActionListener to the button to change the actual Font of the text area.

If you actually need to change the Font on selected pieces of text, then you also can't do this with a JTextArea. You would need to use a JTextPane. Read the JTextPane API and follow the link to the Swing tutorial on "Text Component Features" for an example of changing attributes on selected text. In this cause you use Actions provided by the editor kit.

So basically you need to read the Swing tutorial to find out the basics of using Swing components.

Upvotes: 2

user296828
user296828

Reputation:

If you're listening to the textarea, then it would depend on how many different ways you want the user to be able to change the font of what they are typing.

You could use MouseListener if you want them to be able to change the font on right click/etc... or a KeyListener if you want to listen for a series of keys.

Upvotes: 1

Related Questions