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
}
Related
I have a .txt file which contains following text:
111000111001
x00000010001
111110000001
I want to put this content into string so I use this method.
public void read() {
FileHandle file = Gdx.files.internal("map.txt");
String text = file.readString();
System.out.println(text.charAt(12));//Here is the problem,it's showing empty character instead of x
}
When I want to get the 12th element(x on 2nd line),it's impossible(I think there's a problem of passing to new line,but I don't know how to solve it).Can you help me please?
There's something called Carriage Return that makes extra characters appear at the end of each line (3 extra characters to be precise) when reading from a text file so to avoid getting those in your way you can use:
text = text.replaceAll("(?:\\n|\\r)", "");
And now when you try to print the 12th element you get the "x" you wanted
System.out.println(text.charAt(12)); // Prints x
Here's more info about the replaceAll() method:
Java Api: String.replaceAll()
Basically assume I have two text fields on my java applet:
[__________] [___________]
The first text field takes in user input and the second textbox prints that user input to store it as a list. So if the user inputted "cat" then "dog", then "frog", the second textfield should look like this:
[cat, dog, frog]. When the user types a word and clicks button 1 it should add the word into the second textfield. The code below is what I tried but nothing is happening:
textf = user input field
texty = output field
public void actionPerformed(ActionEvent e){
if (e.getSource() == b1 ){
x = textf.getText();
texty.add(x);
textf.setText(null);
}
Is this a Swing GUI? Are those JTextFields?
You need to look at the Java API since you're using an inappropriate method, add(...) in your code, and I'd be very surprised if your code with your use of the add method will even compile, since the add method is used for adding other GUI Components to the container that is calling the method, and that's not what you're trying to do.
You're trying to append text, and for that you will need to get the text from the 2nd text field, using getText() add the new String to this text using String concatenation (basically using the + operator, and then set the text of the 2nd text field with the new String using setText(...).
Myself, I'd display the accumulated texts in a JList or JTextArea and not a 2nd JTextField.
So if the user inputted "cat" then "dog", then "frog", the second textfield should look like this: [cat, dog, frog].
Then you need to insert the text into the Document of the text field:
So assuming you create the second JTextField with code like:
JTextField textField2 = new JTextField("[]");
You need to insert text before the "]". So you would insert the text into the Document of the second text field:
String text = textField1.getText() + ",";
Document doc = textField2.getDocument();
doc.insertString(text, doc.getLength() - 1, null);
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.
Well, I'm trying to replace a word by using contains() Method:
String z = tfB.getText().toString();
String show = textPane.getText().toString();
if(show.contains(z)){
// how I specify the word that were found and change it without
effecting anything with in that line
}
well what I main by that:
What I'm trying to do is get the value from the user.
then search if it found replace it with something. For example:
String x = "one two three four five";
It should set the textPane to "one two 3 four five"
or
"one two 3-three-3 four five"
could any one please tell me how to do it.
Thank you
What I'm trying to do is get the value from the user. then search if it found replace it with something.
Don't use the contains() method because you will need to search the text twice:
once to see if the text is found in the string
again to replace the text with a new string.
Instead, use the String.indexof(...) method. It will return the index of the text IF it is found in the String.
Then you should replace the text directly in the Document of the text pane, not in the String itself. So the code would be something like:
int length = textPane.getDocument().getLength();
String text = textPane.getDocument().getText(0, length);
String search = "abc...";
int offset = text.indexOf(search);
if (offset != -1)
{
textPane.setSelectionStart(offset);
textPane.setSelectionEnd(offset + search.length();
textPane.replaceSelection("123...");
}
Also, not that you get the text from the Document, not the text pane. This is to make sure the offsets are correct when you replace the text in the Document. Check out Text and New Lines for more information on why this is important.
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 = " ";
}