How to add text to new line - java

I want to add text to new line.
My code:
TextField text = new TextField();
TextArea area = new Area();
String txt = text.getValue().toString();
area.setValue("\n" + txt);
When I click in my button I see my value from TextField. I want new text in new line in TextArea. Please help.

You have to append that new value to the existing one and set that. Something along the lines of:
area.setValue(area.getValue() + "\n" + txt);
The Vaadin TextArea has no direct way to append. Also the rules in java, when to use + for stringsapply. Consider using a StringBuffer, if you do this alot.

Instead of
area.setValue("\n" + txt);
Use
area.setValue(area.getValue + "\n" + txt);
This will ADD text, not REPLACE

I recommend to add the new line at the end. You will avoid having an empty line at the beginning in your TextArea.
E.g. outputTextArea.setValue(outputTextArea.getValue() + input.getValue() + "\n");
Furthermore: Make sure to use a TextArea and not a TextField, because line breaks with "\n" won't work in TextField!

Keep in mind that Vaadin works with browsers, so normal scape characters doesn't work, ie: you cannot use '\n' on normal Label. Instead of it do the following:
label.setCaptionAsHtml(true);
label.setValue(label.getValue()+"<br>");
If you are using a TextArea, you can use '\n' as usual.
In case of TextFiel, you cannot write multiple lines, as it would convert it in a TextArea and this behavior is deprecated.

Related

Changing HTML to PlainText

I'm trying to edit HTML to be plaintext in java, but I am running into an issue. I am trying to get the number on the padding-left element in the code and transform it into tabs but it doesn't work.
ie.
<p style="padding-left:40px;">Hello</p> becomes Hello with a tab in front of it.
Here is my code so far (every 40px becomes one tab)
private static String setNonHTML(String txt)
{
System.out.println(txt.substring(txt.indexOf("<p style=\"padding-left:") + 23, txt.indexOf("px\"><b>")));
//return "";
return txt
.replaceAll("<br>","\n")
.replaceAll(txt.substring(txt.indexOf("<p style=\"padding-left:"), txt.indexOf("px\"><b>") + 7)
,"\n" + repeat("\t",Integer.parseInt(txt.substring(txt.indexOf("<p style=\"padding-left:") + 23, txt.indexOf("px\"><b>")))/40))
.replaceAll(txt.substring(txt.indexOf("<p style=\"padding-left:"), txt.indexOf("px\">") + 4)
,"\n" + repeat("\t",Integer.parseInt(txt.substring(txt.indexOf("<p style=\"padding-left:") + 23, txt.indexOf("px\">")))/40))
.replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*", "\n");
}
I cleaned up some of your code to show you what is happening
private static String setNonHTML(String txt)
{
System.out.println(txt.substring(txt.indexOf("<p style=\"padding-left:") + 23, txt.indexOf("px\"><b>")));
//return "";
//grab the padding text indexes
int beforePaddingIndex = txt.indexOf("<p style=\"padding-left:");
int afterPaddingIndex = txt.indexOf("px\"><b>");
//replace all breaks with new lines
txt = txt.replaceAll("<br>", "\n");
//replaces all instances of 40px\"> with \n\t
txt = txt.replaceAll(txt.substring(beforePaddingIndex, afterPaddingIndex + 7), "\n" + repeat("\t", Integer.parseInt(txt.substring(beforePaddingIndex + 23, afterPaddingIndex)) / 40));
//the indexes of these items have changed because the last operation replaced them. The following items will not have indexes due to the replace operation.
beforePaddingIndex = txt.indexOf("<p style=\"padding-left:");
afterPaddingIndex = txt.indexOf("px\"><b>");
afterPaddingBeforeBoldIndex = txt.indexOf("px\">");
//replace a substring of the same tag a second time? should find nothing
txt = txt.replaceAll(txt.substring(beforePaddingIndex, afterPaddingIndex), "\n" + repeat("\t", Integer.parseInt(txt.substring(beforePaddingIndex + 23, afterPaddingBeforeBoldIndex)) / 40));
txt = txt.replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*", "\n");
return txt;
}
as you can see, after the first replace all, there is a second replace all that takes place on virtually the same indexes. You grab the index of values inline after the first replace all so I set them again to replicate that behavior. Splitting out code into descriptive variables and sections is a good practice and is monumentally helpful when trying to debug complicated sections. I don't know what the output of your program is giving you, so I have no way to know if this actually solves your issue, but it does look like a bug and I believe this might give you a good start.
As for what you should do to fix this, you may want to look into some off the shelf solution like http://htmlcleaner.sourceforge.net/javause.php
That allows you to traverse and modify html programmatically and read off attributes like padding left and the extract content between tags.

Java Android, listing multiple stored values in settext

