Vaadin, where to put images? - java

I have a Liferay project with Vaadin portlet.
I want to add icon to my buttons.
Button search = new Button("Search");
search.setIcon(new ThemeResource("img/silk/add.png"));
But dont know where i gonna put it? Now i put it in docroot directory.
UPDATE
No i try this.
Button search = new Button("Search");
search.setIcon(new ThemeResource("LifeRayVaadin-portlet/icons/add.png"));
But when i redeploy project in console get
09:34:05,773 WARN [404_jsp:109] /html/VAADIN/themes/liferay/LifeRayVaadin-portlet/icons/add.png

So your portlet is looking for the icons in /html/VAADIN/themes/liferay/LifeRayVaadin-portlet/icons/add.png.
You could create a directory icons under VAADIN and call:
search.setIcon(new ThemeResource("../../icons/add.png"));
ThemeResource without any path will look for the file in VAADIN/themes/yourtheme/ path and thus ../../ will get you (in this case) to /VAADIN/. I personally would never hardcode the name of a theme or a portlet in a project, because when it changes you have to go through every reference and change them.

You can put your images in $PORTLET-NAME/docroot/icons directory and call them using the Path
/$PORTLET-NAME$/icons/add.png
in your case it will be
Button search = new Button("Search");
search.setIcon(new ThemeResource("/$PORLTET-NAME$/icons/add.png"));

Related

Read external Files with GlassFish / Vaadin?

I would like to make .txt-files accessible for my VaadinApp on a GlassfishServer.
Lets say I have a .txt-file, its content is 12345.
Now, when I click a button on my Vaadin StartPage, I would like to show the Data that has been written into this .txt-file.
I know how Java Input/Output is working but I have no clue how to make those files accessible for my VaadinApplication running on Glassfish 4.1.2.
Is there a folder I can put the .txt-file in, or how would I access the file?
Thanks
There is component named Label is available in Vaadin.
So that, the values which needs to be shown on the screen can be set as a caption/value for that object.
This can be done either through constructor or setter in that object.We will set the value through the setter as we need to display the value, once the button is clicked. That can be done like this.
Label sampleLabel = new Label();
sampleLabel.setContentMode(com.vaadin.shared.ui.ContentMode.HTML);
Now we will see how this can be added to the label, when a button is clicked.
Button sampleButton = new Button("Click");
sampleButton.addClickListener(event -> sampleLabel.setValue(<call the method that reads data from the text file>));
I hope this will be helpful.
Note: Basically you can place the file anywhere in the system.
But most preferred way.
If you are using maven to build the project, place the files in the resource folder.(src/main/resources)

Custom ToolBar with Java Swing for Desktop

I have created a GUI with Java Swing and wanting to create a custom toolbar according to my modules. Below are the images am wanting to use:
These images are placed in the same level as the src folder within my application. I am aware that I can perhaps create a jar with these images so that I can easily access them from within my application but do not know how. I have spent hours trying to make this work.
Below is my GUI that I have created ad wanting to beautify with these images for the toolbar else create an array of labels that will act as a navigation but either approach I couldn't get it to work.
The code below was my last attempt on this:
JToolBar toolbar1 = new JToolBar();
ImageIcon client = new ImageIcon("clients.png");
ImageIcon timesheet = new ImageIcon("timesheets.png");
JButton clientTB = new JButton(client);
JButton timesheetTB = new JButton(timesheet);
toolbar1.add(clientTB );
toolbar1.add(timesheetTB);
add(toolbar1, BorderLayout.NORTH);
I even moved these images and placed them within the class that's calling them.
What could I be doing wrong, please help?
You have a look at the JavaDocs for ImageIcon(String), the String value is "a String specifying a filename or path"
This is a problem, because your images aren't actually files, any more, they have been embedded within your application (typically within the resulting jar file) and no longer be treated like "normal files".
Instead, you need to use Class#getResource which searches the application's classpath for the named resource, something like...
// This assumes that the images are in the default package
// (or the root of the src directory)
ImageIcon client = new ImageIcon(getClass().getResource("/clients.png"));
Now, I have a personal dislike for ImageIcon, because it won't tell you when the image is loaded for some reason, like it can't be found or it's the wrong format.
Instead, I'd use ImageIO to read the image
ImageIcon client = new ImageIcon(ImageIO.read(getClass().getResource("/clients.png")));
which will do two things, first, it will throw a IOException if the image can't be loaded for some reason and two, it won't return until the image is fully loaded, which is helpful.
See Reading/Loading an Image for more details

Javafx change view during runtime and save the new one

I am searching for a way to change the view of an JavaFX FXML Application during runtime and save the result so, that it is present at the next Application start. Is there a way? I was searching the WWW but dont find any solution! For example: I have a function where i can add a label to a GridPane and can save the new added label. At the next start of my Application, the new label is present. Its for an DB Application where the user can add new Database fields for custom data.
I think, by using properties file(or DataBase) you can achieve this. Before closing the application update the current status into properties file. So that while opening the application once again read the properties file and update it to the previous status.

Netbeans Plugin development: #ActionReference paths reference

I am developing a Netbeans plugin which will verify a Web Project against some rules.
I am trying to add it a menu as a first item in the Projects context menu. From what I understand, this is done by specifing the path attribute in the #ActionReference annotation. Anyone knows where to find a complete reference for the possible paths?
This is what I have now, and my Menu item appears somewhere in the bottom of the Projects context menu, so I suspect I need something different than "Projects/Actions"
#ActionID(
category = "Tools",
id = "org.chain.war.WarVerifier")
#ActionRegistration(
displayName = "#CTL_WarVerifier")
#ActionReference(path = "Projects/Actions", position = 0)
public final class WarVerifier implements ActionListener
I am on Netbeans 7.2
Thanks,
Chris.
Right-click on one your package New > Other... and under Module Development choose XML Layer.
This XML Layer will show as a drop down containing 2 elements, choose this layer in context you will have the full hierarchy.

javafx open a new fxml file in new tab dynamically

I am creating a javafx program in which i need to open a new fxml file in new tab dynamically.
I want that When user click on button a new tab is opened with new fxml file.
I Had no idea I can add tab as per design as much as i need but i want to do it dynamically when user click on button then only a new tab open.
I had also seen Questing but not working for me.
Please help me.
Thank you.
You can add tabs dynamically with
myTabPane.getTabs().add(myNewTab);
Create a new Tab with new Tab(), load your FXML and call
myNewTab.setContent(loadedFxmlRoot);
You can add tabs using this code, you need to confirm if your tabPane has this tab or no, else your program invokes SizeOfBounds exception.
if (!MessagePane.getTabs().contains(AllMessageTab)) {
MessagePane.getTabs().add(AllMessageTab);
}
SingleSelectionModel<Tab> selectionModel = MessagePane.getSelectionModel();
selectionModel.select(AllMessageTab);
AllMessageTab.setContent(_YourContentNodeHere);

Categories