How to move next line in Label? - java

I have Label in SWT
The text is
"Doctor\ntest\ttest2"
I want to change it to
"Doctor
test test2"
menaing to change the \n to new line and \t to tab.
Can I do it in Label in SWT? Do I need to change the control ?

You can do this in the label control.
final Label label = new Label(composite, SWT.NULL );
label.setText( "Line one \n line two abc \t tabbed" );
label.pack();
Results in:
You can also do this by using a Text-Control and set it disabled.

Related

How do I set the colour, font and size of a prompt-text string?

I am trying to change the properties of a String made from TextField::setPromptText using JavaFX.
This code:
TextField shapeField = new TextField();
shapeField.setMaxWidth(100);
shapeField.setPromptText("Circle, Triangle, Hexagon");
TextField fillField = new TextField();
fillField.setMaxWidth(100);
fillField.setPromptText("Red, Green, Grey");
Currently sets the prompt text to this:
I want to be able to make the font smaller and change the colour. How would I do this?
For color you need:
-fx-prompt-text-fill: red;
and you can change font size with but you need to add an event to check if text is empty then use your prompt text size and when not the actual size of the text (edited):
-fx-font-size
for example:
tf.setStyle("-fx-prompt-text-fill: red;\n" + "-fx-font-size: 26pt;");

New Line in TextField

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

jlabel setLineWrap - outgoing text

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.

Java (JLabel spacing)

I want the JLabel to display "hi, my name is Bob"
However, when I coded:
JLabel.setText("hi, my name is Bob");
the spaces are "consumed" and the output will read
"hi, my name is bob"
Can anyone please help me on this?
Thanks in advance
Here are two ways that both have the same effect. One sets the font to MONOSPACED while the other marks the text as HTML preformatted (which also uses a monospaced font & preserves spaces and tabs).
String TEXT = "hi, my name is Bob";
// ...
JLabel l = new JLabel(TEXT);
l.setFont(new Font(Font.MONOSPACED, Font.PLAIN, l.getFont().getSize()));
ui.add(l);
ui.add(new JLabel("<html><body><pre>" + TEXT));
Having said that, I agree with #Madonah & #maraca that this is best handled in two labels, using layouts (borders and padding) to achieve the required result.

Java - Is it possible to have two components on the same side with GridLayout?

Is it possible to have the "Save note" button under the "Create note" button using GridLayout? If not, what layout should I be using? dataPanel contains both text fields and both labels. buttonPanel is the save note button and namePanel is the create note button.
container.add(dataPanel, BorderLayout.CENTER);
container.add(buttonPanel, BorderLayout.SOUTH);
container.add(namePanel, BorderLayout.EAST);
Thanks in advance!
Of course it is possible! Simply define a grid layout with 2 rows and 3 columns each:
- first column will contain the labels
- second column will contain the text boxes
- 3rd column will contain the buttons
Also, why are you using the BorderLayout constants when adding to the GridLayout? With GridLayout once you have decided on the rows/columns simply add them in the order you want them to be layed out in the "grid" from left to right, top to bottom. So in the above example you will do something like this:
JPanel p = new JPanel( new GridLayout(2,3) );
p.add( /* enter the desired note label */ );
p.add( /* note name here text box */ );
p.add( /* create note button */ );
p.add( /* enter a new note label */ );
p.add( /* note text here text box */ );
p.add( /* save note button */ );

Categories