Read external Files with GlassFish / Vaadin? - java

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)

Related

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.

Add confirm (Yes /No) dialog box inside Model class in Adempiere

I want to create confirm dialog box inside Model class.
I created window which has several text boxes and after entering the values user can save data. At the point of saving data I want to add a confirm dialog box asking "Are you sure to save these data ?"
So inside Model class I tried to put
org.adempiere.webui.window.FDialog.ask(1,null,"Are you sure to save these data ?");
When I add this into my code, it will give errors and I can't build the project.
If anyone knows how to add confirm dialog box in model class? Please help me to do this...
In Adempiere
For Swing Class (i.e.) model class you can use by below
int response = JOptionPane.showConfirmDialog(null, Are you sure to save these data ?
"", JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION)
;
else
;
or
Adialog in Client modules cann't used in Base modules
ADialog.ask(WindowNo, null,"Are you sure to save these data ?");
FDialog Should be used only in ZKWebui package, never use zk classes in base/client modules
org.adempiere.webui.window.FDialog.ask(1,null,"Are you sure to save these data ?")
In window/tab before save you can use "Commit Warning" column in Window,Tab & field (Application Dictionary)
Hope it will helps you.
You can use JOptionPane but not ADialog or FDialog.
Usage of ADialog throws build error. As its is defined in client folder, you cant use it in the upper hierarchy.
You can find the build order from here

watermark in birt based on parameter

I have a invoice for which I want to show in preview mode with a watermark. I have integrated the report with my web application (Spring MVC).
Any solution?
EDIT: I want to pick a value from a dataset (Table: invoiceheader), and use that value to decide whether to show /not show the watermark. I am unable to pick that value from Birt script. Can you please please guide..
Sorry .. the customer asked for a different way of identifying the fact rather that from passing the parameter.
Thanks
You need to declare a report parameter in the .rptdesign of the invoice. For example we name it "useWatermark", and we set the datatype to boolean and a default value to true.
Select the report root in the outline view of the designer -> script tab -> beforeFactory
if (!params["useWatermark"].value){
reportContext.getDesignHandle().findMasterPage("Simple MasterPage").setProperty("backgroundImage", "");
}
This script removes the watermark if the parameter is set to false. By default, the master page is named "Simple Masterpage" but if you have renamed it or if there are multiple masterPages defined in the report you should adjust the script consequently.
EDIT: if we need to extract the information from a dataset, then we can't make use of the masterPage, we have to use a grid instead.
Create a grid with one cell, and drop all the content of your page into it
Set your watermark as background-image of the grid
Create a report variable "useWatermark" in the "Outline/variables" branch (see picture below), set default value to true/false as you like
Select dataset invoiceHeader -> script tab -> onFetch, and affect the variable from a boolean dataset column, or from any expression returning true/false:
vars["useWatermark"]=row["myBooleanDatasetColumn"];
This dataset must be used somewhere in the report body, otherwise it won't be triggered and the variable won't be initialized
Select the grid -> script tab -> onRender
if (!vars["useWatermark"]){
this.getStyle().backgroundImage="";
}

Vaadin, where to put images?

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"));

Categories