New Line in TextField - java

I am trying to have some pre-defined text in my TextField, but I don't know how to add new lines in between the words. I tried \n, but it isn't working.
Code:
package view;
import javafx.scene.text.Text;
public class Overview extends VBox{
private TextField info;
public Overview() {
//Adds a little border around the app so the text field doesn't go to the edges
this.setPadding(new Insets(30, 30, 30, 30));
//----- Initialising text field --------
info = new TextField("Name:\n" + "PNumber:\n"+ "Email:\n"+ "Date:\n"+ "Course:\n"+ ""+ "Selected Modules:\n"+ "==============");
info.setAlignment(Pos.TOP_LEFT);
info.setMinHeight(850);
info.setEditable(false);
//------ Adding the text to the Text Field --------
//-------- Putting it into VBox so that it stretches with the screen ----------
VBox fit = new VBox(info);
this.getChildren().add(fit);
}
}
Does anyone know how to add a newline between each word?
I want there to be a separation between words and then it will automatically fill in the information later.

TextField is only for single lines, it will not render multiple lines as you want your code to achieve. so instead of using TextField use TextArea.

try to use the textarea instead of textfield. You can use the mulitple lines in textarea but textfield is a single line control

Related

How to make a textarea filled with text (label) every time the button pressed?

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();
}`

How to input text in a textarea in gwt

I am new to GWT and have made 3 textarea objects and have added them to a vertical panel, which is also added to my rootpanel. However, I cannot seem to input any text in these textareas. Any suggestions?
VerticalPanel panel = new VerticalPanel();
TextArea tb = new TextArea();
TextArea tb1 = new TextArea();
TextArea tb2 = new TextArea();
panel.add(tb);
panel.add(tb1);
panel.add(tb2);
RootPanel.get().add(panel);
I would try enabling them:
tb.setEnabled(true)
tb1.setEnabled(true)
tb2.setEnabled(true)
But I don't think that should be necessary.
There might be something small you are missing, I would compare all of your code to this. It seems to be a good working example that you could compare your code to and see if you missed a small step.
It seems you may need to add the TextArea objects to horizontal panels and then add those horizontal panels to the vertical panel.
The problem you describe maybe caused by adding another widget on top of your TextArea widgets. In this case TextArea widget may remain visible, but it will be unusable.
I don't see it in the code snippet that you provided, but maybe it's not all of your code.
Try this. It is the example straight from the GWT Javadoc.
Maybe you need to use setCharacterWidth(int size) and setVisibleLines(int size) before adding it.
public class TextBoxExample implements EntryPoint {
public void onModuleLoad() {
//Make an 80 x 50 TextArea
TextArea ta = new TextArea();
ta.setCharacterWidth(80);
ta.setVisibleLines(50);
// Add them to the root panel.
VerticalPanel panel = new VerticalPanel();
panel.add(ta);
RootPanel.get().add(panel);
}
}

How to avoid that multiline text is clipped in javafx?

I have a GridPane which I'm filling with various graphical/textual elements.
For the text, single line labels gets the right size. The same happens to e.g. images
of various sizes (the grid is stretched to give space for the image).
However, for multiline text elements (a.e. a label containing text with newlines in it), it clips the element at one line height... How can I force an UI element (like a label) to take up enough space to display its content?
Here's some code (scala):
val chatPanel = new GridPane {
setFitToWidth(true)
setFitToHeight(true)
setManaged(true)
setMaxWidth(10000)
setMaxHeight(10000)
}
def sendTextInfoBlock(title:String,message:String) {
val button = new Label(message) {
// setWrapText(true)
// setMinHeight(100) <- this works, but of course doesn't match the required height
}
// val button = new Button(message)
chatPanel.add(button,1,row)
the message is a text with newlines, like "this is a long\nand interresting\nmessage"
I gave up, stacked a VBox full of labels (one label per line of text), and just styled it
like a button. That worked well at least.

using LayoutClickListener to terminary replace components

I have grid layout witch some fields added like that:
private Component userDetailsTab(final User user) {
final GridLayout details = new GridLayout(2, 1);
details.setMargin(true);
details.setSpacing(true);
details.addComponent(createDetailLabel(Messages.User_Name));
final Component username = createDetailValue(user.getName());
details.addComponent(username);
...
I have also Layout click listener which replace labels on text field, it looks like that:
final TextField tf = new TextField();
details.addListener(new LayoutClickListener() {
private static final long serialVersionUID = -7374243623325736476L;
#Override
public void layoutClick(LayoutClickEvent event) {
Component com = event.getChildComponent();
if (event.getChildComponent() instanceof Label) {
Label label = (Label)event.getChildComponent();
details.replaceComponent(com, tf);
tf.setValue(label.getValue());
}
}
});
In future I want to enable click on label, edit it and write changes to database after clicking somewhere else (on different label for example).
Now when I click on 1st label and then on 2nd label, effect is: 1st has value of 2nd and 2nd is text field witch value of 2nd. Why it's going that way? What should i do to after clicking on 1st and then 2nd get 1st label witch value of 1st?
You don't need to swap between Labels and TextFields, you can just use a TextField and style it look like a Label when it's not focused.
When I tried to create click-to-edit labels, it created a ton of extra work for me. I'd discourage it (and do as Patton suggests in the comments).
However, if you're going to insist on trying to create in-place editing, you will want to do the following:
Create a new class that extends a layout (e.g. HorizontalLayout), which can swap out a label for a text field
use LayoutClickListener to removeComponent(myLabel) and addComponent(myTextField)
use BlurListener to swap back to the label
use ValueChangeListener on the text field to copy its value to the label
This is a still a bad idea because:
Users cannot see affordances as easily (they can't tell what's editable)
Users cannot use the keyboard to tab to the field they want to edit
It adds unncessary complexity (maintenance time, etc).
I would recommend, if you want in-place editing, just show the text field, and save the new value with the BlurListener.

Dynamic text box in Java

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

Categories