I have been trying to find a way to build two Ellipse object locationed next to each other that can be clicked and go to other screens by clicking one of them.
I don't know where to start. Right now I know only hope to build 2 regular buttons.
And how do we create it?
Just use a Circle as shape for the buttons. By default the shape will be scaled to the Button size, which results in ellipse shaped Buttons.
Button btn = new Button("Say 'Hello World'");
// circular shape scaled to Button dimensions = ellipse
btn.setShape(new Circle(1));
// increase distance from edges to text
btn.setPadding(new Insets(8));
What you can do is have a label
Label label = new Label();
label.setId("button");
And then add a image ellipse or what ever that you want to your label.
Image image = new Image(getClass().getResourceAsStream("ellipse.png"));
label.setGraphic(new ImageView(image));
Then add a mouse click event to the label
label1.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
// write code to change screen
} });
elipse.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
//Add scene change here.
});
Related
Hey I'm currently creating a back option to my program. I would like it so that when they user clicks the top left of the background image it will bring you back to the last page. I'm not really sure how I would go about doing so. I know that you can create setOnMouseClicked to buttons but can you do it for a background image and can you specify the cordinates aswell?
here is my code
public class Home extends StackPane{
public Home(){
javafx.scene.image.Image sizeBG = new
javafx.scene.image.Image("file:src/Images/Background.jpg");
BackgroundSize bSize = new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, false);
this.setBackground(new Background(new BackgroundImage(sizeBG, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, bSize)));
bSize.setOnMouseClicked(e ->{
MainScene.mainStage.setScene(new IntroScene());
});
}
}
Here is a reference of my background image. the top left arrow is what I would like to click
I want to create button without any borders or shadow, but an icon instead using java swing component. How can I accomplish this?
Real Button
JButton btnNewButton = new JButton("");
btnNewButton.setContentAreaFilled(false);
btnNewButton.setBorderPainted(false);
btnNewButton.setBorder(null);
btnNewButton.setIcon(new ImageIcon(path));
This will give you a real button without any borders around the given image to work with. Note that in this state the button doesn't have a "click animation" anymore. For such an animation you could use the .setSelectedIcon(selectedIcon);
Clickable Image
ImageIcon img = new ImageIcon(path);
JLabel button = new JLabel(img);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//Set pressed or something else
}
});
But this one just provide you a clickable Image and should only be used when a clickable Image without any other intentions is needed.
Note that this way is just a workaround.
How to make a button stacked behind a Label clickable?
Button
Button btn = new Button();
//TODO: button's main function, like onPressed, onReleased etc. here...
Custom label
CustomLabel sp = new CustomLabel("Some texts here"));
//TODO: custom label's main function, like a translucent background etc here...
//main purpose of this is to overlay the button
Main Pane
StackPane mainPane = new StackPane();
mainPane.getChildren.addAll(btn, sp);
With this, the area which the custom label overlays becomes unclickable.
Is there anyway to make the button still clickable even though overlay-ed, for example?
Or is there another way to do it? Something like setting label to not visible on click?
EDIT : To answer Itamar Green 's question..
By using the example as shown in this link: Mouse Events get Ignored on the Underlying Layer , it still does not seems to work. The button stacked under the layer is still not clickable.
sp.setPickOnBounds(false);
You can set the mouseTransparent property of the element(s) drawn on top to true. Note that this way all descendants of the node are ignored for mouse events:
#Override
public void start(Stage primaryStage) {
Button btn = new Button("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
System.out.println("Hello World!");
});
Region region = new Region();
region.setStyle("-fx-background-color: #0000ff88;");
region.setMouseTransparent(true);
StackPane root = new StackPane(btn, region);
Scene scene = new Scene(root, 100, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
Comment out
region.setMouseTransparent(true);
and the button will no longer react to mouse events in any way...
thanks for your help. I think I have solved my own question.
All I did was set a clickable event for my label.
Label sp = new Label("some texts here")
sp.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
//copy over the button's event.
}
});
Not sure if there is any better way of doing this...
in addition with the correct answer. you can enable or disable " mouse transparent " property via fxml with scenebuilder
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'm looking to add interactivity to an image but cannot see a way off adding a mouselistener to it. I would like to get the X & Y of where whas clicked on the image.
The Flow if the image is:
tileset = new ImageIcon("xx.png"); //ImageIcon Image that wants to be clicked
label.setIcon(tileset); // assigned to a label
panel.add(label); //assigned to a panel
tileScrollPane = new JScrollPane(panel); // Assigned to a scrollable pane
frame.add(tileScrollPane, BorderLayout.CENTER); // then onto a JFrame
You should be able to add a MouseListener to the label:
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent event) {
// Handle click - coordinates in event.
}
});