Results 1 to 13 of 13
  1. #1
    TheGoodGuy is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default 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!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    do you want this done in a JLabel? or something else?

  3. #3
    TheGoodGuy is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    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

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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 whatever
    Last edited by al_Marshy_1981; 09-04-2010 at 02:57 AM.

  5. #5
    TheGoodGuy is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    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.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    nope we can make this work in JTextArea i am sure, give me an hour :)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Take a look at the JEditorPane class.

  8. #8
    TheGoodGuy is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    hey norm, i already did, but i'm having the same problem... can i change only sone of the text in the JEdidorPane? thanks!

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by TheGoodGuy View Post
    hey norm, i already did, but i'm having the same problem... can i change only sone of the text in the JEdidorPane? thanks!
    Yes you can. Check out the tutorials.

  10. #10
    TheGoodGuy is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    yea ok got along! thanks, guys! :)

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

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

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    best I could do with JTextArea but looks like you got a solution anyhow

    Java Code:
    			yourTextToBold.setFont(new Font("",Font.BOLD,20));
    			yourTextToBold.select(yourTextToBold.getText().indexOf(match,0),match.length());
    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 ...

  13. #13
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    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 ...
    I know what you are saying and I agree but his requirement was not limited to bold font he also would have accepted highlighted text

Similar Threads

  1. 2 row display with multiple fonts
    By billq in forum New To Java
    Replies: 3
    Last Post: 02-04-2010, 11:45 AM
  2. Java Runtime Bad Fonts
    By ayacopino in forum New To Java
    Replies: 3
    Last Post: 01-19-2010, 04:23 PM
  3. PdfBox and barcode fonts
    By alexre2005 in forum New To Java
    Replies: 5
    Last Post: 12-03-2009, 02:59 PM
  4. Fonts (Changing Fonts and Color's)
    By dbashby in forum New To Java
    Replies: 10
    Last Post: 04-06-2009, 01:32 AM
  5. how to get raw image data for fonts
    By Nicholas Jordan in forum Java 2D
    Replies: 8
    Last Post: 04-05-2009, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •