I was wondering if it is possible to write into a JtextArea (java swing) and then catch this String like in a JTextField ?
Yes it is. As you can see on the Java APIs both components are subclasses of JTextComponent, and thus inherit getText() and setText() methods.
JTextField can be seen as a "simpler" JTextArea, since it behaves very similarly but only has one line of text and no scrolling.
Related
I'm using java swing to create my GUI and using the scanner class to get the information inputted from the JTextFields across to the server. is this possible and if so how?
No. There is no console, so don't use Scanner. Instead get text when you need it by using TextField's getText() method.
That isn't how Swing works. Scanner is only for command-line input. If you have a JTextField, just call the .getText() method on it.
JTextField myField = new JTextField();
...
String currentText = myField.getText();
Swing is event-based. You probably want to have a JButton and have that JButton cause the text to be submitted to the server when it is clicked. For that, you'll need an ActionListener. See the tutorial below for more information:
http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html
I am writting a simple server chat client using gui to make the chat box and so on. I am simply wondering if I should use jtextarea instead of jtextfield if someone wants to write a long message since jtextfield does not allow word wrap.
Does it matter if I use jtextarea of jtextfield or is there a specific reason not to use jtextarea as my input box?
My guess:
You'll probably need both a JTextField and a JTextArea (or other multi-line text component).
The JTextField would be for the user to type in their chat messages to be sent. It would be editable.
and the JTextArea would show the incoming chats as well as the chats the user has sent. It would not be editable.
Consider placing the JTextArea in a JScrollPane
Consider placing both in a BorderLayout-using container with the JScrollPane that holds the JTextArea placed in the CENTER position and the JTextField in the SOUTH position.
A JTextField is a little easier to work with. You can add an ActionListener to it so that when the user hits enter the Action can be invoked an the message will be sent. Of course you can still have a "Send" JButton. the user can click on as well.
With a JTextArea, the Enter key will add a new line, so it you want the user to be able to sent the text when Enter is used you will need to customize the processing, maybe by using Key Bindings. I would prefer the text area because I like to see as much of the text as possible, like I am doing now as I repond to this question.
From oracle docs and personal experience, the JTextField makes life a lot easier as the ActionEventListener is set by default so the actions(characters or text) set in the field are copied to the JTextArea which is like a log of activity carried out in the field.
So the ball is in your court. You can use it JTextField together with JTextArea or JTextArea alone and write the listener class to suit you purpose and certainerly added a listenable event e.g a button.
Besides, always specify the size of the field to avoid layout issues that may compromise the Ui of your application as there is always a dynamic resizing if the size is not specified out rightly.
I'm writing a custom control, based on JTextField. My JTextField uses my own Document class, derived from PlainDocument, so that I can process all the user input in overriden insertString(...) and remove(...) methods.
Here's the problem. After I process user input, sometimes I want to move the caret to another position. What is the better way to do it?
By default Document puts the caret next to last input. So I tried to put a char to my target position and delete it immediately. For some reason it doesn't work in remove() method... and the code doesn't look nice :)
Thanks for and proposals.
It seems unnecessary to extend PlainDocument. Simply add a DocumentListener to the Document of your JTextField and you can process the user input in the 3 methods declared in DocumentListener
Use setCaretPosition to move the caret to wherever you would like
You should actually be using a DocumentFilter if you want to control user input. A DocumentFilter allows you to intercept all the input as it happens. You can then use JTextField.setCaretPosition (comes from JTextComponent) to set the caret position. Just pass your DocumentFilter implementation a reference to the JTextField so it can set the caret position for you.
Here's the Java trail for DocumentFilter. Also, an example on JavaRanch.
I need to pass a text from a JTextField in a JFrame to a second JTextField in another JFrame. GUI has been created using GUI Editor netbeans 6.9.
Don't use a GUI Editor, especially if you are just learning Swing.
You need a reference saved somewhere to both fields so that you getText() of the first JTextField and setText() on the second field.
Use textField.getText to get the text from a textfield and anotherTextField.setText to set the text of the other textfield.
If your JFrames are in two different classes you may have to expose methods in those classes to get and set the text between them.
You should bind your model to both of the views (JTextFields) instead of copying Text from one to another.
Read http://www.martinfowler.com/eaaDev/OrganizingPresentations.html for more information.
I want to load up a window using the java swing, something like this
However I want to be able to use it for text input also not just to dump data into it..
any idea how to do it?
JTextAreas are editable by default, so input is trivial. Just put one into a test UI and see for yourself.
From a design perspective, using the same JTextArea for both input and output strikes me as unwise. The only example I can think of is a shell interface, and that imposes stronger limits on how input and output works than a JTextArea will provide out of the box.
I'm not sure if I got the point of your question, but you can get the text the user has typed in using the getText() method:
JTextArea ta = new JTextArea();
String input = ta.getText();
Check out this Sun toturial:
http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html
In particular, scroll down to the "TextAreaDemo" example.