How To Change Label Text On Button Click Javafx
JavaFX | Label
Characterization is a function of JavaFX parcel . Label is used to brandish a short text or an image, information technology is a not-editable text control. Information technology is useful for displaying text that is required to fit within a specific space, and thus may need to use an ellipsis or truncation to size the cord to fit. Labels also are useful in that they can have mnemonics which, if used, will ship focus to the Control listed as the target of the labelFor property. Characterization tin only a brandish of text or prototype and information technology cannot get focus.
Constructor for the Characterization class are:
- Label(): Creates an empty label
- Label(String t): Creates Label with given text.
- Label(String t, Node g): Creates a Label with the given text and graphic.
Unremarkably used methods:
method | explanation |
---|---|
createDefaultSkin() | Create a new instance of the default peel for this command. |
getLabelFor() | Gets the value of the property labelFor. |
labelForProperty() | A Label tin human activity every bit a label for a different Command or Node. |
setLabelFor(Node v) | Sets the value of the belongings labelFor. |
Below programs illustrate the use of label:
- Program i: This programme creates a label indicated by the proper name b. The progress indicator volition be created inside a scene, which in turn volition be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. Then a stack-pane is created, on which addChildren() method is called to adhere the label inside the scene, forth with the resolution specified by (200, 200) in the code. Finally the testify() method is called to display the last results.
Java
import
javafx.awarding.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.outcome.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
public
form
label
extends
Application {
public
void
start(Stage southward)
{
s.setTitle(
"creating label"
);
Label b =
new
Label(
"This is a label"
);
StackPane r =
new
StackPane();
r.getChildren().add(b);
Scene sc =
new
Scene(r,
200
,
200
);
s.setScene(sc);
due south.show();
}
public
static
void
main(String args[])
{
launch(args);
}
}
- Output:
- Java program to create a label with images: This plan creates a label with images indicated by the name b, the image is named i and the imageview is indicated by proper name iw. The label will be created inside a scene, which in turn volition be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide championship to the stage. And then a stack-pane is created, on which addChildren() method is called to adhere the label with images inside the scene, along with the resolution specified by (200, 200) in the code. Finally the show() method is called to display the final results.
Java
import
javafx.awarding.Awarding;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.result.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
import
javafx.scene.image.*;
import
java.io.*;
public
class
label_1
extends
Application {
public
void
outset(Phase s)
throws
Exception
{
s.setTitle(
"creating label"
);
FileInputStream input =
new
FileInputStream(
"f:\\gfg.png"
);
Image i =
new
Epitome(input);
ImageView iw =
new
ImageView(i);
Label b =
new
Characterization(
""
, iw);
StackPane r =
new
StackPane();
r.getChildren().add together(b);
Scene sc =
new
Scene(r,
200
,
200
);
southward.setScene(sc);
due south.show();
}
public
static
void
main(Cord args[])
{
launch(args);
}
}
- Output:
- Java plan to create a characterization with images and text: This plan creates a label with images and text indicated by the proper noun b, the image is named i and the imageview is indicated by name iw. The text to be displayed on the label is passed an argument to the constructor of the label. The label volition exist created inside a scene, which in turn volition be hosted within a stage (which is the meridian level JavaFX container). The office setTitle() is used to provide title to the stage. And so a stack-pane is created, on which addChildren() method is called to adhere the label with images inside the scene, along with the resolution specified past (200, 200) in the code. Finally the show() method is called to display the final results.
Java
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.command.*;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.consequence.EventHandler;
import
javafx.scene.control.Characterization;
import
javafx.stage.Stage;
import
javafx.scene.paradigm.*;
import
coffee.io.*;
public
class
label_2
extends
Awarding {
public
void
start(Stage s)
throws
Exception
{
s.setTitle(
"creating label"
);
FileInputStream input =
new
FileInputStream(
"f:\\gfg.png"
);
Image i =
new
Image(input);
ImageView iw =
new
ImageView(i);
Label b =
new
Label(
"Label Text"
, iw);
StackPane r =
new
StackPane();
r.getChildren().add(b);
Scene sc =
new
Scene(r,
200
,
200
);
s.setScene(sc);
s.show();
}
public
static
void
primary(String args[])
{
launch(args);
}
}
- Output:
- Java program to create a labels and textfield and utilise setLabelFor property: In this program the labels are set up for mnemonic parsing (if Alt+1 is presses then focus shifts to first textfield and if Alt + 2 is presses then focus shifts to 2d textfield.
Coffee
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.result.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
import
javafx.scene.paradigm.*;
import
java.io.*;
public
class
label_3
extends
Application {
public
void
first(Stage s)
throws
Exception
{
due south.setTitle(
"creating label"
);
TextField b1 =
new
TextField(
"textfield"
);
Characterization b =
new
Label(
"_1 TextField"
);
b.setLabelFor(b1);
TextField b4 =
new
TextField(
"textfield"
);
Label b3 =
new
Characterization(
"_2 TextField"
);
b3.setLabelFor(b4);
TilePane r =
new
TilePane();
b.setMnemonicParsing(
true
);
b3.setMnemonicParsing(
true
);
r.getChildren().add(b);
r.getChildren().add(b1);
r.getChildren().add(b3);
r.getChildren().add(b4);
Scene sc =
new
Scene(r,
200
,
200
);
due south.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
launch(args);
}
}
- Output:
Note: The post-obit programs might non run in an online IDE delight employ an offline compiler. You lot should accept the latest version of Java to run this programs.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Label.html
Source: https://www.geeksforgeeks.org/javafx-label/
Posted by: reesewousing.blogspot.com
0 Response to "How To Change Label Text On Button Click Javafx"
Post a Comment