Custom exe icon in Netbeans native package for Swing Application - java

I want to use my icon in native packaging for java Swing application. There is no option for setting Application icon in properties. There is by default available for JavaFX applications. I need it for Swing having custom exe icon.
Inno setup is used for native packaging in netbeans 8.0.2.
Application icon is in Java Coffee cup icon. I can able to set the frame icon, but I need to set it for generating exe.
My swing application icon

My actual configuration is Netbeans 8.01 with JDK 8 on windows 10
If you look inside your project, there is a subfolder nbproject that contains build directives. A close look at the file build-native.xml give you that:
if(isTrue(nativeEnabled) && defined(nativeType) && nativeType != "none") {
var icon = new String(project.getProperty("deploy.icon.native"));
if(defined(icon)) {
if(isTrue(fx_ant_api_1_2) && !isTrue(have_jdk_pre7u14)) {
var dicon = derelativizePath(icon);
// create temporary icon copy renamed to application name (required by native packager)
so you have to add a line in YourProjectFolder/project.properties and it must be like:
deploy.icon.native=C:\\Path\\of\\the\\icon\\yourIcon.ico
(path double slashed)
After that you will be able to see the generated file with yourIcon provided.

In your main Frame (In Navigation Window generally in the lower left) Click the JFrame to get the JFrame Options.
In the properties window (generally in the lower right) Go to Other Properties group->IconImage (Click the button (...) )
Choose custom code and there put something like:
new ImageIcon(getClass().getResource("/resources/images/Image.png")).getImage()
Note that this image is in the classpath and is the path of the image in your JAR file.
So if you have the project package like org.myPackage in your directory /home/user/Projects/myProj/org/myPackage
You must have other directory /home/user/Projects/myProj/resources/images/ with this image.

Related

How to run Java Swing in Netbeans?

I've been trying to run gui in netbeans using java swing but it won't run at all. All the projects that I execcute that uses jframe does not run. Any tips on how to fix this thing? This is how it looks like:
I got this to run with no problems using NetBeans IDE 8.2. Let's start from the beginning.
Delete your current OfficeManagementSystem project from your IDE and local drive system. Select the OfficeManagementSystem project located within your IDE Project pane located on the left side of the IDE. Right-click on it and select the Delete popup menu item. A Delete Project dialog is displayed. Check-mark the Also delete sources under ..... checkbox then select the Yes button.
Close NetBeans!
To make sure all is gone, open your file browser (in Windows10 this is named File Explorer [win key + E]) and navigate to your NetBeans Projects folder. Default location in Windows is C:\Users\{yourAccountName}\Documents\NetBeansProjects\. Make sure that OfficeManagementSystem project folder is deleted.
With your web browser, go to the Source Code Web Page and select the Code button:
Then from the dropdown menu select Download ZIP:
A Save File dialog will appear possibly similar to this:
Select the Save File option then select the OK button. Choose a location to save the ZIP file and remember it.
Navigate to you downloaded file named HospitalManagementSystemusingJAVASwing-master.zip and Un-Zip it with your favorite decompression software like WinRar or 7Zip. Have it unzip into its own folder.
Navigate to and open the the folder created by your decompression software, it should have been named HospitalManagementSystemusingJAVASwing-master. If done properly you should have yet another folder (a sub-folder) also named HospitalManagementSystemusingJAVASwing-master. Rename this folder to HospitalManagementSystem by right-clicking on the folder and selecting Rename from the popup menu. Delete the unnecessary text within the name then hit your ENTER key.
After Renaming the folder, once again right-click on it and select Copy from the popup menu.
As described earlier, navigate to you NetBeansProjects directory (folder) and double-click on it. Paste the new HospitalManagementSystem folder into the NetBeansProjects folder. This project will now be added to the NetBeans Projects List.
Start NetBeans!
Once NetBeans has completely started, from the menu bar select the File ➞ Open Project... menu item. The Open Project dialog will be displayed. Scroll down the left pane of this dialog until you see HospitalManagementSystem and select it. Now hit the Open Project button. This project will load into the NetBeans IDE Projects pane located on the left side of your IDE under the name of: OfficeManagementSystem. Why this person changed the project name at this point is beyond me but to each their own.
Expand the project and its active package (officemanagementsystem) so as to expose all the classes involved. You should see the following listed:
LoginPage.java // The first Form we want displayed.
MenuPage.java // The Menu page (once login is achieved).
Module1.java // Should really be named PatientPage
Module2.java // Should really be named DoctorAvailabilityPage.
OfficeManagementSystem.java // The application start-up class.
This application still will not run since it contains nothing within the main() method of the start-up class (OfficeManagementSystem). Open this class into the IDE Editor by double-clicking on it.
Expand the main() method code block and add this code line into it:
new LoginPage().setVisible(true);
Run the application! The first Window displayed will be the Login Page. Username is: admin and the password is: admin. (change in code if you like). And away you go. :)
All java applications need a static main method as a bootstrap.
It should look something like the following:
public static void main(String[] args) { YourJFrameClass frame = new YourJFrameClass(); }
That should get you started.
The easiest method is to press Shift+F11 (Clean and Build Project), then hit F6 to run it.
It refreshes Netbeans appropriately and finds your main without all the manual labor; and if you have multiple mains, it will give you the option to select the correct one.
Check if your JFrame Class has this method:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new YOURFORMNAME().setVisible(true);
}
});
}
The setVisibile(true) allows the form to be visible to the user. The main method is used to define the entry point for the application therefore this method is needed to run the JFrame

Visual Studio Code Java Extension Pack starting template (vscode)

