I am having an issue with putting a JTextArea on a popup panel. When a button is pressed, a popup should appear with a JTextArea on it. This area should be editable (e.g the user should be able to input text into it). There is a button on the pop up which, when pressed, should make the popup disapear (and will ultimately process the text input by the user).
However I have the problem that the JTextArea is not editable-although I have set it to Edtitable,
|
Code:
|
textArea.setEditable(true); |
and a test in my code states that it is!
|
Code:
|
if (textArea.isEditable()) {
System.out.println("text area is editable");
} |
I have used JTextArea before, and I don't usually have a problem making it editable-but there seems to be some issue when it is placed on a pop up.
This is a for a project where I am creating a messaging application. My idea is that the user will press a button to start composing a message. The popup window with the JTextArea will appear and he will input his message. He will then press a send button which will make the window disappear and send the message.
If anyone can point out where I'm going wrong-or else suggest a better way of doing this (perhaps a different set of components to use), I will be very grateful.
thanks in advance.
Here is the full code of my program.
|
Code:
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextAreaTest extends JPanel implements ActionListener {
protected JTextArea textArea;
protected JButton myButton;
protected Popup popup;
protected JButton anotherButton;
protected JPanel myPanel;
private final static String newline = "\n";
public TextAreaTest() {
super(new GridBagLayout());
myButton = new JButton("press this"); //popup window appears when this is pressed
myButton.setActionCommand("myButton");
myButton.addActionListener(this);
add(myButton);
anotherButton = new JButton("anotherButton"); //button on popup. closes popup when pressed
anotherButton.setActionCommand("anotherButton");
anotherButton.addActionListener(this);
textArea = new JTextArea(5, 20);
textArea.setEditable(true); //note: set to true!
textArea.append("this is the text Area");
JScrollPane scrollPane = new JScrollPane(textArea);
myPanel = new JPanel(); //this is the popup panel
myPanel.add(anotherButton);
myPanel.add(scrollPane);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals("myButton")) {
PopupFactory factory = PopupFactory.getSharedInstance();
popup = factory.getPopup(null, myPanel, 0, 0);
popup.show();
if (textArea.isEditable()) {
System.out.println("text area is editable");
}
} else if (command.equals("anotherButton")) {
System.out.println("another Button pressed");
String text = textArea.getText();
System.out.println(text);
popup.hide();
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new TextAreaTest());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} |