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.
Related
Basically, I have a java program that acts like a media player, PLAY PAUSE FAST FORWARD options etc.
Which also have keybindings.
However, in another JPanel I also have a JTextField that allows the user to enter some text to act as commentary.
If the user decides to write some text, then this JTextField is focused and they key bindings don't work anymore.
But if I did setFocusable(false) to all containers and child containers, the user won't be able to enter text into the JTextfield.
How do I solve this?
JComponent#getInputMap uses WHEN_FOCUSED by default.
Depending on your needs you can use WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or WHEN_IN_FOCUSED_WINDOW to change the level of focus you component needs in order to respond to key input
See JComponent#getInputMap(int) and How to Use Key Bindings for more details
I'm new to Java and I started a little RPG game. When a battle starts, I would like to display the battle messages in a little box. I would like the box to be scrolled automatically, displaying the new message every time, and not losing the old messages.
I am going to give you the information you need, but it is your duty to figure out how to use them:
1-you should use a JTextArea to display your messages.
2-when a new message come, use the append() function on you JTextArea object (use \n to return automatically in line).
3-add a JScrollPane to your JTextArea so it can be scrollable.
4-update you caret automatically to always show the last message printed use this where myJTA is your JTextArea:
DefaultCaret caret = (DefaultCaret)myJTA.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
now you have all the pieces of the puzzle.
Good luck.
EDIT:
if you want your JTextArea not editable use:
myJTA.setEdtable(false);
I have a JTextField that a User and an Admin update to try and solve a problem that a user may have. Although when I setText(conversation) to the JTextField the text starts in the middle and when it goes to the end of a line it does not go to a different line. Can I set the JTextField so that text begins at the top? and when the user/admin gets to the side of the text field it goes to another line? I have tried looking this up everywhere and can not find an answer
here is a picture of the window with the conversation text field
Just noticed that you are using a JTextField. JTextField is a single line and will hence print the text that appears to be center aligned. You should be using a JTextArea instead that that is multi-lineand it will solve your problem
I'm working on a Swing application that uses the default Swing methods for handling focus. Focus isn't working as I'd expect.
In one case, I have a JTextField that I call .requestFocusInWindow() When the window is displayed a JLabel has focus instead
The Java 6 docs for JLabel say "As a result, it cannot get the keyboard focus." http://docs.oracle.com/javase/6/docs/api/javax/swing/JLabel.html
However, I have a sample application that shows a JLabel receiving focus and KeyboardFocusManager.getFocusOwner() returns that component. (http://github.com/akinsgre/swingStarter)
The code the the class is https://raw.github.com/akinsgre/swingStarter/master/src/main/java/test/HelloWorldSwing.java
Can anyone help me understand or explain what I'm missing?
I think you need to associate the label with the text field. So try using the setLabelFor method and see if that helps.
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.