When I execute this simple Vaadin example (auto generated code) with Idea14.
#Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
Button button = new Button("Click Me");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
layout.addComponent(new Label("Thank you for clicking"));
}
});
layout.addComponent(button);
}
I receive this output:
screenshot
I expected a button with CSS.
What did I do wrong?
The browser is not able to download the theme of your application.
Do you have the vaadin-themes dependency added to your project?
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-themes</artifactId>
</dependency>
Related
Hi can anyone tell me how to make a click able button​In Libgdx
For a mobile device that does something if you touch it
This is a minimal example of what you are trying to achieve with comments
package com.mygdx.gtest;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
public class Test extends ApplicationAdapter{
private Skin skin;
private TextButton button;
private Stage stage;
#Override
public void create() {
//make a stage for your button to go on
stage = new Stage();
//load a skin(a collection of styles for objects)
// skin is from gdx-skins (https://github.com/czyzby/gdx-skins)
skin = new Skin(Gdx.files.internal("neon-ui.json"));
//create your button
button = new TextButton("Button1", skin);
//add it to your stage
stage.addActor(button);
// add a listener to your buttons so it does something when clicked
button.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
System.out.println("I was clicked");
}
});
// set the sgae as the input processor so it will respond to clicks etc
Gdx.input.setInputProcessor(stage);
}
#Override
public void render() {
//clear the screen
Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// tell stage to do action and then draw itself
stage.draw();
stage.act();
}
}
This is how you can add a Button without adding any external skin
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = yourCustomFont;
textButtonStyle.fontColor = Color.WHITE;
stage.add(new TextButton("Custom Btn ", textButtonStyle));
I want to embed a dukescript app inside a Swing App so I think that I have to embed a panel to support JavaFX rendering.
final JFXPanel fxPanel = new JFXPanel();
Scene scene = createScene();
fxPanel.setScene(scene);
To be able to render Dukescript I think that I have to add a Webview so the alreasy existing JavaFX presenter (from html for java) can receive it an use it accordingly.
Group root = new Group();
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
root.getChildren().add(browser);
And then to use it with Dukescript I think that I should add maven dependency for JavaFx presenters.
<dependency>
<groupId>org.netbeans.html</groupId>
<artifactId>net.java.html.boot.fx</artifactId>
<scope>runtime</scope>
</dependency>
So I think it the desired code should be something like that.
net.java.html.boot.fx.FXBrowsers presenter= new FXBrowsers();
presenter.load(browser ,
"index.html",
Runnable onPageLoad??);
Have you already tried it? Thank you in advance.
the leaflet4j Project has a demo of embedding DukeScript in JavaFX:
https://github.com/dukescript/leaflet4j
It should work the same in Swing, but you'll have to call the JavaFX code on the JavaFX EventQueue, similar to this:
Platform.runLater(new Runnable() {
#Override
public void run() {
webView = new WebView();
FXBrowsers.load(webView, this.getClass().getResource("/demo/test.html"),
new Runnable() {
#Override
public void run() {
try {
DataModel.onPageLoad();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
);
I have a custom DialogFragment as an inner class in my Activity. The custom DialogFragment contains 2 Buttons.
The first Button opens the Camera and the second one opens the Gallery.
Normally this DialogFragment is shown after an Image is pressed.
Until here everything is fine.
Now I want add a new functionality. When the Activityis opened the first time, I want to open the Camera automatically, that means I want to "press" the first Button of my DialogFragment.
In my Activity onCreate method I just show the DialogFragment and perform a click on the first Button. The problem is that the DialogFragment is not dismissed.
Here is my code:
public static class MyDialogFragment extends DialogFragment {
Button openCameraButton;
Button openGalleryButton;
boolean openCameraAutomatically;
public static MyDialogFragment newInstance(boolean openCameraAutomatically) {
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putBoolean("open_camera", openCameraAutomatically);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openCameraAutomatically = getArguments().getBoolean("open_camera");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_dialog, container, false);
getDialog().setTitle("title");
openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button);
openCameraButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// launch Camera Intent...
getDialog().dismiss();
}
});
openGalleryButton = (Button) rootView.findViewById(R.id.open_gallery_button);
openGalleryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Launch Gallery Picker Intent
getDialog().dismiss();
}
});
if(openCameraAutomatically) {
openCameraButton.performClick();
}
return rootView;
}
}
And here is how I call it:
FragmentManager fm = getSupportFragmentManager();
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(fm, "");
The line getDialog().dismiss(); does not dismiss the DialogFragment, after the Camera callback (onActivityResult) the DialogFragment is still visible. If I press the Button manually (without using the method performClick) everything works fine.
Any ideas?
Thanks.
Try just dismiss() to dismiss the dialog and the fragment instead of getDialog().dismiss() which just dismisses the dialog and but not the fragment.
https://developer.android.com/reference/android/app/DialogFragment.html#dismiss()
void dismiss ()
Dismiss the fragment and its dialog. If the fragment was added to the back stack, all back stack state up to and including this entry will be popped. Otherwise, a new transaction will be committed to remove the fragment.
Update:
Here is another thought. You are trying to dismiss the dialog before the view for the DialogFragment is fully ready. This isn't a problem once the buttons are available for you to push, i.e., the layout is complete.
Try moving the automatic dismissal later in life cycle. I think that will work for you.
I am making a media player using javafx & eclipse. I have used 3 classes in my program. I am almost done. But I am facing a problem to implement a media library. I want to implement my media library or playlist using file management system in java. I know for this I need to use directory & file locations. As it is my first project & I am not that good in using file management in java so I really don't understand how to use them correctly & how to implement my library using file management. The main class of my code is given below.
Thanks in advance.
public class Main extends Application{
Player player;
FileChooser fc;
#Override
public void start(Stage primaryStage){
// TODO Auto-generated method stub
MenuItem open= new MenuItem("Open");
//MenuItem library= new MenuItem("Library");
Menu file= new Menu("File");
Menu library= new Menu("Playlist");
MenuBar menu= new MenuBar();
primaryStage.setTitle("My Media Player");
file.getItems().add(open);
menu.getMenus().add(file);
menu.getMenus().add(library);
fc= new FileChooser();
open.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
player.player.pause();
File file= fc.showOpenDialog(primaryStage);
if(file!=null)
{
try
{
player= new Player(file.toURI().toURL().toExternalForm());
Scene scene= new Scene(player, 1280, 690, javafx.scene.paint.Color.BLACK);
primaryStage.setScene(scene);
}
catch(MalformedURLException el)
{
el.printStackTrace();
}
}
}
});
library.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
}
});
player= new Player("file:///E:/duniya.mp4");
player.setTop(menu);
Scene sc= new Scene(player, 1280, 690, javafx.scene.paint.Color.BLACK);
sc.setRoot(player);
primaryStage.setScene(sc);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
}
I'm trying to make JavaFX Mnemonic work. I have some button on scene and what I want to achieve is to fire this button event by pressing Ctrl+S.
Here is a code sceleton:
#FXML
public Button btnFirst;
btnFirst.getScene().addMnemonic(new Mnemonic(btnFirst,
new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN)));
Button's mnemonicParsing is false. (Well, while trying to make this work I've tried to set it to true, but no result). JavaFX documentation states that when a Mnemonic is registered on a Scene, and the KeyCombination reaches the Scene unconsumed, then the target Node will be sent an ActionEvent. But this doesn't work, probably, I'm doing wrong...
I can use the standard button's mnemonic (by setting mnemonicParsing to true and prefix 'F' letter by underscore character). But this way user have to use Alt key, that brings some strange behaviour on browsers with menu bar (if application is embedded into web page than browser's menu activated after firing button event by pressing Alt+S).
Besides, standard way makes it impossible to make shortcuts like Ctrl+Shift+F3 and so on.
So, if there some way to make this work?
For your use case, I think you actually want to use an accelerator rather than a mnemonic.
button.getScene().getAccelerators().put(
new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN),
new Runnable() {
#Override public void run() {
button.fire();
}
}
);
In most cases it is recommended that you use KeyCombination.SHORTCUT_DOWN as the modifier specifier, as in the code above. A good explanation of this is in the KeyCombination documentation:
The shortcut modifier is used to represent the modifier key which is
used commonly in keyboard shortcuts on the host platform. This is for
example control on Windows and meta (command key) on Mac. By using
shortcut key modifier developers can create platform independent
shortcuts. So the "Shortcut+C" key combination is handled internally
as "Ctrl+C" on Windows and "Meta+C" on Mac.
If you wanted to specifically code to only handle a Ctrl+S key combination, they you could use:
new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN)
Here is an executable example:
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class SaveMe extends Application {
#Override public void start(final Stage stage) throws Exception {
final Label response = new Label();
final ImageView imageView = new ImageView(
new Image("http://icons.iconarchive.com/icons/gianni-polito/colobrush/128/software-emule-icon.png")
);
final Button button = new Button("Save Me", imageView);
button.setStyle("-fx-base: burlywood;");
button.setContentDisplay(ContentDisplay.TOP);
displayFlashMessageOnAction(button, response, "You have been saved!");
layoutScene(button, response, stage);
stage.show();
setSaveAccelerator(button);
}
// sets the save accelerator for a button to the Ctrl+S key combination.
private void setSaveAccelerator(final Button button) {
Scene scene = button.getScene();
if (scene == null) {
throw new IllegalArgumentException("setSaveAccelerator must be called when a button is attached to a scene");
}
scene.getAccelerators().put(
new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN),
new Runnable() {
#Override public void run() {
fireButton(button);
}
}
);
}
// fires a button from code, providing visual feedback that the button is firing.
private void fireButton(final Button button) {
button.arm();
PauseTransition pt = new PauseTransition(Duration.millis(300));
pt.setOnFinished(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
button.fire();
button.disarm();
}
});
pt.play();
}
// displays a temporary message in a label when a button is pressed,
// and gradually fades the label away after the message has been displayed.
private void displayFlashMessageOnAction(final Button button, final Label label, final String message) {
final FadeTransition ft = new FadeTransition(Duration.seconds(3), label);
ft.setInterpolator(Interpolator.EASE_BOTH);
ft.setFromValue(1);
ft.setToValue(0);
button.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
label.setText(message);
label.setStyle("-fx-text-fill: forestgreen;");
ft.playFromStart();
}
});
}
private void layoutScene(final Button button, final Label response, final Stage stage) {
final VBox layout = new VBox(10);
layout.setPrefWidth(300);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(button, response);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 20; -fx-font-size: 20;");
stage.setScene(new Scene(layout));
}
public static void main(String[] args) { launch(args); }
}
// icon license: (creative commons with attribution) http://creativecommons.org/licenses/by-nc-nd/3.0/
// icon artist attribution page: (eponas-deeway) http://eponas-deeway.deviantart.com/gallery/#/d1s7uih
Sample output:
Update Jan 2020, using the same accelerator for multiple controls
One caveat for accelerators in current and previous implementations (JavaFX 13 and prior), is that you cannot, out of the box, define the same accelerator key combination for use on multiple menus or controls within a single application.
For more information see:
JavaFX ContextMenu accelerator firing from wrong tab
and the related JDK-8088068 issue report.
The linked issue report includes a work-around you can use to allow you define and use the same accelerator within multiple places within an application (for example on two different menu items in different context menus).
Note that this only applies to trying to use the same accelerator in multiple places within an application, if you don't need try to do that, then you can ignore this information.