Controlling the display of text in a JTextField
I have the following routine:
Code:
private void getEngineOutput(Process engine) {
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(engine.getInputStream()), 1);
String lineRead = null;
while((lineRead = reader.readLine()) != null) {
textArea.append(lineRead);
textArea.append("\n");
textArea.setCaretPosition(textArea.getDocument().getLength() - 0);
System.out.println(lineRead);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
It produces lines of data as follows:
Code:
5 31 0 2250 1. e4 Nc6 2. Nc3 Nf6 3. d4
5 31 0 2853 1. e4 Nc6 2. Nc3 Nf6 3. d4
6 9 0 3192 1. e4 Nc6 2. Nc3 Nf6 3. d4 Bd6
6 9 2 4298 1. e4 Nc6 2. Nc3 Nf6 3. d4 Bd6
7 49 2 6336 1. e4!
Is it possible to 'reverse' the output routine so that data outputs/prints at the top of the JTextField as opposed to outputting at the bottom? Using the above, I would like the data to print out as follows...
Code:
7 49 2 6336 1. e4!
6 9 2 4298 1. e4 Nc6 2. Nc3 Nf6 3. d4 Bd6
6 9 0 3192 1. e4 Nc6 2. Nc3 Nf6 3. d4 Bd6
5 31 0 2853 1. e4 Nc6 2. Nc3 Nf6 3. d4
5 31 0 2250 1. e4 Nc6 2. Nc3 Nf6 3. d4
Re: Controlling the display of text in a JTextField
Are you asking how to insert the new line at the front vs end?
Get the old text, concatenate it after the new text and put it back in the text field.
Or build the whole String, concatenating: new + old and then put it into the text field.
Re: Controlling the display of text in a JTextField
Quote:
Originally Posted by
Norm
Are you asking how to insert the new line at the front vs end?
Yes, well summarised :-)
I am not sure how to do it but I will research further on Internet
Re: Controlling the display of text in a JTextField
I gave a hint in my post#2
Re: Controlling the display of text in a JTextField
I have tried many permutations to concatenate the data to insert the new line at the front v end with no luck so far. I am having problems concatenating old and new data inside a loop. This is where I am so far
Code:
private void getEngineOutput(Process engine) {
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(engine.getInputStream()), 1);
String lineRead = null;
while((lineRead = reader.readLine()) != null) {
textArea.setText(lineRead); //changed from append to setText so that I can capture the current line
textArea.append("\n");
textArea.setCaretPosition(textArea.getDocument().getLength() - 0);
String oldLine = textArea.getText(); //pick up the old line
System.out.println(lineRead + "\n" + oldLine);//display new line and old line data
chat.showEngineAnalysis(lineRead.concat(oldLine));//send concatenated data to my text box
}
}
catch(Exception e) {
e.printStackTrace();
}
}
Can you give me a start as I am not sure I am approaching this correctly?
Thanks
Re: Controlling the display of text in a JTextField
Please post the text that your code generates.
You appear to be putting the Strings into two places: textArea and chat.showEngineAnalysis
using different techniques.
And you are printing out the Strings.
Which of these three ways are not doing what you want?
Are any of them doing what you want?
Code:
1. textArea.setText(lineRead); //changed from append to setText so that I can capture the current line
2. textArea.append("\n");
3. textArea.setCaretPosition(textArea.getDocument().getLength() - 0);
4. String oldLine = textArea.getText(); //pick up the old line
Your code at line 1 changes the textArea to contain only lineRead.
Your code at line 4 reads what was put into the textArea at line 1 + line 2
The original contents of textArea is GONE after line 1
Re: Controlling the display of text in a JTextField
I was trying different methods with different techniques but none of my my methods prints the line at the top of the JTextArea. I left the code as was...aploogies for the confusion.
This is the code that is generated by the line
Code:
chat.showEngineAnalysis(lineRead.concat(oldLine));//send concatenated data to my text box
Code:
5 33 0 1334 1. Nf3 Nc6 2. Nc3 Nf6 3. e3 5 33 0 1334 1. Nf3 Nc6 2. Nc3 Nf6 3. e3
6 5 0 1604 1. Nf3 Nc6 2. Nc3 Nf6 3. e3 d6 6 5 0 1604 1. Nf3 Nc6 2. Nc3 Nf6 3. e3 d6
6 5 0 2750 1. Nf3 Nc6 2. Nc3 Nf6 3. e3 d6 6 5 0 2750 1. Nf3 Nc6 2. Nc3 Nf6 3. e3 d6
7 27 0 5023 1. Nf3 Nf6 2. Nc3 Nc6 3. e3 e6 4. Bd3 7 27 0 5023 1. Nf3 Nf6 2. Nc3 Nc6 3. e3 e6 4. Bd3
This is the code generated by the line
Code:
System.out.println(lineRead + "\n" + oldLine);//display new line and old line data
Code:
5 33 0 1334 1. Nf3 Nc6 2. Nc3 Nf6 3. e3
5 33 0 1334 1. Nf3 Nc6 2. Nc3 Nf6 3. e3
6 5 0 1604 1. Nf3 Nc6 2. Nc3 Nf6 3. e3 d6
6 5 0 1604 1. Nf3 Nc6 2. Nc3 Nf6 3. e3 d6
6 5 0 2750 1. Nf3 Nc6 2. Nc3 Nf6 3. e3 d6
6 5 0 2750 1. Nf3 Nc6 2. Nc3 Nf6 3. e3 d6
7 27 0 5023 1. Nf3 Nf6 2. Nc3 Nc6 3. e3 e6 4. Bd3
7 27 0 5023 1. Nf3 Nf6 2. Nc3 Nc6 3. e3 e6 4. Bd3
7 31 0 7082 1. Nc3 Nc6 2. e4 Nf6 3. Nf3 e6 4. d4
7 31 0 7082 1. Nc3 Nc6 2. e4 Nf6 3. Nf3 e6 4. d4
Finally, this is the code outputted by the line
Code:
textArea.setText(lineRead); //changed from append to setText so that I can capture the current line
Code:
18 13 1367 15037384 1. Nc3 Nc6 2. Nf3 Nf6 3. e4 e5 4. d4 exd4 5. Nxd4 Bb4 6. Nxc6 Bxc3+ 7. bxc3 bxc6 8. Qd4 O-O 9. Bg5 d6 10. Bxf6 gxf6
Re: Controlling the display of text in a JTextField
can you post the code you are actually using?
Have you tried doing it this way?
String old = ... // get the current text
String new = ... // get the new text
String temp = new + "\n" + old; // put new in front of old
// Now temp has the new text in front of the old text
Re: Controlling the display of text in a JTextField
I tried your method previously - it doesn't work. I need to print the most recent line at the top of my JTextArea but instead the most recent line prints at the bottom. In effect I need the data displayed/sorted upside down if you are with me. Trapping the lineRead inside the loop is causing the problem as currently the data is duplicating and not printing top down.
Here is my code. Thanks
Code:
private void getEngineOutput(Process engine) {
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(engine.getInputStream()), 1);
String lineRead = null;
while((lineRead = reader.readLine()) != null) {
textArea.setText(lineRead); //changed from append to setText so that I can capture the current line
//textArea.append("\n");
//textArea.setCaretPosition(textArea.getDocument().getLength() - 0);
String oldLine = textArea.getText(); //pick up the old line
String newLine = lineRead;// get the new line
String temp = newLine + "\n" + oldLine; // put new in front of old
System.out.println(temp);//display new line and old line data
//chat.showEngineAnalysis(temp);// this is the JTextField where I would like the data to be sent when it is working
}
}
catch(Exception e) {
e.printStackTrace();
}
}
Re: Controlling the display of text in a JTextField
It may be worth noting that it doesn't make sense to keep setting text into a text component inside a loop. The text won't be displayed until after the method finishes and the component has a chance to refresh.
Also, when concatenating to a string inside a loop, it's way more efficient to use StringBuilder. It's the recommended way of doing it.
They know enough who know how to learn...
J. Adams
Re: Controlling the display of text in a JTextField
Quote:
Originally Posted by
peahead
I tried your method previously - it doesn't work. I need to print the most recent line at the top of my JTextArea but instead the most recent line prints at the bottom.
Of course it works (how could it not?) - you're just doing it wrong.
Try stepping through your loop code with pencil and paper, writing down the values of the variables as they change. You should immediately see that your loop needs rewriting.
Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson and G. Sussman
Re: Controlling the display of text in a JTextField
Did you read my post #6?
Your code is still doing what I pointed out there:
Code:
1 textArea.setText(lineRead); //changed from append to setText so that I can capture the current line
2 String oldLine = textArea.getText(); //pick up the old line
Line 1 changes the WHOLE contents of textArea. The old text is gone.
Line 2 gets what was placed into textArea at line 1. ie it gets what is in lineRead
Take dlorde's advice, work thru the contents of the variables using a piece of paper to see what is happening.
Re: Controlling the display of text in a JTextField
Norm & dlorde,
I have studied this loop but I cannot for the life of me figure out how to print lines at the top of my list.
I am trying method 1 proposed by Norm, i.e. Get the old text, concatenate it after the new text and put it back in the text field.
This is where I have got to
get the oldline from textArea.append(lineRead))
String oldLine = textArea.getText();;
get the new line = lineRead ???
String newLine = lineRead;
create and concatenate the temp string = new + "\n" + old;
String temp = newLine + "\n" + oldLine;
It's case of me not seeing the wood for the trees I'm afraid. I appreciate your help so far but I am truly stuck with this loop.
This is my code so far.
Code:
private void getEngineOutput(Process engine) {
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(engine.getInputStream()), 1);
String lineRead = null;
while((lineRead = reader.readLine()) != null) {
textArea.append(lineRead);
String oldLine = textArea.getText();
textArea.append("\n");
textArea.setCaretPosition(textArea.getDocument().getLength() - 0);
String newLine = lineRead;
String temp = newLine + "\n" + oldLine;
System.out.println(temp);
//chat.showEngineAnalysis(lineRead);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
Re: Controlling the display of text in a JTextField
Your posted code does NOT follow the steps that were recommended.
Edit your code and add comments to each statement line inside of the loop saying why you are doing it.
When you have done that, if you still don't see what is wrong with your code, post your commented code here.
Re: Controlling the display of text in a JTextField
Quote:
Originally Posted by
peahead
I have studied this loop but I cannot for the life of me figure out how to print lines at the top of my list.
I am trying method 1 proposed by Norm, i.e. Get the old text, concatenate it after the new text and put it back in the text field.
It's case of me not seeing the wood for the trees I'm afraid. I appreciate your help so far but I am truly stuck with this loop.
You say you are trying Norm's method, but the code simply isn't doing what you say it's doing. I think your existing incorrect code is confusing your thought processes. If you are not prepared to step through it on paper, line by line, to see what actually happens, I would suggest you throw it away and start afresh. This is not as drastic as it seems - you now know all the necessary elements to make it work, so you should be able to assemble them correctly. Start with a clean slate.
The key point is to solve the problem before writing the code. You have written some incorrect code, and despite all that we have been telling you, you can't let it go, and you can't spot the obvious errors. I hoped that stepping through by hand, writing down the results of each line of code, would allow you to see where it was wrong, but it seems not. However, if you work out, step by step, in plain English, exactly what must be done to achieve the task, it is not difficult to translate that understanding into Java. You have been trying to work out what has to be done and hack incorrect Java code at the same time, which is an order of magnitude more difficult.
Plan to throw one away; you will anyhow...
F. Brooks