Results 1 to 14 of 14
Thread: newline in a string
- 01-23-2011, 07:56 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
-
- 01-23-2011, 10:47 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
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.
- 01-23-2011, 11:03 PM #4
The line.separator property works perfectly fine. It must be how you use it on your code, which we cannot see. Hmmmm!
- 01-23-2011, 11:27 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That doesn't work correctly for you?Java Code:System.out.print("Hello\nThis is\n Using newline);
- 01-23-2011, 11:54 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 01-24-2011, 12:03 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
Alright, I will post when I get back to desktop.
- 01-24-2011, 01:17 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
So here is the entire Applet:
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.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 ) { } }
Thanks everyone for the helpLast edited by Billaguana; 01-24-2011 at 01:33 AM.
- 01-24-2011, 01:35 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 01-24-2011, 01:48 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
Thank you. I new it would be something so simple and I can't believe I forgot about <br>. Much Thanks!
- 01-24-2011, 01:59 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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...
- 01-24-2011, 02:43 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
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?
- 01-24-2011, 07:03 PM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
It would be better to post this in the Swing/AWT forum. Include the code you are using now.
- 01-24-2011, 07:06 PM #14
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Basic string problem, just need to extract two numbers out of string
By gonzoateafly in forum New To JavaReplies: 6Last Post: 12-06-2010, 09:26 AM -
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 06:53 PM -
how to use newline attribute for chunk tag in xml?
By itextforbk in forum XMLReplies: 1Last Post: 04-27-2009, 10:19 AM -
Problem either with BufferedReader or sending a newline character
By aikanaro in forum NetworkingReplies: 1Last Post: 01-15-2008, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks