Results 1 to 13 of 13
Thread: Strings and fonts
- 09-04-2010, 02:45 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
Strings and fonts
Hello, guys, i'm new to this forum (:
Basically my situation is the following:
i wanna present a certain text on the screen, and every time i see a certain word (let's say 'cat', for this matter) i want it to be in bold. or underlined. or highlighted.
As far as i know i can't do it with a simple String variable.
I tried using a JtextArea and set it's font to a certain font, but the problem with that is the simple fact that this changes the *whole* text, and i only want some of it to be in bold.
any suggestions? thanks!
- 09-04-2010, 02:50 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
do you want this done in a JLabel? or something else?
- 09-04-2010, 02:51 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
well basically i'm hoping to do it in a JTextAea, or something like that, because i want the user to be able to copy-paste the result... wow, ur quick with the answer, lol
- 09-04-2010, 02:54 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
why would it matter if it is in bold if you wanna copy paste etc
anyway im pretty sure you can subclass JTextField and make its font bold or whateverLast edited by al_Marshy_1981; 09-04-2010 at 02:57 AM.
- 09-04-2010, 02:59 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
well, basically i'm trying to build like a mini google system
let's just say i wanna read from a txt file on my computer, show the file on the JTextArea or whatever, and every time i see a certain word (provided by the user) to make it bold. or highlighted. or anything like that.
but you know what, if you have a solution for JLabel, i'd be happy to accept that as well, i really need anything that can help.
- 09-04-2010, 03:05 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
nope we can make this work in JTextArea i am sure, give me an hour :)
- 09-04-2010, 03:06 AM #7
Take a look at the JEditorPane class.
- 09-04-2010, 03:21 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
hey norm, i already did, but i'm having the same problem... can i change only sone of the text in the JEdidorPane? thanks!
-
- 09-04-2010, 03:25 AM #10
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
yea ok got along! thanks, guys! :)
- 09-04-2010, 04:17 AM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
best I could do with JTextArea but looks like you got a solution anyhow
Java Code:import java.awt.Font; import javax.swing.*; import javax.swing.text.BadLocationException; public class HighLights { private JFrame myFrame; private String yourFileContents; private JTextArea yourTextToBold; public HighLights() { myFrame=new JFrame("highlights"); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setSize(500,500); String yourFileContents="Jimmy was here Jimmy"; String search=yourFileContents; String match="Jimmy"; if(yourFileContents.indexOf(match)!=-1) { yourTextToBold=new JTextArea(); yourTextToBold.setText(search); yourTextToBold.setFont(new Font("",Font.BOLD,20)); yourTextToBold.select(yourTextToBold.getText().indexOf(match,0),match.length()); } myFrame.getContentPane().add(yourTextToBold); myFrame.setVisible(true); } public static void main(String[]args) { new HighLights(); } }
- 09-04-2010, 07:59 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
A JTextArea can't do it; the code snippet above sets the font for the entire text to a boldfaced font. You either need a JTextPane or JEditorPane or you have to use the html text capabilities of some other JComponents.
kind regards,
Jos
ps. maybe something acceptible can be hacked in a JTextArea using special Highlighters ...
- 09-05-2010, 02:01 AM #13
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
2 row display with multiple fonts
By billq in forum New To JavaReplies: 3Last Post: 02-04-2010, 11:45 AM -
Java Runtime Bad Fonts
By ayacopino in forum New To JavaReplies: 3Last Post: 01-19-2010, 04:23 PM -
PdfBox and barcode fonts
By alexre2005 in forum New To JavaReplies: 5Last Post: 12-03-2009, 02:59 PM -
Fonts (Changing Fonts and Color's)
By dbashby in forum New To JavaReplies: 10Last Post: 04-06-2009, 01:32 AM -
how to get raw image data for fonts
By Nicholas Jordan in forum Java 2DReplies: 8Last Post: 04-05-2009, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks