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
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 would like to show a text like "write your message here." that automatically disappears after the textbox gets the focus?
I was trying to do this without having to deal with listeners, with an initial text for example.
Try the example. I think that's what you need
http://tips4java.wordpress.com/2009/11/29/text-prompt/
SwingX contains a class "PromptSupport" that does exactly what you need.
It's very easy to handle:
JTextField tf = new JTextField(5);
PromptSupport.setPrompt("A Prompt", tf);
Take a look here:
http://weblogs.java.net/blog/kschaefe/archive/2010/07/15/swingx-using-promptsupport
You can download the jar-file here:
http://java.net/downloads/swingx/releases/
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.
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.