I just changed my editor to vscode and loving it since then.
I am using Java extension pack (specifically project manager I guess), and when I create a new project it generates lib, src/App.java. nothing special.
Is there anyway I can change this default files, folders(package)?
Also, is there anyway I can change this default App.java code?
public class App {
public static void main (String[] args) {
System.out.println("Hello World!!");
}
}
When you open Command Palette and choose Java:Create Java Project, click No build tools will generate a template project:
and this function is provided by extension Project Manager for Java:
To customize templates, find the location of Project Manager for Java and change the default ones. On my machine, it's C:\Users\...\.vscode\extensions\vscjava.vscode-java-dependency-0.13.0. There may be many other extensions in your folder extensions and you can search it with keyword java and its version number. For me, the version number is 0.13.0, and you can check it in extension marketplace.
Turn to the directory C:\Users\...\.vscode\extensions\vscjava.vscode-java-dependency-0.13.0, open the folder images to check if you find the correct one. The templates is there and under the directory templates\invisible-project\src, you can see App.java. Customizing templates like adding and editing files is achievable.

intellij java desktop application how to use resources

I am busy with a java desktop application in intellij. I am struggling to get a image to display in a JLabel
Here is my current code (in a class extending JPanel):
icon = new ImageIcon(getClass().getResource("resources/icon.png"));
lblIcon.setIcon(icon);
Here is a picture of my project structure:
The image is in the resources directory and the screen in in the screens\jpanel.java directory
If I rememeber correctly, IntelliJ automatically adds the contents of the folder marked as "resources root" to the root of your compiled project.
getClass().getResource("...") expects a path relative to your classloader, thus you simply need to provide the name of your image in this case:
icon = new ImageIcon(getClass().getResource("/icon.png"));
lblIcon.setIcon(icon);

How to include images which are used in my code in my jar file in java swing? [duplicate]

I made a java application.and bundled all classes in jar file..wen i run the project from netbeans my app is running successfully..but wen i place my .jar file at another place and run from there..i am not getting the icons used by my application..In the code i get my icons from images directory present in project folder.
Now,i wanted to know how can we present these image files to the end user (like we present .jar file).Thanks in advance
It seems like there are two questions here:
How do I get NetBeans to include an image file in the jar produced when I build my project?
How do I access an image file from a jar?
This answer applies to NetBeans 6.8 and addresses both of the subquestions.
Assume that you have an ant based Java Application Project.
Here is the 'Files' view of the project
JP
+ images
+ test.jpg
+ nbproject
+ src
+ jp
+ Main.java
+ test
+ build.xml
+ manifest.mf
Inside your Main.java you have code like this:
public static void main(String[] args) throws IOException {
// find the file in the file system.. probably not a good idea
File f = new File("images/test.jpg");
System.out.println(f.getCanonicalPath()+" "+f.exists());
When you run this project from inside NB you get this output:
/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
When you run the code packed into the jar, you get something like this:
bash-3.2$ pwd
/export/home/vkraemer/nbhg/web-main
bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar
/export/home/vkraemer/nbhg/web-main/images/test.txt false
To get something better when the jar is executed, you need to do the following:
Add the images directory as a source root for you project.
Right click on the project and select the Properties item. A dialog will appear.
Select 'Sources' in the list that is on the left side of the dialog. This will change the content of the panel on the right side of the dialog.
Press the 'Add Folder...' button that appears next to the 'Source Package Folders' table. A FileChooser will appear.
Use this chooser to select the images folder and press the OK button. An entry for the images folder will be added table.
Use the OK button on the Project Properties dialog to accept the changes and dismiss the dialog.
Change your code to use Class.getResource().
public static void main(String[] args) throws IOException {
// find the file in the file system.. probably not a good idea
File f = new File("images/test.jpg");
System.out.println(f.getCanonicalPath()+" "+f.exists());
URL url = Main.class.getResource("/test.jpg");
System.out.println(url);
When you run the project from inside the IDE, you should see something like this:
/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg
When you run the code packed into the jar, you will get something like this:
bash-3.2$ pwd
/export/home/vkraemer/nbhg/web-main
bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar
/export/home/vkraemer/nbhg/web-main/images/test.jpg false
jar:file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar!/test.jpg
After you get the URL for the test.jpg file, you can use ImageIcon(URL) to create the icon
For a Maven Project in NetBeans 6.9, put your filen into Resources in the "Other Sources" folder of the Project.
And be sure you dont forget the leading "/"

Java IDE - Eclipse, Importing resources

Hopefully someone can help me with something that is probably very simple. I'm new to Java coding (2 weeks in), and using Eclipse IDE under Linux. I'm currently in the process of creating a JFrame application, everything so far is going well.
I have one little snag though - I have included a set of Icons and assigned them to a JLabel, and have them displayed. Upon exporting an Executable JAR, they are not in the JAR as a resource. If I open the JAR file, I can see the Images, in the ROOT of the JAR, not organized in their respective folders. (icons/, etc).
TL;DR - How do I import resources, in their folders, into a JAR as a resource.
public void drawCategoryIcons() {
for (int i = 0; i < aspCategories.length; i++) {
Icon pcIcon = new ImageIcon(getClass().getResource( "/icons/" + cats[i]));
aspCategories[i] = new JLabel("", JLabel.CENTER);
aspCategories[i].setIcon(pcIcon);
panel.add(aspCategories[i], "w 200, center");
}
}
If I RUN the project within Eclipse, everything works as it should. Exporting it, I get tons of errors.
You have probably created the icons directory as separate source folder in the eclipse project (it has an icon composed of a folder icon and a package icon then). Source folders are just logical elements during development (so you can separate modules of your application logically), they are not part of the exported application. Only everything inside source folders is exported.
Therefore a simple workaround might be to instead create a package "icons" in your other source folder (typically called "src"). Those packages will exist as sub folders after export.

Categories

Resources