Results 1 to 14 of 14
  1. #1
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default newline in a string

    I have searched everywhere and have gotten both the "use \n" which doesn't work for me, and the
    Java Code:
    String endofline = System.getProperty("line.Seperator");
    which just comes up null.


    What do I need to put for a newline of text?

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

  3. #3
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    ya no, thats spelled right in the code just not on here (I feel stupid now) but it still makes it all on one line.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    The line.separator property works perfectly fine. It must be how you use it on your code, which we cannot see. Hmmmm!

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    System.out.print("Hello\nThis is\n Using newline);
    That doesn't work correctly for you?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    That doesn't work correctly for you?

    The OP is attempting to use the operating system's line separator string and not the '\n' character.

    @OP: You really must post code that "still makes it all on one line" before anyone can make more than a guess as to what the problem is.

  7. #7
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    Alright, I will post when I get back to desktop.

  8. #8
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    So here is the entire Applet:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.lang.Object.*;
    
    public class Mouse1 extends Applet
    {
       int width, height;
       boolean isButtonPressed = false;
       private JLabel ending; 		 //field
    	private JButton pt1, pt2, pt3, pt4, mt1, mt2, mt3, mt4, teams;
    	int t1s, t2s, t3s, t4s;
    	String el = System.getProperty("line.Separator");
    
       public void init() {
       	
       	width = getSize().width;
          height = getSize().height;
          setBackground(Color.green);
    
    		setLayout(new FlowLayout());
    		
    		pt1 = new JButton("Team 1");
    		pt1.addActionListener(new ListenerOne());
    		pt1.setBackground(Color.green);
    		add(pt1);
    		
    		pt2 = new JButton("Team 2");
    		pt2.addActionListener(new ListenerTwo());
    		pt2.setBackground(Color.green);
    		add(pt2);
    		
    		pt3 = new JButton("Team 3");
    		pt3.addActionListener(new ListenerThree());
    		pt3.setBackground(Color.green);
    		add(pt3);
    		
    		pt4 = new JButton("Team 4");
    		pt4.addActionListener(new ListenerFour());
    		pt4.setBackground(Color.green);
    		add(pt4);
    		
    		mt1 = new JButton("Team 1");
    		mt1.addActionListener(new ListenerFive());
    		mt1.setBackground(Color.red);
    		add(mt1);
    		
    		mt2 = new JButton("Team 2");
    		mt2.addActionListener(new ListenerSix());
    		mt2.setBackground(Color.red);
    		add(mt2);
    		
    		mt3 = new JButton("Team 3");
    		mt3.addActionListener(new ListenerSeven());
    		mt3.setBackground(Color.red);
    		add(mt3);
    		
    		mt4 = new JButton("Team 4");
    		mt4.addActionListener(new ListenerEight());
    		mt4.setBackground(Color.red);
    		add(mt4);
    		
    		ending = new JLabel("Score:");
    		add(ending);
       }
    
       private class ListenerOne implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t1s = t1s + 1;
    			ending.setText("Score:" + el + "Team 1: " + t1s + el + "Team 2: " + t2s + el + "Team 3: " + t3s + el + "Team 4: " + t4s);
    		}
    	}
    	private class ListenerTwo implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t2s = t2s + 1;
    			ending.setText("Score:" + el + "Team 1: " + t1s + el + "Team 2: " + t2s + el + "Team 3: " + t3s + el + "Team 4: " + t4s);
    		}
    	}
    	private class ListenerThree implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t3s = t3s + 1;
    			ending.setText("Score:" + el + "Team 1: " + t1s + el + "Team 2: " + t2s + el + "Team 3: " + t3s + el + "Team 4: " + t4s);
    		}
    	}
    	private class ListenerFour implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t4s = t4s + 1;
    			ending.setText("Score:" + el + "Team 1: " + t1s + el + "Team 2: " + t2s + el + "Team 3: " + t3s + el + "Team 4: " + t4s);
    		}
    	}
    	private class ListenerFive implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t1s = t1s - 1;
    			ending.setText("Score:" + el + "Team 1: " + t1s + el + "Team 2: " + t2s + el + "Team 3: " + t3s + el + "Team 4: " + t4s);
    		}
    	}
    	private class ListenerSix implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t2s = t2s - 1;
    			ending.setText("Score:" + el + "Team 1: " + t1s + el + "Team 2: " + t2s + el + "Team 3: " + t3s + el + "Team 4: " + t4s);
    		}
    	}
    	private class ListenerSeven implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t3s = t3s - 1;
    			ending.setText("Score:" + el + "Team 1: " + t1s + el + "Team 2: " + t2s + el + "Team 3: " + t3s + el + "Team 4: " + t4s);
    		}
    	}
    	private class ListenerEight implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t4s = t4s - 1;
    			ending.setText("Score:" + el + "Team 1: " + t1s + el + "Team 2: " + t2s + el + "Team 3: " + t3s + el + "Team 4: " + t4s);
    		}
    	}
    	
    
       public void paint( Graphics g ) {
       }
    }
    Its the ending.setText in all eight actionListeners that i try to get to work but instead it shows up null in all of the "el" places.

    Thanks everyone for the help
    Last edited by Billaguana; 01-24-2011 at 01:33 AM.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Try taking the text and using System.out.println() to display it as well as using it as an argument to setText().

    Labels are not (just my opinion) for multiline text.

    But, if you really must, you can use an HTML string to set the text of a label. HTML uses "<br>" as a line separator. So you might try:

    Java Code:
    String br = "<br>";
    //...
    t1s = t1s + 1;
    ending.setText("<html>Score:" + br + "Team 1: " + t1s + br + "Team 2: " + t2s + br + [i]etc[/i]
    ---------------------------

    Better to use a text area - and it doesn't really care what line separator you use, so you might as well use '\n'.
    Last edited by pbrockway2; 01-24-2011 at 01:37 AM.

  10. #10
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    Thank you. I new it would be something so simple and I can't believe I forgot about <br>. Much Thanks!

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

    ------------------------

    I was thinking for a bit of limiting my post to the first line. I still think that's the most important bit. Printing the string and seeing in the applet console that it did have newlines would have led you to conclude that labels don't display new lines and that might have triggered the recollection of using HTML with components.

    In any case it would have shown that you were dealing with a label problem, and not a system property problem. 90% of solving a problem is to figure out what the problem actually is...

  12. #12
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    Haha, so true. Last thing, when I run it in the applet I get the first image as a start, and the second no matter what I click. Why is this?
    Attached Thumbnails Attached Thumbnails newline in a string-temp.png   newline in a string-temp1.png  

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It would be better to post this in the Swing/AWT forum. Include the code you are using now.

  14. #14
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2010, 09:26 AM
  2. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  3. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  4. Replies: 1
    Last Post: 04-27-2009, 10:19 AM
  5. Replies: 1
    Last Post: 01-15-2008, 08:55 PM

Posting Permissions

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