I'm trying to update a JTextArea using the .append. I'm sending in a string to the method from another class and I know the textBox method gets the string as I can use .println to test it. The only thing is it does not update the JTextArea which is strange as when I first start the program and the gui is being created i'm able to update it.
public void textBox (String text){
textArea.append(text);
}
Does anyone have any ideas? Many thanks in advance.
Try using textArea.append(text + "\n");
I too had the same problem . I solved it by adding a "\n" at the end
JTextArea textArea = new JTextArea(text);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
String appendText = "jumps over the lazy dog.";
textArea.append(appendText);
Related
I'm making a java Text Editior and I can't seems to know how to insert a line of text that is "[code][/code]" Here is what I'm trying to program. The method for the inserting is called "insert". So it has to be something that is insert,(something that inserts strings of text in JTextArea)
/////////////////// CODE //////////////////////////////////////////////////////////////////////////////////////////////////
this.insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
/////////////// END OF CODE ///////////////////////////////////////////////////////////////////
Simple example to set/assign text to JTextArea .. this is not the solution but it will help you...
JTextArea textArea = new JTextArea(
"This is an editable JTextArea. " +
"A text area is a \"plain\" text component, " +
"which means that although it can display text " +
"in any font, all of the text is in the same font."
);
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
Although to set the text .. use this method
void insert(String str, int pos)
Inserts the specified text at the specified position.
public void setText(String t)
Sets the text of JTextArea
For refrerence and help please follow jtextareaguide
Link to video tutorial
Guide for Simple Editor in Java
Java already provides a method for inserting text in the JTextArea class. Try this...
JTextArea t = new JTextArea();
t.setText("specified string");
t.append("+ added string");
use:
JTextArea text=new JTextArea();
text.setText("Message..");
Here is a doc.
public class JTextArea extends JTextComponent
A JTextArea is a multi-line area that displays plain text. It is intended to be a lightweight component that provides source compatibility with the java.awt.TextArea class where it can reasonably do so. You can find information and examples of using all the text components in Using Text Components, a section in The Java Tutorial.
Try this:
JTextArea textj1 = new JTextArea();
textj1.setText(textj1.getText().trim() + "a string or even an arraylist");
Better using:
textArea.setText(textArea.getText()+" Message");
I'm having trouble trying to get a linebreak included in a Stringbuilder to appear in a JLabel.
I've found a couple of similar problems solved here, e.g. [here] Problems to linebreak with an int in JLabel and [here] How do I append a newline character for all lines except the last one? , along with a few others but none of them seem to work for me.
When printing to System.out, it works fine and I get a new line each time but when passed to a JLabel, everything appears as one line.
Here's the current version of the code, which has attempted to append a newline character, as well as including one in the main declaration. Neither has any effect when passed to the JLabel:
public void layoutCenter() {
JPanel central = new JPanel();
add(central, BorderLayout.CENTER);
JTabbedPane tabs = new JTabbedPane();
this.add(tabs);
// memory tab
StringBuilder mList = new StringBuilder();
memLocList = new Memory[MEM_LOCATIONS]; //Memory is a separate class
for (int i = 0; i < memLocList.length; i++) {
mList.append("\n");
memLocList[i] = null;
mList.append("Memory location: " + i + " " + memLocList[i] + "\n");
}
System.out.println(mList.toString());
JComponent memTab = makeTextPanel(mList.toString());
tabs.addTab("Memory", memTab);
}
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
panel.add(filler);
return panel;
}
I've also tried using System.getProperty(line.separator) with similar results and can't think of anything else to try so thought I'd appeal for help here.
Thanks,
Robert.
-EDIT-
Thanks to mKorbel, changing the JLabel to a JTextPane solved it.
The two lines in question are:
JTextPane filler = new JTextPane();
filler.setText(text);
Thanks again to everyone.
JLabel isn't designated to held multilines document, there are two choices (by accepting newline or tab by default)
if document could not be decorated or styled somehow then to use JTextArea
in the case document could be decorated or styled somehow then to use JEditorPane or JTextPane
You're going to have to use <html> and <br> to get line breaks in a JLabel Swing component.
If you absolutely must use JLabel, then I suggest using one for each line.
You can make a JLabel have mulitple lines by wrapping the text in HTML tags and using br tags to add a new line.
If you news auto wrapping I suggest using a JTexrArea. You can make it uneditable and style it so it looks like a label.
You can look at this tutorial:
http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
One of the example is using html to make it two lines for a JButton text. It should be very similar.
I have a Swing Application where a user has to Input Some information. I need the cursor By default to be at Position 10 of the JtextField: I have tried these Two Methods but none of them has worked for me:
JTextField text = new JTextField(" ", 50);
text.setHorizontalAlignment(10)
The other one I have tried is
JTextField text = new JTextField(" ", 50);
text.setCaretPosition(10)
Is there really a way to do what am trying?
Try this:
text.getCaret().setDot(10);
Doesn't the problem come from your JTextField containing an empty String ?
If you want the cursor to be at a set position, this position should be reachable, i.e having a String containing 10 blank spaces.
PS : I think setCaretPosition is the right method here.
I am using this code to show text in a JTextArea:
jTextArea1.setText(jTextArea1.getText() + text + "\r\n");
jTextArea1.repaint();
But it shows an exception:
java.lang.NullPointerException
You never instantiated your JTextArea. Also, you might want to check out JTextArea#append.
jTextArea1.setText(jTextArea1.getText() + text + "\r\n");
StringBuilder sb = new StringBuilder();
if(jTextArea1.getText().trim().length() > 0){
sb.append(jTextArea1.getText().trim());
}
sb.append(text).append("\r\n");
jTextArea1.setText(sb.toString());
Above two friends gave you answer.
I want to explain it. Because first times I also met that problem. I solved that, but today solve as above code snippet.
As Jeffrey pointed out, you have to create an object instance before you call non-static methods on it. Otherwise, you will get a NullPointerException. Also note that appending a text to a JTextArea can be done easily by calling its JTextArea.append(String) method. See the following example code for more detail.
package test;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
Main m = new Main();
m.start();
}
private void start() {
JTextArea ta = new JTextArea();
ta.append("1\n");
ta.append("2\n");
ta.append("3\n");
JFrame f = new JFrame();
f.setSize(320, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(ta);
f.setVisible(true);
}
}
The following code adds text to the text area. Note that the text system uses the '\n' character internally to represent newlines; for details, see the API documentation for DefaultEditorKit.
private final static String newline = "\n";
...
textArea.append(text + newline);
Source
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiline text in JLabel
I want to do this:
JLabel myLabel = new JLabel();
myLabel.setText("This is\na multi-line string");
Currently this results in a label that displays
This isa multi-line string
I want it to do this instead:
This is
a multi-line string
Any suggestions?
Thank you
EDIT: Implemented solution
In body of method:
myLabel.setText(convertToMultiline("This is\na multi-line string"));
Helper method:
public static String convertToMultiline(String orig)
{
return "<html>" + orig.replaceAll("\n", "<br>");
}
You can use HTML in JLabels. To use it, your text has to start with <html>.
Set your text to "<html>This is<br>a multi-line string" and it should work.
See Swing Tutorial: JLabel and Multiline label (HTML) for more information.
public class JMultilineLabel extends JTextArea{
private static final long serialVersionUID = 1L;
public JMultilineLabel(String text){
super(text); // According to Mr. Polywhirl, this might improve it -> text + System.lineSeparator()
setEditable(false);
setCursor(null);
setOpaque(false);
setFocusable(false);
setFont(UIManager.getFont("Label.font"));
setWrapStyleWord(true);
setLineWrap(true);
//According to Mariana this might improve it
setBorder(new EmptyBorder(5, 5, 5, 5));
setAlignmentY(JLabel.CENTER_ALIGNMENT);
}
}
It totally looks the same for me, but its ugly
Another easy way (but changes the text style a bit) is to use a <pre></pre> html block.
This will persist any formatting the user entered if the string you are using came from a user input box.
Example:
JLabel label = new JLabel("<html><pre>First Line\nSecond Line</pre></html>");
The direct procedure of writing a multi-line text in a jlabel is:
JLabel label = new JLabel("<html>First Line<br>Second Line</html>");
The problem with using html in JLabel or any Swing component is that you then have to style it as html, not with the usual setFont, setForeground, etc. If you're ok with that, fine.
Otherwise you can use something like MultilineLabel from JIDE, which extends JTextArea. It's part of their open source Commom Layer.
JLabel can accept html code. Maybe you can try to use the <br> tag.
Example:
JLabel myLabel = new JLabel();
myLabel.setText("<html> This is a <br> multi-line string </html>");
http://java.sun.com/docs/books/tutorial/uiswing/components/html.html