java - JavaFx 8 - A class that has a border than you're able to color and able to have text in the middle of it -
java - JavaFx 8 - A class that has a border than you're able to color and able to have text in the middle of it -
does know of class can utilize essential rectangle, has text in middle of rectangle , rectangle has fill color along border color(the border can changed reddish or along lines)
essentially right have pane, , want create 2d grid(10x10), each individual object in grid rectangle-typed object has number text center justified, fill color, , border color.
note: i've tried utilize gridpane, lack of documentation i've found has led me believe can set fill color, , each cell in grid pane not separate object want to. i've tried implement rectangle rectangle doesn't have text or border can manipulate.
thank help.
just utilize label
. here's proof of concept:
import javafx.application.application; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.layout.stackpane; import javafx.stage.stage; public class customlabeldemo extends application { @override public void start(stage primarystage) { label label = new label("hello world"); label.setstyle( "-fx-alignment: center;" +"-fx-padding: 6px;" +"-fx-background-color: red, -fx-background;" +"-fx-background-insets: 0, 4px;" ); stackpane root = new stackpane(label); primarystage.setscene(new scene(root, 350, 75)); primarystage.show(); } public static void main(string[] args) { launch(args); } }
the way css working here defines 2 backgrounds: first (and 1 @ back) red; 1 in front end set -fx-background
, color defined in default stylesheet background of controls. corresponding these 2 insets 2 backgrounds: first set zero, , sec set 4 pixels. means 4 pixels of reddish border visible. padding set create sure text doesn't overlap outer background (the border).
in real application, should set style in external file. can define "looked-up-color" border color; create much easier alter color @ runtime:
import javafx.application.application; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.layout.vbox; import javafx.stage.stage; public class customlabeldemo extends application { @override public void start(stage primarystage) { label label = new label("hello world"); label.getstyleclass().add("custom-label"); button changecolorbutton = new button("change green"); changecolorbutton.setonaction(event -> label.setstyle("custom-label-border-color: green;")); vbox root = new vbox(10, label, changecolorbutton); root.setalignment(pos.center); scene scene = new scene(root, 350, 75); scene.getstylesheets().add("custom-label.css"); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } }
with custom-label.css:
.custom-label { custom-label-border-color: reddish ; -fx-alignment: center; -fx-padding: 6px; -fx-background-color: custom-label-border-color, -fx-background; -fx-background-insets: 0, 4px; } .button { -fx-alignment: center ; }
if have fixed set of states colors represent, might want utilize pseudoclass represent state:
import javafx.application.application; import javafx.css.pseudoclass; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.checkbox; import javafx.scene.control.label; import javafx.scene.layout.vbox; import javafx.stage.stage; public class customlabeldemo extends application { @override public void start(stage primarystage) { label label = new label("hello world"); label.getstyleclass().add("custom-label"); checkbox errorcheckbox = new checkbox("error"); pseudoclass errorstate = pseudoclass.getpseudoclass("error"); errorcheckbox.selectedproperty().addlistener((obs, wasselected, isnowselected) -> label.pseudoclassstatechanged(errorstate, isnowselected)); vbox root = new vbox(10, label, errorcheckbox); root.setalignment(pos.center); scene scene = new scene(root, 350, 75); scene.getstylesheets().add("custom-label.css"); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } }
custom-label.css:
.custom-label { custom-label-border-color: greenish ; -fx-alignment: center; -fx-padding: 6px; -fx-background-color: custom-label-border-color, -fx-background; -fx-background-insets: 0, 4px; } .custom-label:error { custom-label-border-color: reddish ; } .check-box { -fx-alignment: center ; }
java netbeans javafx-8
Comments
Post a Comment