Results 1 to 9 of 9
  1. #1
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default Line change in String is not working

    I am building a string. Everytime I have 3 together, I want them to be later displayed in a new line. I have tried multiple versions, but for some reason it is not working . May one of you have an idea, may even to work around it. What I have:
    Java Code:
    objectCount[0] ++;
    if (objectCount[0] == 1) 
    {
    	stringSeries[0] = project.getName();
    } else if (objectCount[0] > 1)
    {
    	stringSeries[0] = stringSeries[0] + ", " + project.getName();
    	if (objectCount[0] > linebreak)
    	{
    		stringSeries[0] = stringSeries[0] + ", \f\n" + project.getName();
    		objectCount[0] = 0;
    	}
    In the second last line I tried \n \f \n\f. The text I am building should be used as a text in a JFreeChart XYCategoryPlot - I hope that is not the problem.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Can you show what the program outputs now
    and also what you want it to output.

    What do you expect the \f character to do?
    Last edited by Norm; 05-30-2011 at 10:04 PM. Reason: Query re \f

  3. #3
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Current Output is: AAAA, BBBB, CCCC, DDDD, EEEE (all in one line)
    Output should be:
    AAAA, BBBB, CCCC,
    DDDD, EEEE (next line after value of linebreak (=3= is reached

    The idea of \f was a pagefeed - I just tried multiple version, since I have seen this in some code.

    An alternative way I have tried is:
    Java Code:
    String newline = System.getProperty("line.separator");
    Second last line:
    Java Code:
    stringSeries[0] = stringSeries[0] + ", " + newline + project.getName();

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Does your code put a newline (\n) after the CCCCC, ?
    Where are your storing the String and how are you displaying it?
    Can some filter be removing the \n character?

    Add the following just after the line that sets the value of stringSeries[0]:
    System.out.println("stringSeries[0]=" + stringSeries[0]); // show current value

    Also add printlns to show that the if tests you have are working. You could have a logic error and never execute the line with the \n

  5. #5
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    To Question 1: No, unfortunately no new line
    To Question 2:
    It is stored stringSeries[0] --> than in series[0] = new XYSeries(stringSeries[0]) --> than in dataset.addSeries(series[j])
    I want to display the text in a JFreeChart diagramm

    After adding your string I get:
    AAAA, BBBB, CCCC,
    CCCC (new line with same value as in the line before)

    In the diagram it still shows one line - I assume it is than a JFreeChart issue, which cannot display line changes

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

    Default

    Have you tried using HTML code with a line break. Since JFreeChart is based on Swing, there's a chance that it might recognize HTML line breaks. And by HTML I mean the most basic of HTML Strings like the following:

    Java Code:
    String text = "<html>Hello<br>World</html>";

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    I assume it is a JFreeChart issue
    Could be.

    Java Code:
    System.out.println("stringSeries[0]=" + stringSeries[0]); // show current value
    Did you ever add this line to show the value? I don't see an evidence in your post.

  8. #8
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Yes I have added the println line. I am not familiar with HTML, this is now the current version.
    Java Code:
    objectCount[1] ++;
    if (objectCount[1] == 1) 
    {
    	stringSeries[1] = "<html>" + project.getNameShort() + "<html>";
    } else if (objectCount[1] > 1)
    {
    	stringSeries[1] = stringSeries[1] + ", <html>" + project.getNameShort() + "<html>";
    	if (objectCount[1]  > linebreak)
    	{
    		stringSeries[1] = stringSeries[1] + ", <html><br />" + project.getNameShort() + "<html>";
    		objectCount[1] = 0;
    		System.out.println("stringSeries[1]=" + stringSeries[1]);
    	}
    }
    The result is 'interesting': <html>AAAA<html>, <html>BBBB<html>, <html>CCCC<html>, <html>DDDD<html>, <html><br />EEEE<html>
    May there is one last hint, to take advantage of the HTML idea

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    If that is the printed output from the program then It looks like the code in the if block with that println is NOT being executed. Check your logic.

Similar Threads

  1. new line within a string
    By perplexingtrax in forum New To Java
    Replies: 8
    Last Post: 04-16-2013, 08:17 PM
  2. how to change a single index of a string
    By ftrengnr in forum New To Java
    Replies: 5
    Last Post: 11-26-2010, 04:08 AM
  3. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  4. Change to string
    By PhQ in forum New To Java
    Replies: 7
    Last Post: 04-14-2010, 03:26 PM
  5. How to change string Color
    By Java.child in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2009, 04:27 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
  •