• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having different text styles in the same JtextArea

 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. I'm using a bit of spare to to make a basic text editior. Basic as in all you can pretty much do is enter font, then change the font style and size.

At the moment it works great, you can have text of different styles and sizes... but it can only be applied to all text in the JTextArea, where I want it to only be applied to highlighted or current text... can this be done?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it can only be applied to all text in the JTextArea,
That is the limitation of the JTextArea class.
where I want it to only be applied to highlighted or current text... can this be done?
Yes. Try using a JTextPane with StyledDocument. More info here:
An Example of Using a Text Pane.

edit format.
[ March 17, 2007: Message edited by: Craig Wood ]
 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've had a look around the net using google and the link posted, but I can't seem to find much helpful information on styleddocumnets.

Are there any website or materials that show what a styleddocument is and how they are created/used? As i'm not sure what they are and how to implement one into a Text Pane
 
Paul Clapham
Sheriff
Posts: 21882
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the tutorial to read. But you have to spend a lot more than 45 minutes on it before you will understand it. It isn't a simple thing. So go back and have another look at it. Find an example that is somewhat similar to what you want to do and start fiddling with it.

You should be aware that you may have preconceived notions of how styled documents work, so while you are going through the tutorial try to clear those from your mind and see how it's done there. At least that's how I try to read tutorials.
 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean this tutorial
http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html#textpane
okay thanks, ill have a look.
Basically, I want to have a text area where users enter text and can select what text size and style to apply to each area of this text. Im having trouble finding code examples of this with styleddocuments.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Frank:
Basically, I want to have a text [pane] where users enter text and can select what text size and style to apply to each area of this text. Im having trouble finding code examples of this with styleddocuments.


The first 35 pages of chapter 22 of O'Reilly's Java Swing book is essentially the tutorial you are looking for.

It even includes a simple style-based text editor demo similar to the one you describe. The demo probably doesn't meet your needs exactly, since the styles apply to entire paragraphs (not to particular words or characters) but you can look at the code if you wish:
StyleFrame.java
StyleBox.java

disclaimer: I am the author of that chapter.

edit: fix markup errors
[ March 19, 2007: Message edited by: Brian Cole ]
 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, well I've figured it all out and got it working.. after a lot of time.
Basically everything is now working fine except I have a JcomboBox where the user selectes the size and this changes the text... except If i select the size 20, nothing will happen, then if i select the size 8 the selected text will change to size 20, and the next go the text will change to size 8.

basically the text size selection seems to be stuck one move behind what you actually choose. This is bugging me, could anyone PLEASE shed some light into what might be going on... here is the code I'm using to change the font size

combobox is initiated like this....


then in the action listener I put this...

 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone? I would appricaite any help greatly
 
Paul Clapham
Sheriff
Posts: 21882
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused as to what that code is supposed to do. Originally you were asking about how to use multiple styles in a text component. But now I don't see any references to text components in your code.

But you're talking about "the text" so obviously something is happening. Do you mean the text in your JComboBox? You keep setting its Action, which I'm not mistaken is the action which will be taken the next time something happens with the JComboBox. (I haven't used that component myself so I'm not sure what causes the action.) And that's consistent with what you describe. You set an Action that says "Make the font 20" and then the next time you do something to the component, it makes the font 20.
 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Jcombobox filled with numbers ranging from 2 to 20 which are intended to represent the size of the text entered into the JTextPane.

I use the "StyledEditorKit.FontSizeAction" to set the font size of the JTextPane, this font size is taken from what is currently selected in the Jcombobox.

I use the following code to do this

public void jComboBox4_actionPerformed(ActionEvent e) {
int thefontsize= (Integer) jComboBox4.getSelectedItem();
Action sizeAction = new StyledEditorKit.FontSizeAction("Font Size",thefontsize);
jComboBox4.setAction(sizeAction);
}

However when I use this action and select, say 20 from the combo box then text will not change from its original state, however if i select another value from the combobox then the text size will change to 20. this carries on with whatever previous value was selected in the combobox only being applied after selecting another value.

Im confused as to why this is happening. Any help would be appriciated
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Frank:

jComboBox4.setAction(sizeAction);


What do you expect this line to do?

In actuality, it has no immediate effect but sets an Action that will be invoked the next time the user makes a combo box selection.

You could try replacing that line with something like
sizeAction.actionPerformed(e);
 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did the trick. Thanks a lot!

Now i've just got to figure out how to make the JComboBox editable, as when it is editable the size doesnt seem to change when a value is entered or selected. It's very odd.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!