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.
Related
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.
I am trying to create a text based adventure in Java I need help in combining a Scanner with JPanel so that the user can type into the panel directly rather than choosing from a list of options. Is there any way of doing this or would a dialogue be needed and if so is there a way of doing so within the JPanel?
For doing so, you need to add some components to your panel, such as, JtextField or JTextArea.
Then you have to add a action listener to your text field/area.
You can find a lot of advices with a simple google search.
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 have to create an xml file based on the value of attributes set by user in dialog of Java Swing.There are five attributes name,age,sex,date of birth and place. when the user clicks on OK button of the Dialog box an xml file should be created in the temp directory of the user.
Kindly help me as I am new to Java Swing
Regards,
Kumar
Create a GUI with controls to collect the data you specified. Create a class to generate the XML output. Call this class.
To build a Swing GUI, you start with a JFrame.
Within this JFrame, you have a master JPanel.
The attributes will each have JLabel components and JTextField components. You will use a layout manager to arrange the label and text field components.
You have a JButton so that the user can tell your program that she's entered all of the attributes.
You should be able to look up all these Swing components and build a GUI.
Then, as jzd said, you create a class to generate the XML output and call this class from the GUI.