Issue
Here is my simple application:
public class MainFrame extends JFrame{
private Dimension size;
private JPanel buttonPanel;
private JButton button;
public MainFrame() throws ClassNotFoundException{
super("Applikacja1");
try {
UIManager.setLookAndFeel("com.formdev.flatlaf.FlatDarkLaf");
} catch (Exception ex) {
}
JFrame.setDefaultLookAndFeelDecorated(true);
size = new Dimension(350, 530);
setPreferredSize(size);
setMinimumSize(size);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
pack();
//setting button
button = new JButton("Start programu");
button.setVisible(true);
this.add(button);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
}
What I need to do in order to change look of this program to FlatLaf and use their libraries? What i need to download from internet? I tried download whole code(from here https://www.formdev.com/flatlaf/) next add it as libraries to netbeans and it didn't work. Can someone do a little totally-newbie tutorial how to do it?
Solution
I am not an expert on this, but here are some instructions:
Option 1: download files manually and install them
You can download and install the following files:
- Binary file: this is the bytecode executed by the Java Virtual Machine. This file in your case is called flatlaf-1.1.jar. This is the latest release so far. Those files usually (as in this case) consist of just the library name and the verison. This is the absolute mandatory file needed to be able to use the library, if you select this option.
- JavaDoc file: this is the file containing the documenting comments of each class, method, field and generally whatever documentation the developers provide with their code. This file in your case is called flatlaf-1.1-javadoc.jar. Usally the developers provide an online documentation which is even downloadable in HTML format, but downloading this file beforehand, will let you see the docs any time in your IDE (ie project editor, such as NetBeans or IntelliJ) even when offline. So this file is a very good one, but not an absolute mandatory one.
- Sources file: this is the file containing the source code of each class, method, field and generally the code of the library, bundled in a jar file. This file in your case is called flatlaf-1.1-sources.jar. If you connect your project with this source's file you get the benefit of seeing the actual code (not Java bytecode as is the case of the binary, but Java plain text source code) that the library is implemented in. This file then is again not an absolute mandatory one, but it is a good one to consult if something is not clear enough (or is missing) from the documentation.
All the files I mentioned so far can be found where the site guides you to find them, for example the latest release files of your desired library can be found all together here. You can find this link as follows: first navigate to the FlatLaf's home page, then scroll a bit down on this page to find the section Download which gives you another link, the one in the Maven repository (specifically this one). There you can find instructions on how to link the library to your project and, of course, download the necessary files. In this case, there are two buttons on the top right, the one called Browse and the other called Downloads. The Downloads button simply drops down a list of all assets (ie files) that you can download. The Browse button navigates you to the directory of the server where you can find all the files presented with the drop down list of the Downloads button, so let's go there in order to see all the available files...
If you went there, you can see many more files. Most of them are imporant for security, which something I did not cover yet. It is always important to verify the integrity of the files you download, because there are security risks if you don't. You don't want the library you download to be a malicious copy provided by a phising site for example. So you need to verify them together with the link you are visiting. Admitedly, I am not doing this very often, when downloading libraries, but I feel that, my neglectness of doing it, does not make it something I should not mention. So I am warning you: be aware of what you are downloading.
Let's have a break to see what all those files (most likely) are:
- jar files are the main files you need to make things work. They are the files I mentioned above.
- pom file I think is a settings' file for Apache Maven build system.
- asc files are the PGP signatures.
- md5, sha1, sha256 and sha512 are the hashes. If you are familiar, you know what I am saying. So you should take a step back and verify the signatures and hashes of the main (jar) files you just downloaded.
Best way to verify the integrity in my opinion is to simply download the source code, verify it yourself and via the provided resources, then build form source (if you trust your compiler) and install. But that's not so simple, nor time preserving. All those links I provide may contain malicious content: I just cannot know yet because I haven't spent the necessary time to verify. So continue at your own risk, as they usually say. Also note I am getting a warning when downloading jar files generally because "they are not trusted" as the message says. Moving on...
Option 2: sync with a build system
If you have a system to automate the building process of your project (instead of compiling each and every Java source code file of your project with javac
command line utility), such as Apache Maven, Gradle, Apache Ant and others, then you have the option to include a so called dependecy of the library in your project and let the build system synchronize (ie download and include the library in the build process). This is at least what happens with Gradle which I am a little familiar with. As for the others I am not really familiar, so this is where I stop talking for them. Just note that there are several instructions and tutorials and documentation about how to use them, either via command line directly, or via your IDE. Then the library developers as far as I know usually they let you know how to include the corresponding dependency in the build tool's settings file.
Option 3: build from source, then install
You can download the source code (of this software at least) from GitHub, or sometimes by FTP sites or generally from some online server of some sort which provides the corresponding content, and build it. The source code of your desired library is located here, as you have probably found out, judging from the question.
Installing
So now that you downloaded (and verified), or built what you needed, the actual installation is as follows... I am going to demonstrate for the NetBeans 8.2 IDE with Java 8 (the minimum supported version for FlatLaf), but the steps I guess are generally the same for each IDE and version. If they are not, you can be more specific about your environment and then we can talk again.
- Include the compiled library (flatlaf-1.1.jar) in your project, like so: right click the project from the Projects tab of NetBeans (usually to the left of the IDE) and go to the bottom most button named Properties and click it. In the dialog that pop's up, from the Categories section, to the left of the dialog, select the category named Libraries. Select the tab Compile from the middle, and click on Add JAR/Folder to the right. Then go select your library file and click Open to include it. That's it. You are basically now ready to start including the FlatLaf's classes to your project, because this file was the absolute mandatory one, as I said above.
- Next, the JavaDoc file. Create a class in the project and use a class from the library by importing it and declaring a reference for example of that imported class. Press CTRL+SPACE on the class name... A dialog pops up saying there is no JavaDoc attached, and gives you a place to click which says "Attach JavaDoc..." or something similar. Click it, and then click on the Add ZIP/Folder button, select the JavaDoc jar file and confirm your choice. This is now ready.
- Finally the Sources' file. As you have ready the class reference in your code, left click the class name with holding the CTRL key. This will get you to the source code. You will see that only the methods names are shown and every method body says Compiled Code and if you unfold/expand it, then you see the bytecode in comments. That means the sources are not attached. You are seeing the bytecode of the compiled jar file. On the top right of the editor, there is now a button which reads Attach Sources.... Click it, and then a dialog pops up. Click the button Add Zip/Folder..., navigate and select the flatlaf-1.1-sources.jar file and you can now see the source code of the library.
Building a sample application
You can consult the GitHub page, or the the project's main page which guides you through. Basically you can try the following:
import com.formdev.flatlaf.FlatDarkLaf;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Main {
private static void createAndShowGUI() {
FlatDarkLaf.setup(); //Must be called first of all Swing code as this sets the look and feel to FlatDark.
final JPanel panel = new JPanel(); //FlowLayout.
panel.add(new JButton("FlatDarkLaf button!"));
panel.add(new JTextField("FlatDarkLaf text field!"));
final JFrame frame = new JFrame("FlatDarkLaf test.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(Main::createAndShowGUI);
}
}
Final note: you must apply your LAF before any Swing component creation. In the code in your question seems like you are not, because you first create the JFrame
(via constructor's super
call) and then you install the LAF in a manner which is also not recommended.
Answered By - gthanop