Adding Characters to Comments
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Saturday, August 25, 2001 05:45 PM

Hi all...

I have various JButtons which once selected by the user append the users input to the TextArea.


What I wanted was to add the "" to my JTextArea.

I tried defining 2 String variables:
String a = " ' "
String b = " ' "
I tried concatenating these in order to append them, however the compiler does not recognise these as valid.


Any help much appreciated...

Re: Adding Characters to Comments

Posted By:   David_Cole  
Posted On:   Saturday, August 25, 2001 09:54 PM

Here is a simple example to demonstrate the above:



import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;

public class JTextAreaTest
extends JFrame implements ActionListener
{
private JButton theButton;
private JTextArea theTextArea;
private JTextField theTextField;

public JTextAreaTest()
{
super("The Frame");
theButton = new JButton("Add Text");
theButton.addActionListener(this);
theTextField = new JTextField(20);
theTextField.setActionCommand("TheTextField");
theTextField.addActionListener(this);
theTextArea = new JTextArea("",15,30);

this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(theButton,BorderLayout.SOUTH);
this.getContentPane().add(theTextField,BorderLayout.CENTER);
this.getContentPane().add(theTextArea,BorderLayout.NORTH);
this.pack();
this.show();
}

public void actionPerformed(ActionEvent ae)
{
String quote = """;
theTextArea.setText(theTextArea.getText()
+ quote + theTextField.getText() + quote );
}

public static void main(String[] args)
{
JTextAreaTest JTextAreaTest = new JTextAreaTest();
}
}

Re: Adding Characters to Comments

Posted By:   David_Cole  
Posted On:   Saturday, August 25, 2001 09:47 PM

To create a String with a quote you need to use the escape character


For example for a single quote:


String s = """;


And for a double quote:


String s = """";


Finally the same applies for an apostrophe:


String s = "\'";


Good Luck.

About | Sitemap | Contact