I have a blank text field which I'm trying to use settext on after a button click, so I can change the blank text field to the text in the stored variables.
The variables are two ints which I've converted to string and I'm trying to do the following:
blankText.setText("" + var1);
currently works but when I try to add the other variable in the same field I'm not sure how to go about it?
For example I tried to do: blankText.setText("" + var1, var2) which throws an error. I want them listed side by side in the same text field. is this possible?
To combine Strings, use +:
blankText.setText(var1 + var2);
blankText.setText(var1 + " " + var2);
blankText.setText("" + var1 + " " + var2);
if you want to space your variables
If you are trying to add or append the text in already written textview then use
blankText.append(""+var2);
if you want to write two variables in textview at the same time then use the concatination symbol
blankText.setText(" " + var1 + " " + var2); // also will remove the previous written text

read a file using scanner and save it in string including white space

I have a text file that i want to read. I have no problem doing that.
My problem is that i need to check if a line has a white space or not.
for example, lets assume the below is my text file. I want to save "something" in new string and if it has nothing in that column and row then i want to " " as string.
Column1 Column2 Column3
Row1 Something Something Something
Row2 Something Something
Row3 Something
i tried to read the file with scanner and and save each line in new string. but i have no clue to how to get the white space from the string. i'm not sure if this method will work or not.
any suggestions
thanks
If you want to check for white space just use
String out;
if(inputLine.indexOf(" ")!=-1){
// Whitespace exists in string
out = "something";
} else {
// No whitespace exists
out = " ";
}

get and write line with some number

I need to get certain line from text component with multiline support. So it either JTextArea or JTextPane.
How to get line 1 , 2 or .. etc? For example get line3 from text below
line1
line2
line3
line4
And is it possible to set another value for some line? For examp. set lineNew instead of line2
line1
lineNew
line3
line4
Is there any way?
To grab the text in a JTextComponent, use the getText() method which will return a String.
Then to get the lines, split the string on \n.
JTextArea txt = new JTextArea("line1\nline2\nline3\nline4");
String s = txt.getText();
String[] lines = s.split("\n");
// now to access the second line, use lines[1]
Now if you want to modify the text, you can use the setText(String) method.
txt.setText("something else");
There are also a few other methods that you can use to change the text like insert(String,int), append(String), and replaceRange(String,int,int). All of this is documented in the javadocs.
You can use JTextArea#replaceRange to replace a certain line.
For retrieving a certain line I am not completely sure, but I think that the JTextArea#getLineCount, JTextArea#getLineStartOffset, JtextArea#getLineEndOffset should allow you to quickly extract a certain line from the text. Or as tskuzzy already suggested, retrieve the complete text and split it yourself
How to get line 1 , 2 or .. etc?
Get the text from JTextArea / JTextPane by JTextArea.getText() / JTextPane.getText(). Once you have the text as string you can get the different lines by splitting the text with the new line character as a separator.
JTextArea jText = new JTextArea("line1\nline2\nline3\nline4");
String temp = jText.getText();
String[] tempArr = temp.split("\n");
// Method to getText
public String getText(int lineNos){
return Str[lineNos].getText();
}
// Method to setText
public void setText(int lineNos){
Str[lineNos].setText("Hello"); // Can also use Scanner here
}

JTextfield, how to verify content in the getText() method

I have a textfield called x.
When the textfield contains " ", I want to do something. If it does not, do something else.
I tried doing
String test = x.getText();
if(test.startsWith(" ")){buttonN.setForeground(Color.GRAY));}
else{buttonN.setForeground(Color.BLACK));}
but it didnt work. any suggestions
Why not use contains?:
if(x.getText().contains("\u0020"))
buttonN.setForeground(Color.GRAY);
else
buttonN.setForeground(Color.BLACK);
Although the aforementioned will work, it won't detect tabular spacing. That being said, I'd recommend using a regular expression instead:
if(Pattern.compile("\\s").matcher(x.getText()).find())
buttonN.setForeground(Color.GRAY);
else
buttonN.setForeground(Color.BLACK);
Reference.
If you just want to ensure if the text field is empty regardless of whether it contains space, tab, newline etc. use the following:
if(x.getText().trim().length() == 0){
buttonN.setForeground(Color.GRAY);
}else{
buttonN.setForeground(Color.BLACK);
}
The String.trim() removes any whitespace in the String.
The easiest solution for any verifycation of the getText() command is this:
If (field.getText().isEmpty()) {
buttonN.setForeground(Color.GRAY);
}
else {
buttonN.setForeground(Color.BLACK);
}
(Color.GRAY)) and (Color.BLACK)) end with 2 closing parenthesis, while only one was opened.
String test = x.getText();
if (test.startsWith (" "))
{
buttonN.setForeground (Color.GRAY);
}
else buttonN.setForeground (Color.BLACK);
Some spaces around parenthesis make the reading more convenient.

Categories