I think I am missing something obvious, but I can't seem to change the value of a Slider (Scene2D.ui) programmatically.
The reason that I want to do this is because I have a character that you can customize with the sliders but I want to have some preset characters that you can use as a starting point for customization.
What I have so far is something like this:
(ChangeListener)(slider.getListeners().get(0)) but I cannot call the changed(ChangeEvent event, Actor actor) method that is called when the slider is changed.
You can use setValue(float value) method of Slider to change value of slider programmatically.
public class GdxTest extends ApplicationAdapter {
Stage stage;
Slider slider;
#Override
public void create() {
stage=new Stage();
Gdx.input.setInputProcessor(stage);
Skin skin=new Skin(Gdx.files.internal("skin/glassy-ui.json"));
Table table=new Table();
table.setFillParent(true);
stage.addActor(table);
table.add(slider=new Slider(0,100,1,false,skin));
slider.setValue(50); // value changed by 50%
}
...
}
slider.setAnimateDuration(0.3f);
Related
How to crate a multiline text? I tried to use Label , but text have only 1 line.
Table table = new Table();
table.setPosition(0,0);
table.setSize(800,440);
label = new Label("Some long string here...", skin);
label.setFillParent(true);
label.setAlignment(center);
label.setPosition(0,0);
label.setWidth(40);
label.setHeight(label.getPrefHeight());
label.setText(tutortext);
table.addActor(label);
stage.addActor(table);
stage.setDebugAll(true);
https://i.stack.imgur.com/3whQr.png
Use setWrap(true); on label.
According to doc.
If false, the text will only wrap where it contains newlines (\n). The preferred size of the label will be the text bounds.
If true, the text will word wrap using the width of the label. The preferred width of the label will be 0, it is expected that something external will set the width of the label. Wrapping will not occur when ellipsis is enabled. Default is false.
When wrap is enabled, the label's preferred height depends on the width of the label. In some cases the parent of the label will need to layout twice: once to set the width of the label and a second time to adjust to the label's new preferred height.
I've tested with small example :
public class MultiLine extends ApplicationAdapter {
Stage stage;
Skin skin;
#Override
public void create() {
stage=new Stage();
skin=new Skin(Gdx.files.internal("skin/glassy-ui.json"));
Label label=new Label("MULTILINE IN LIBGDX GAME DEVELOPMENT",skin);
label.setWrap(true);
Table table=new Table();
table.setFillParent(true);
table.add(label).width(150);
stage.addActor(table);
}
#Override
public void render() {
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act();
}
#Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}
Here is the output :
A way simpler solution is to just use \n for linebreaks so instead of
Label label=new Label("MULTILINE IN LIBGDX GAME DEVELOPMENT",skin);
you would just do
Label label=new Label("MULTILINE\nIN\nLIBGDX\nGAME\nDEVELOPMENT",skin);
The label will be:
MULTILINE
IN
LIBGDX
GAME
DEVELOPMENT
I would like to hide or deactivate a TextField and its Label in my JavaFX application.
This is what I tried
myTextField.setVisible(false);
, but it's not working
I use Eclipse V4.5.0 on windows 7 with jfx8 V2.0.0
There is difference between hiding and deactivating a TextField in JavaFX.
To Hide :-
You need to set the visible property to false.
The possible reason's why its not working in your case is that if you have skipped mentioning the fx:id for your TextField or Label.
To do so just go through the fxml if using and set the fx:id="myTextField" , and then the same code that you have write will start to work.
The same is used to hide any Label.
To Deactivate :- There is a field named as disable just set that disable property to true to disable or deactivate any field.
I understand that you want to hide/show a text field (javaFX), i usually use the same method you mentioned
assume that the text field variable name is field then:
to hide it use
field.setVisible(false);
to show it use
field.setVisible(true);
and it works always for me.
I am not sure that I understand correctly your question, but I will try to answer to what I understand.
if you only want to deactivate TextField you can use:
myTextField.setEditable(false);
This will not "hide" the TextField it will be only not editable.
Based on your provided code, the problem might be in static created (in the FXML) TextField. What I suggest is to try and create the Pane and the TextField dynamically in the runtime.
Here is simple example how to create and use JavaFX components in the runtime :
public class ButtonInPane extends Application{
// Override the start method in the Application class
Button btn=new Button("OK");
HBox cont;
TextField myTextField;
public void start(Stage stage1){
Label myLable=new Label("Some Lable");
myTextField=new TextField("Some text");
cont=new HBox();
cont.getChildren().addAll(myLable,myTextField);
Stage stage = new Stage(); //this instead of JFrame
FlowPane pane2 = new FlowPane(); //this instead of JPanel
pane2.getChildren().addAll(btn,cont);
Scene scene2 = new Scene(pane2, 250, 50);
stage.setTitle("Button in a FlowPane"); // Set the stage title
stage.setScene(scene2); // Place the scene in the stage
stage.show(); // Display the stage
stage.setAlwaysOnTop(true);
//set event
setEventActions();
}
private void handlePlayAction() {
cont.setVisible(false);
//OR myTextField.setVisible(false);
}
private void setEventActions() {
this.btn.setOnAction(event -> this.handlePlayAction());
}
public static void main(String[] args)
{ launch(args);
}
}
You can use:
myTextField.setDisable(true);
It will disable field for a particular Action.
You can also do this for a Label in your .fxml file like this:
<Label layoutX="349.0" layoutY="85.0"
text="Label" visible="false" fx:id="actionSuccessLabel"/>
and then display it later in your Controller class like this:
actionSuccessLabel.setVisible(true);
Don't forget to do the setVisible or the visible property binding after the new function.
TextField textField = new TextField(field.getValue());
textField.visibleProperty().bind(field.getVisibleProperty());
I am writing a javafx program, for moving text. The program display the background (some red points) and text at the beginning.
Now, I would like to add a menu to select the content of text to display. In addition, I want the scene to display nothing at the beginning, then after I select content of text, the program starts to display everything...
Is there any special command to implement this?
PS: I add the contents (text and shapes) -> "Group" -> "root".
I think what you want is something along this:
public class PersonOverviewController implements Initializable{
#FXML
BorderPane paneWithControls;
#FXML
MenuItem menuItem;
#FXML
Label text;
#FXML
private void initialize() {
paneWithControls.setVisible(false); //hide content
menuItem.setOnAction(new EventHandler<ActionEvent>() { //implementing action listener
#Override
public void handle(ActionEvent event) {
text.setText("Some text you want to display"); //set value for controls
paneWithControls.setVisible(true); //display content
}
});
}
}
EDIT:
If you really want to "hide" the scene you can do something like this:
primaryStage.setScene(null);
and later on:
primaryStage.setScene(sceneObject1);
However this is not good way to achieve result you are looking for. Once you have the scene, just change the root of it, or set properties of it's controls. I have mistaken this also when I was learning JavaFX.
There is plenty of online resource on how to add a graphic to a JavaFX button.
However I would like to know if there is a way to add a second (or any number, but more than 2 images probably woudn't make much sense in most situations) image.
My use case : i have a button with a square icon on the left, followed by a text label. The image is a representation of some real-life concept that button is linked with (could e.g. a car or a person). I would like to add a small icon to the right of some buttons, a "right chevron" to indicate the nature of the interaction.
I was thinking maybe to use a HBox with the full width as the graphic node of the button, and add the 2 images to it, but I don't think it is possible to put the text on top of the graphic node.
Any idea ?
Create your own custom node with icons and text and set it as graphic. Of course you don't show the button text because it's already in your custom graphic node.
Here's a simple example. You need to provide the files icon1.png and icon2.png.
public class ButtonWithMultipleIcons extends Application {
#Override
public void start(Stage primaryStage) {
try {
Group group = new Group();
Button button = new Button();
button.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
HBox hBox = new HBox();
ImageView icon1 = new ImageView( getClass().getResource( "icon1.png").toExternalForm());
ImageView icon2 = new ImageView( getClass().getResource( "icon2.png").toExternalForm());
Label label = new Label("Text");
//make the button grow if you want the right icon to always be on the right of the button :
label.setMaxWidth(Long.MAX_VALUE);
HBox.setHgrow(label, Priority.ALWAYS);
hBox.getChildren().addAll( icon1, label, icon2);
button.setGraphic(hBox);
group.getChildren().add( button);
Scene scene = new Scene(group,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
You can try using html formatting in your button and adding an image with the tag before and after your text.
You can find more information about it in here:http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
I had the same thing to get along with. You describing the solution with using a HBox. I think this would also be the best way.
Do the following:
Create a new CustomButton with your own specific layout maybe using a HBox and extending Button class from JavaFX. Style it your way you need it and you can add as many images you want to have in your custom button. Maybe it can look like this:
public class CustomButton extends Button
{
private HBox hbox;
private ImageView image1;
private ImageView image2;
public CustomButton()
{
super();
// Here add your specific images to global or local variables and then add all the children (images) to your HBox layout and this one to the button.
}
}
The problem ist, that JavaFX provides normal buttons and not such deeply customized components. If you want to style it, you can use CSS anyway.
I want to create a button that changes when the user hovers it, or clicking it.
I created the following variable
Button buttonPlay = new Button();
I don't know what to do now, how to load the images? How to write text into the button? How to implement the events / effects (hover, click)?
It would be very helpful if someone could write some example code for a button.
A button is simply an actor in libgdx. To render an actor you use a stage that contains all the actors of the screen, renders them and updates them. I assume you want a button with text, so you should use the class TextButton and add it to a stage. A TextButton takes a string to render and a ButtonStyle, in this case a TextButtonStyle, which is basically a class that contains all the information about the button (font, drawable to render while not pressed, drawable to render while pressed etc).
public class ButtonExample extends Game{
Stage stage;
TextButton button;
TextButtonStyle textButtonStyle;
BitmapFont font;
Skin skin;
TextureAtlas buttonAtlas;
#Override
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
font = new BitmapFont();
skin = new Skin();
buttonAtlas = new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
skin.addRegions(buttonAtlas);
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = skin.getDrawable("up-button");
textButtonStyle.down = skin.getDrawable("down-button");
textButtonStyle.checked = skin.getDrawable("checked-button");
button = new TextButton("Button1", textButtonStyle);
stage.addActor(button);
}
#Override
public void render() {
super.render();
stage.draw();
}
}
So whats happening here? I am creating a stage, a font and a textureatlas with all the textures for the buttons in "buttons.pack". Then I initialize an empty TextButtonStyle and
and i add the font and the textures for the up, down and checked states. font, up, down and checked are all static variables of type Drawable so you can really pass it any kind of Drawable (texture, 9-patch etc). Then simply add the button to the Stage.
Now in order to do something when the button is actually clicked, you have to add a listener to the button, a ChangeListener.
button.addListener(new ChangeListener() {
#Override
public void changed (ChangeEvent event, Actor actor) {
System.out.println("Button Pressed");
}
});
Of course instead of adding the button directly to the Stage you should add it to a Table and add the Table to the Stage but I didn't want to make this post too confusing. Here is a good tutorial on tables in libgdx.
This is how you do it without adding any skin
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = yourCustomFont;
textButtonStyle.fontColor = Color.WHITE;
stage.add(new TextButton("Custom Btn ", textButtonStyle));
buttons.pack is the file generated from the libgdx texture packer, texture packer is the tool which can used to generate the texture atlas , That is multiple images you can loaded to the GUI using a single file . it also help to save some memory please refer this link`https://code.google.com/p/libgdx-texturepacker-gui/downloads/list,
https://github.com/libgdx/libgdx/wiki/Texture-packer