Print and count each character of a string in the JTextArea [duplicate] - java

I have a text area with some text in it and I want to add some lines to it again, (the first lines + the other lines that I want to add) but it doesn't work.
The way I'm doing it right now erases the old text and shows just the new lines.

Instead of using JTextArea.setText(String text), use JTextArea.append(String text).
Appends the given text to the end of the document. Does nothing if the model is null or the string is null or empty.
This will add text on to the end of your JTextArea.
Another option would be to use getText() to get the text from the JTextArea, then manipulate the String (add or remove or change the String), then use setText(String text) to set the text of the JTextArea to be the new String.

Are you using JTextArea's append(String) method to add additional text?
JTextArea txtArea = new JTextArea("Hello, World\n", 20, 20);
txtArea.append("Goodbye Cruel World\n");

When you want to create a new line or wrap in your TextArea you have to add \n (newline) after the text.
TextArea t = new TextArea();
t.setText("insert text when you want a new line add \nThen more text....);
setBounds();
setFont();
add(t);
This is the only way I was able to do it, maybe there is a simpler way but I havent discovered that yet.

Related

Java swing add text on text area [duplicate]

I have a text area with some text in it and I want to add some lines to it again, (the first lines + the other lines that I want to add) but it doesn't work.
The way I'm doing it right now erases the old text and shows just the new lines.
Instead of using JTextArea.setText(String text), use JTextArea.append(String text).
Appends the given text to the end of the document. Does nothing if the model is null or the string is null or empty.
This will add text on to the end of your JTextArea.
Another option would be to use getText() to get the text from the JTextArea, then manipulate the String (add or remove or change the String), then use setText(String text) to set the text of the JTextArea to be the new String.
Are you using JTextArea's append(String) method to add additional text?
JTextArea txtArea = new JTextArea("Hello, World\n", 20, 20);
txtArea.append("Goodbye Cruel World\n");
When you want to create a new line or wrap in your TextArea you have to add \n (newline) after the text.
TextArea t = new TextArea();
t.setText("insert text when you want a new line add \nThen more text....);
setBounds();
setFont();
add(t);
This is the only way I was able to do it, maybe there is a simpler way but I havent discovered that yet.

How can I create a new line in JTextField to type in?

I want to make a Swing program that writes updates on what you have done. It writes all the information on an uneditable JTextField.
For example:
You have changed the text to BLUE.
You have changed the text to RED.
The problem is that I can't make the two lines separate.
What I get is this:
You have changed the text to BLUE. You have changed the text to RED.
What I've tried is this: (This does not work)
TF_BetInfo.setText(TF_BetInfo.getText() + "\nYou have changed the text to BLUE.");
TF_BetInfo.setText(TF_BetInfo.getText() + "\nYou have changed the text to RED.");
you can't have multiple lines with JTextField , you can use JTextArea for that purpose , and the code wouldn't be much different too , you can still use the same code or you can just
TF_BetInfo.append("\nyou have ...... ");
where TF_Betinfo is a JTextArea rather than a JTextField .
You can't use JTextField for this. JTextField is sinle-line edit control. That is why you got all your output in one line.
If you want several lines to be printed in edit use JTextArea. In your case you can use jTextArea.append(string) (note: jTextArea is an object of class JTextArea, and string is an object of class String).
You can't actually use several lines in a JTextField, but what you can do, is using a JTextArea of the wanted size, and then use the following:
yourJTextArea.setLineWrap(true);
This will automatically detect when the JTextArea needs to use another line and add it, pretty cool, useful and you only need one code line to do it.
JTextArea is more flexible but if you really want flexibility read about JTextPane or JEditorPane, you can show URLS, internet pages and everything that comes to your mind.

Multiline Text output in javafx

I need to output 5-8 lines to my window. I managed that with a TextArea, BUT, i don't want the user to be able to write inside it, AND I also can't use my keyboard to navigate between buttons and other elements on the page.
Lets say that there is a String named text, which field type do you think i should write it out into? Text is good but only lets me use a single line.
Text actiontarget3;
String text = new String(" something \n Second line something");
actiontarget4.setText(text);
Because I use FXML to design the menu page, the relevant part is:
<Text fx:id="actiontarget3"
GridPane.columnIndex="1" GridPane.rowIndex="1"/>
Basically, I managed to get the info from a ping test, added it to an ArrayList<String>
and I want to display the results on the Graphic User Interface. But the above example covers my problems without having to copy paste here the entire code.
You can make it so a TextArea is not editable.
JTextArea ta = new JTextArea();
ta.setEditable(false);
You may also want to look into using a JTextPane.

Confusion regarding JTextArea and newline character

I'm a bit confused on how the JTextArea works with regards to newline characters. I've got a JTextArea within a JScrollPane object.
JScrollPane scrollPane4 = new JScrollPane();
scrollPane4.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane4.setBounds(66, 155, 474, 133);
getContentPane().add(scrollPane4);
postingArea = new JTextArea();
postingArea.setMaximumSize(new Dimension(470, 200));
postingArea.setLineWrap(true);
postingArea.setBorder(border);
postingArea.setLineWrap(true);
postingArea.setWrapStyleWord(true);
scrollPane4.setViewportView(postingArea);
The text area is used to gather input from the user and post to an SQL database. The string can be read from the database and redisplayed on a separate web page at another time. When a user enters text, the line does wrap, however, what gets entered into the database is one long string. Therefore, what is redisplayed is one long string with a horizontal scroll bar. Is there a way for me to add newline characters at the appropriate location in the text via an event handler? Or, do I simply inform the user to press "Enter" when they want a new line?
I don't see where your TextArea is being added within your ScrollPane. If it were truly being added, the code would have looked like:
JScrollPane scrollPane4 = new JScrollPane(postingArea);
And make your TextArea as:
JTextArea postingArea = new JTextArea(ROWS, COLUMNS);
Where ROWS and COLUMNS are integers specifying the number of rows and columns in the TextArea.
And don't use setBounds() method, but rely on the LayoutManager to do its work as said by #nachokk. Its not a preference, but good practice. Also, since the LayoutManager will do its work, regardless of you setting your component's bounds, if you LayoutManager doesn't consider the custom bounds parameters, it will calculate it on its own, and that's why you are having the issue.
The rest of your code looks fine.
I have resolved this by using the Utilities class. In particular, i called getRowEnd () in a loop to determine the logical end of the line (eg. the point of the word wrap) in the JTextArea. I then inserted the string <br> at that point. The updated string was then inserted into an SQL database and later retrieved and displayed on another page. The text displayed correctly with proper line breaks.

In java, how do I add line breaks to a JButton's tooltip?

I have a JButton that uses setToolTipText(String str) and I have a few lines of writing in the tooltip that need to to be separated by line breaks. I tried concatenating + "/n" at the end of each line of text right before I wanted the line break to go, but it did not do anything. JToolTip text doesn't have a constructor with a JTextArea and I couldn't see anything in JTextArea that allowed it to convert to a String. Anyone have any ideas?
Also, how do I get a JToolTip to last longer than the default 3 seconds?
Thank you for your time.
Use HTML.
Set the tooltip to "<html>My multi-line<br>tooltip.</html>"

Categories