how do I make a text box which the user can insert text into, then that text can be saved to some variable?
JTextField is probably the class you are looking for.
JTextField textField = new JTextField();
yourPanel.add(textField);
This will add the textField into your JPanel. Then at any point in your code where you have a handle to your textField, call getText(); of your JTextField.
String s = textField.getText();
See this tutorial for a better reference:
http://download.oracle.com/javase/tutorial/uiswing/index.html
A JTextField or JTextArea will do what you are asking for, but you'll need either a button or a listener to actually know when to save this to a String.
javax.swing is Event based, which means that you cannot extract the text like this:
JTextField myField = new JTextField();
//wait for user input
String s = myField.getText(); //not guaranteed to work!
Instead, you may want to make a "Submit" button that will send the text to your program when it is clicked:
http://download.oracle.com/javase/tutorial/uiswing/components/button.html
Related
How to setLineWrap, I'm according to https://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html#setLineWrap%28boolean%29
but how I can setLineWrap to jlabel, I have something like this:
String a = "text (...)";
JLabel label = new JLabel(a);
but my text is leaving
I mean:
JLabel:
aaaaaaaaaaaaaaaaaaaaaaaxxxxxxx where a is text and x is text that disappeared
JTextArea:
aaaaaaaaaaaaaaaaaaaaaaa
aaaaaaa
There is no setLineWrap method in JLabel. But if you set HTML to the JLabel you can overcome this.
JLabel l = new JLabel("<html><p>line 1</p><p>line 2</p></html>");
You can actually use a JTextField and make it readonly to look like a Label. When you make the text field readonly, the long text can be scrollable using keyboard.
JTextField txtLabel = new JTextField();
txtLabel.setEditable(false)
txtLabel.setText("aaaaaaaaaaaaaaaaaaaaaaaxxxxxxx");
If you want the text to wrap you may want to use the JTextArea by making it readonly with a label look and feel.
I'm trying to make a text area record that every time the button is press, it means that it has been recorded and should be showing record1, record2, record3, etc. on it.
My goal is that, every button is pressed it will add text to the text area with different text label so that no redundancy.
I tried it with my own with this:
private void btnReqstRefreshActionPerformed(java.awt.event.ActionEvent evt) {
JLabel labelthis = new JLabel("record1");
label.setSize(label.getPreferredSize());
TextArea1.add(label);
TextArea1.revalidate();
TextArea1.repaint();
}
I know it is wrong, but is it possible?
text area is like a mini text editor - you add text to it not other components. Instead of adding labels - just add the text. Something like:
TextArea1.setText(TextArea1.getText() + "record1")
This should append record1 to the existing text in the text area.
According to my experience this is possible.
`private void btnReqstRefreshActionPerformed(java.awt.event.ActionEvent evt) {
i++;//i class level variable(static) to avoid redundancy
//labelThis initialized earlier should be accessible here
String oldText = labelThis.getText().toString();
oldText += "record "+i;
labelThis.setSize(labelThis.getPreferredSize());
TextArea1.add(labelThis);
TextArea1.revalidate();
TextArea1.repaint();
}`
I am trying to take the input string from a JTextArea and display it back to the user by putting it back in the JTextArea, so the finished product should be two identical copies of the string that the user has inputed. The program goes as far as to ask the question but it doesnt seem to either take the input or use the input to output it to the JTextArea. It would be a real help if somebody would help and direct me. ;)
static JFrame jf;
static JTextArea jtf;
public static void main(String[] args) {
jtf = new JTextArea();
jf = new JFrame();
jf.setVisible(true);
jf.setResizable(true);
jf.setSize(400,400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(jtf);
playerchoice = jtf.getText();
jtf.setText(playerchoice);
}
You are drawing the value of jtf in the same text area without waiting for the user to input anything.
You need to add a key listener to the text area (listening for the enter key for example) or a button to trigger the action you want to perform (copy the text in your case)
I think I know the answer. You're copying the text from a JTextArea, and putting it back in there. I don't see any point in doing that. Maybe make another JTextArea and display it there?
I have Jtextfield in one form and I want to get the value that I typed and put it in Jlabel in the other form? How can I do that?
To get the text:
String text = textField.getText();
And then set it on your label with
label.setText(text);
I am looking for a way to put example text into a swing JTextField and have it grayed out. The example text should then disappear as soon as any thing is entered into that text field. Some what similar to what stackoverflow does when a user is posting a question with the title field.
I would like it if it was already a extended implementation of JTextField so that I can just drop it in as a simple replacement. Anything from swingx would work. I guess if there is not an easy way to do this my option will probably be to override the paint method of JTextField do something that way maybe.
Thanks
The Text Prompt class provides the required functionality without using a custom JTextField.
It allows you to specify a prompt that is displayed when the text field is empty. As soon as you type text the prompt is removed.
The prompt is actually a JLabel so you can customize the font, style, colour, transparency etc..:
JTextField tf7 = new JTextField(10);
TextPrompt tp7 = new TextPrompt("First Name", tf7);
tp7.setForeground( Color.RED );
Some examples of customizing the look of the prompt:
If you can use external librairies, the Swing components from Jide software have what you are looking for; it's called LabeledTextField (javadoc) and it's part of the JIDE Common Layer (Open Source Project) - which is free. It's doing what mklhmnn suggested.
How about initialize the text field with default text and give it a focus listener such that when focus is gained, if the text .equals the default text, call selectAll() on the JTextField.
Rather than overriding, put a value in the field and add a KeyListener that would remove the value when a key stroke is registered. Maybe also have it change the foreground.
You could wrap this up into your own custom JTextField class that would take the default text in a constructor.
private JLabel l;
JPromptTextField(String prompt) {
l = new JLabel(prompt, SwingConstants.CENTER);
l.setForeground(Color.GRAY);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.getText().length() == 0) {
// Reshape the label if needed, then paint
final Rectangle mine = this.getBounds();
final Rectangle its = l.getBounds();
boolean resized = (mine.width != its.width) || (mine.height != its.height);
boolean moved = (mine.x != its.x) || (mine.y != its.y);
if (resized || moved)
l.setBounds(mine);
l.paint(g);
}
}
You can't do that with a plain text field, but you can put a disabled JLabel on top of the JTextField and hide it if the text field gets the focus.
Do it like this:
Define the string with the initial text you like and set up your TextField:
String initialText = "Enter your initial text here";
jTextField1.setText(initialText);
Add a Focus Listener to your TextField, which selects the entire contents of the TextField if it still has the initial value. Anything you may type in will replace the entire contents, since it is selected.
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
if (jTextField1.getText().equals(initialText)) {
jTextField1.selectAll();
}
}
});