Month: June 2022

  • How to generate UUID in dart / flutter

    How to generate UUID in dart / flutter

    Very often, we come across the need to have UUID. UUIDs are perfect for giving unique ID to objects and resources. In this tutorial, we will see how to create UUID in dart for a flutter app.

    Generating the UUID

    Step 1: Add the uuid library to your project

    To create UUIDs, we need to add the uuid library into the project. It can be added by inserting the following entry into your project’s pubsec.yaml file.

      uuid: 3.0.6
    

    Step 2: Import “uuid/uuid.dart” and use it

    Now, let’s use the added library to create UUID. First, import the library and then use it. Simply create an object of the Uuid() class and then use the provided v1(), v2(), v3(), v4(), or v5() function based on the UUID version you need. All the standard UUID versions are supported in this library.

    import 'package:uuid/uuid.dart';
    ...
    createUuid() {
      const uuid = Uuid();
      //Create UUID version-4
      return uuid.v4();
    }
    
    import 'package:uuid/uuid.dart';
    
    createV5Uuid() {
      return Uuid().v5(Uuid.NAMESPACE_URL, 'www.genuinecoder.com');
    }
    

    If you are looking for more customized UUIDs with more control over the random data, have look into the library example section.

    Create Unique ID without additional libraries

    Now, let’s say you don’t need UUID exactly, but want unique auto-generated IDs. Then you can proceed with the UniqueKey class.

    UniqueKey() will return an alphanumeric string (usually 5 char length).

    createUUID() {
      return UniqueKey();
    }
    

    Conclusion

    In this tutorial, we have seen how to generate UUID with flutter. The UUID library supports all the standard variations and is straightforward to use. If you have liked this article, have a look into the other flutter tutorials I have written.

  • Flutter ElevatedButton Tutorial (2022)

    Flutter ElevatedButton Tutorial (2022)

    In this tutorial, we will explore the ElevatedButton widget in Flutter. ElevatedButtons are, as the name suggests, buttons with a depth or elevation. They will stand on top of the container with a shadow. So they have a different appearance compared to the flat button TextButton.

    ElevatedButton without Icon

    There are two constructors available for the ElevatedButton, one with icon and one without icon. Let’s first see how to use the one without icon.

    There are two mandatory fields in the constructor, onPressed and child.

    const ElevatedButton({
        Key? key,
        required VoidCallback? onPressed,
        VoidCallback? onLongPress,
        ValueChanged<bool>? onHover,
        ValueChanged<bool>? onFocusChange,
        ButtonStyle? style,
        FocusNode? focusNode,
        bool autofocus = false,
        Clip clipBehavior = Clip.none,
        required Widget? child
    })
    

    Let’s see how we can create this button with an example code.

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const ElevatedButtonExample());
    }
    
    class ElevatedButtonExample extends StatelessWidget {
      const ElevatedButtonExample({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
              appBar: AppBar(title: const Text('ElevatedButton Example')),
              body: const ElevatedButtonWidget()),
        );
      }
    }
    
    class ElevatedButtonWidget extends StatelessWidget {
      const ElevatedButtonWidget({Key? key}) : super(key: key);
    
      //Button click handler: Show snackbar
      handleButtonClick(BuildContext context) {
        const snackBar = SnackBar(
          content: Text("ElevatedButton Clicked!"),
        );
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      }
    
      @override
      Widget build(BuildContext context) {
        return Center(
          //Create Elevated Button
          child: ElevatedButton(
              //Handle button press event
              onPressed: () {
                handleButtonClick(context);
              },
              //Contents of the button
              child: const Text("Click ElevatedButton!")),
        );
      }
    }
    

    The above code generates the following output. Note that the color of the button is taken from the default theme. We will see how to customize the colors and fonts in the next section.

    Simple Flutter ElevatedButton
    Simple Flutter ElevatedButton

    Customize button style and add an icon

    Now, let’s see how to customize the button and add an icon to it. The ElevatedButton class has a separate constructor for creating the button with an icon. We will make use of that.

    For styling the button, ElevatedButton.styleFrom() to update the values from the default theme.

    factory ElevatedButton.icon({
        Key? key,
        required VoidCallback? onPressed,
        VoidCallback? onLongPress,
        ValueChanged<bool>? onHover,
        ValueChanged<bool>? onFocusChange,
        ButtonStyle? style,
        FocusNode? focusNode,
        bool? autofocus,
        Clip? clipBehavior,
        required Widget icon,
        required Widget label,
    })
    

    For the button style customization, we will do the following.

    • Add a download icon
    • Change the font size to 22dp
    • Add 20px padding on all sides
    • Set the foreground color to RED
    • Set the background color to YELLOW

    Now, let’s see we can do it with an example code.

    @override
    Widget build(BuildContext context) {
        return Center(
            //Create Elevated Button
            child: ElevatedButton.icon(
                //Handle button press event
                onPressed: () {
                  handleButtonClick(context);
                },
                //Contents of the button
                style: ElevatedButton.styleFrom(
                  //Change font size
                  textStyle: const TextStyle(
                      fontSize: 22,
                  ),
                  //Set the background color
                  primary: Colors.yellow,
                  //Set the foreground (text + icon) color
                  onPrimary: Colors.red,
                  //Set the padding on all sides to 30px
                  padding: const EdgeInsets.all(30.0),
                ),
                icon: const Icon(Icons.send_rounded), //Button icon
                label: const Text("Click Text Button!")), //Button label
        );
    }
    

    This will generate the following output. As you can see below, the

    ElevatedButton with icon and theme customization
    ElevatedButton with icon and theme customization

    More button style customization

    Let’s go through all the style customization flutter provides for the ElevatedButton. All the following mentioned customizations can be used within ElevatedButton.styleFrom(), Just like we did in the above example for padding, color etc.

    Set the disabled state color (applies for button label and icon)
    onSurface: Colors.colorOfYourChoice
    
    Set the shadow/elevation color
    shadowColor: Colors.colorOfYourChoice
    
    Set the elevation.

    Higher the elevation, more the shadow spread.

    elevation: 6.0
    
    Specify minimum size of the button
    //First parameter is width, second is height.
    minimumSize: Size(20, 40)
    
    Set the button border
    //Setting solid 1px red color border
    side: BorderSide(color: Colors.red, width: 1.0, style: BorderStyle.solid),
    //Setting sold 1px red color border
    
    Set button shape
    //Setting button shape to circle with radius of 35.
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(35.0)), 
    
    Mouse cursor when button is enabled
    //Set mouse cursor to click (hand cursor)
    enabledMouseCursor: SystemMouseCursors.click,
    //Set mouse cursor to text (I cursor)
    
    Button with custom cursor, rounded corners, border, etc.
    Button with custom cursor, rounded corners, border, etc.
    Mouse cursor when button is disabled
    //Hide mouse cursor when hovered over disabled ElevatedButton
    disabledMouseCursor: SystemMouseCursors.none,
    
    Set visual density
    visualDensity: VisualDensity(horizontal: 0.0, vertical: 0.0)
    
    Set tap target size
    tapTargetSize: MaterialTapTargetSize.padded
    
    Set button’s default animation duration for shape change and elevation change
    //Setting the animation duration to 100ms
    animationDuration: Duration(milliseconds: 100)
    
    Enable/disable button click feedback

    Enable or disable vibration (haptic) and sound (acoustic) feedbacks.

    enableFeedback: true
    
    Alighnment of button’s text
    alignment: Alignment.bottomCenter
    
    Background color of the button
    primary: Colors.yourPrefferedBackground
    
    Foreground color of the button icon + text
    onPrimary: Colors.yourFavouriteForegroundColor
    
    Change font properties
    //Set font size and font weight
    textStyle: const TextStyle(
           fontSize: 20,
           fontWeight: FontWeight.bold,
    ),
    
    Set button padding
    //Set the padding on all sides to 20px
    padding: const EdgeInsets.all(20.0),
    

    Conclusion

    In this chapter, we have learned how to make use of the ElevatedButton widget in Flutter. We have seen how to create one with icon, without icon and went through all the style customizations we can do for the button. If you liked this article, you might be interested in the other Flutter tutorials I have written.

  • Java JFrame Tutorial

    Java JFrame Tutorial

    JFrame is a java swing container that can be used to create application windows. JFrames are the top most container in the swing applications. All the GUI components like labels, comboboxes are added into the JFrame. In this tutorial, we will learn how to make use of the Java JFrames to create swing applications and see how to manage the JFrame windows.

    Simple application with JFrame

    First, let’s create a simple application. We will add a button to the JFrame container.

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class JFrameTutorial {
    
      public static void main(String[] args) {
        //Create a JFrame
        JFrame jFrame = new JFrame("My JFrame Window!");
        //Set JFrame size
        jFrame.setSize(300, 200);
    
        //Create a JPanel to contain our button
        JPanel panel = new JPanel();
        //Add button to the panel
        panel.add(new JButton("Click Me"));
    
        //Attach panel to JFrame
        jFrame.add(panel);
    
        //Show the JFrame
        jFrame.setVisible(true);
      }
    }
    

    This will create the following output
    Simple JFrame Window

    Even though the above code works, it is not perfect. If you run the above code, you will have some questions. Let me make things clear for you.

    Question: Even after the JFrame is closed, the application is still running in the background. Why does my application still run after closing the main window?

    This is because you haven’t specified what to do when the JFrame is closed. Remember that in a real-world application, there will be many windows, so many JFrames. By default, if the JFrame was exiting/killing the application when it is closed, then this would cause countless issues. Because each window closing will cause the application to terminate by default.

    To prevent this, when the close button is pressed, the JFrame just gets hidden and will not exit the application fully. To exit the application on window close, add the following line into the code to set setDefaultCloseOperation.

    public static void main(String[] args) {
      JFrame frame = new JFrame("My JFrame Window!");
      frame.setSize(300, 200);
      JPanel panel = new JPanel();
      panel.add(new JButton("Click Me"));
      frame.add(panel);
    
      //Close the application when the window is closed
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
      frame.setVisible(true);
    }
    

    Question: The JFrame window is opening at the edge of the screen. How to make JFrame center on screen?

    By default, the Window will appear at the edge of the screen (most of the time, in most of the common operating systems). To make the JFrame window appear at the dead center of the screen, you need to set the window’s location-relative-to null. It can be done as follows.

    //Set location of the JFrame relative to null to get it open at the center of the screen
    frame.setLocationRelativeTo(null);
    

    Set Icon for JFrame Window

    Very often, we will have to set our custom icon to the application window. With JFrame, this is possible with the method JFrame#setIconImage(Image). This will make the icon appear on the OS taskbar as well.

    //Read the image you want to set as icon
    Image image = ImageIO.read(JFrameTutorial.class.getResourceAsStream("/logo.png"));
    //Set the image icon to window
    frame.setIconImage(image);
    
    JFrame with icon
    JFrame window with icon shown on taskbar

    If you would like to learn how to set up a system tray icon, have a look into this tutorial on setting up of tray-icon with menu in Java.

    import java.awt.Image;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class JFrameTutorial {
      public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("My JFrame Window!");
        frame.setSize(300, 200);
        JPanel panel = new JPanel();
        panel.add(new JButton("Click Me"));
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setIconImage(ImageIO.read(JFrameTutorial.class.getResourceAsStream("/logo.png")));
        frame.setVisible(true);
      }
    }
    

    Close/Hide JFrame Programmatically

    Let’s say we want to close a JFrame programmatically. This can be done with JFrame#setVisible(false) method followed by a JFrame#dispose() method to clear the object. There is a deprecated function JFrame#hide(), but you should never use it as it has been deprecated from Java 1.5.

    Simply set the visibility of the frame to false, and it will hide/close the window. Then call dispose() to release the system resources used by it.

    //Set visibility to false for closing the JFrame
    frame.setVisible(false);
    //Dispose the frame to release resources
    frame.dispose();
    

    Another method for closing

    Another easy method for closing the JFrame is by simulating the close button click event. As we have already seen, pressing the X button will close the window. We can simply simulate the close button click to close the JFrame itself. We can do that as follows

    jFrame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
    

    Maximize and minimize JFrame Programmatically

    Now, let’s say we came across a requirement where we need to maximize a JFrame window programmatically. There is a function called JFrame#setExtendedState(int state) for programmatically maximizing and minimizing. Let’s see how we can do that with some code snippet.

    private void minimizeJFrameWindow(JFrame jFrame) {
      //Minimize to task bar
      jFrame.setExtendedState(JFrame.ICONIFIED);
    }
    
    private void maximizeJFrameWindow(JFrame jFrame) {
      //Maximize the window
      jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
    
    private void maximizeJFrameWindowWidth(JFrame jFrame) {
      //Maximize the window only in the width (x-axis). Height will remain unchanged
      jFrame.setExtendedState(JFrame.MAXIMIZED_HORIZ);
    }
    
    private void maximizeJFrameWindowHeight(JFrame jFrame) {
      //Maximize the window only in the height (y-axis). Width will remain unchanged
      jFrame.setExtendedState(JFrame.MAXIMIZED_VERT);
    }
    

    Conclusion

    In this tutorial, we have learned how to make use of JFrame to create windows in Java. We have seen how to create it, attach icon, center on screen, maximize and minimize etc. If you have liked this article, you might also like the following articles I have written.

  • Flutter TextButton Widget – Usage and Examples

    Flutter TextButton Widget – Usage and Examples

    In this tutorial, we will explore the Button widgets available in the Flutter. Compared to the previous version Flutter2, Flutter3 has updated the button widget. We will see how to use the TextButton widget and customize it. If you want to learn how to create your first flutter project, head over to this tutorial on making your first flutter project.

    TextButtons, as the name suggests, are widgets with simple text that can be interacted with a click. It uses the theme “TextButtonTheme”. These buttons are useful for action in dialogs, snackbars and cards. The material design 3 specifications for the TextButton can be found in here.

    TextButton without Icon

    Constructor of the TextButton given below. The onPressed and child properties are mandatory.

    TextButton({
        Key? key,
        required VoidCallback? onPressed, //onPressed is mandatory
        VoidCallback? onLongPress,
        ValueChanged<bool>? onHover,
        ValueChanged<bool>? onFocusChange,
        ButtonStyle? style,
        FocusNode? focusNode,
        bool autofocus = false,
        Clip clipBehavior = Clip.none,
        required Widget child,            //child is mandatory
    })
    

    Let’s see how we can use the button on a sample application.

    Flutter TextButton without icon
    Flutter TextButton with Snackbar message on press action
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const TextButtonExample());
    }
    
    class TextButtonExample extends StatelessWidget {
      const TextButtonExample({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
              appBar: AppBar(title: const Text('TextButton Example')),
              body: const TextButtonWidget()),
        );
      }
    }
    
    class TextButtonWidget extends StatelessWidget {
      const TextButtonWidget({Key? key}) : super(key: key);
    
      //Button click handler: Show snackbar
      handleButtonClick(BuildContext context) {
        const snackBar = SnackBar(
          content: Text("Text Button Clicked!"),
        );
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      }
    
      @override
      Widget build(BuildContext context) {
        return Center(
          //Create Text Button
          child: TextButton(
            //Handle button press event
            onPressed: () {
              handleButtonClick(context);
            },
            //Contents of the button
            child: const Text("Click Text Button!")
          ),
        );
      }
    }
    

    Customize the TextButton

    TextButton uses the theme TextButtonTheme. Let’s see how we apply the following style updates for the Button.

    1. Change the font size to 20: Use textStyle
    2. Make the button font bold: Use textStyle
    3. Change the text color to RED: Use primary
    4. Add padding of 20px on all sides: Use padding
    @override
    Widget build(BuildContext context) {
      return Center(
        //Create Text Button
        child: TextButton(
          //Handle button press event
            onPressed: () {
              handleButtonClick(context);
            },
            style: TextButton.styleFrom(
                //Change font size and weight
                textStyle: const TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
                //Set the foreground color
                primary: Colors.red,
                //Set the padding on all sides to 20px
                padding: const EdgeInsets.all(20.0),
            ),
            //Contents of the button
            child: const Text("Click Text Button!")),
      );
    }
    
    Customized Flutter 3 TextButton
    Customized Flutter TextButton

    TextButton with Icon

    Now, let’s see how we can create the TextButton with an icon. There is a special factory constructor available for creating TextButton with an Icon. Let’s have a look into the factory constructor.

    factory TextButton.icon({
        Key? key,
        required VoidCallback? onPressed,  //onPressed is mandatory
        VoidCallback? onLongPress,
        ValueChanged<bool>? onHover,
        ValueChanged<bool>? onFocusChange,
        ButtonStyle? style,
        FocusNode? focusNode,
        bool? autofocus,
        Clip? clipBehavior,
        required Widget icon,             //Icon is mandatory
        required Widget label,            //Label is mandatory
    })
    

    As it can be seen from the above constructor, we need to provide an icon, label and onPressed values.

    Let’s see how we can do this with an example code.

    @override
    Widget build(BuildContext context) {
      return Center(
        //Create Text Button
        child: TextButton.icon(
            onPressed: () {
              handleButtonClick(context);
            },
            style: TextButton.styleFrom(
              //Change font size and weight
              textStyle: const TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
              //Set the foreground color
              primary: Colors.deepOrange,
              //Set the padding on all sides to 20px
              padding: const EdgeInsets.all(20.0),
            ),
            //Set the icon
            icon: const Icon(Icons.send_rounded),
            //Set the label
            label: const Text("Click Text Button!")),
      );
    }
    

    It generates the following output. As you can see, the icon is added to the button, the label is set and the customized styling is applying.

    TextButton With Icon
    Flutter TextButton with an icon

    Conclusion

    In this tutorial, we have learned how to use the TextButtons in flutter. We have seen how to use it, customize it, and add icons to it. If you liked this article, you might want to check out my other Flutter tutorials.

  • How to get all the image files from a folder in Java?

    How to get all the image files from a folder in Java?

    Filter image files from a folder in Java

    Let’s see how to filter-out only image files from a directory in Java. We will write code to iterate through all the files present in a directory, and then filter out only the image files by their extension. In the following example snippet, .jpg, .png, .gif and .webp images will be selected from the given folder.

    • Pass the directory from which images should be found as the parameter
    • Specify the image extension for the required images in the list supportedImageExtensions
    private List<File> getAllImageFilesFromFolder(File directory) {
      //Get all the files from the folder
      File[] allFiles = directory.listFiles();
      if (allFiles == null || allFiles.length == 0) {
        throw new RuntimeException("No files present in the directory: " + directory.getAbsolutePath());
      }
    
      //Set the required image extensions here.
      List<String> supportedImageExtensions = Arrays.asList("jpg", "png", "gif", "webp");
    
      //Filter out only image files
      List<File> acceptedImages = new ArrayList<>();
      for (File file : allFiles) {
        //Parse the file extension
        String fileExtension = file.getName().substring(file.getName().lastIndexOf(".") + 1);
        //Check if the extension is listed in the supportedImageExtensions
        if (supportedImageExtensions.stream().anyMatch(fileExtension::equalsIgnoreCase)) {
          //Add the image to the filtered list
          acceptedImages.add(file);
        }
      }
    
      //Return the filtered images
      return acceptedImages;
    }
    
  • Create animated GIF from multiple images in Java

    Create animated GIF from multiple images in Java

    In this tutorial, we will learn how to programmatically create animated GIF by combining multiple images in java easily. We will make use of the library gifencoder for this purpose.

    Add the ‘gifencoder’ library to your project

    If you are using Gradle, add the following dependency into your build.gradle file.

    // https://mvnrepository.com/artifact/com.squareup/gifencoder
    implementation group: 'com.squareup', name: 'gifencoder', version: '0.10.1'
    

    If you are using the maven, add the following entry into your pom.xml file.

    <!-- https://mvnrepository.com/artifact/com.squareup/gifencoder -->
    <dependency>
        <groupId>com.squareup</groupId>
        <artifactId>gifencoder</artifactId>
        <version>0.10.1</version>
    </dependency>
    

    If you are using Java 9+ with modules, add the following line into your module-info.java file.

    requires com.squareup.gifencoder;
    

    Build GIF from multiple image files

    First, let’s build our GIF from 4 image files. The steps for preparing the GIF from images as follows.

    1. Convert the BufferedImages to int[][] pixel array
    2. Provide the image pixel array into the encoder
    3. Encode all the pixel array-based images into GIF image
    import com.squareup.gifencoder.FloydSteinbergDitherer;
    import com.squareup.gifencoder.GifEncoder;
    import com.squareup.gifencoder.ImageOptions;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.concurrent.TimeUnit;
    import javax.imageio.ImageIO;
    
    public class JavaGifTutorial {
    
      public static void main(String[] args) throws Exception {
        JavaGifTutorial gifTutorial = new JavaGifTutorial();
        gifTutorial.createAnimatedGif();
      }
    
      private void createAnimatedGif() throws Exception {
        File image1 = new File("gifencoder/image1.jpg");
        File image2 = new File("gifencoder/image2.jpg");
        File image3 = new File("gifencoder/image3.jpg");
        File image4 = new File("gifencoder/image4.jpg");
    
        //The GIF image will be created with file name "my_animated_image.gif"
        try (FileOutputStream outputStream = new FileOutputStream("my_animated_image.gif")) {
          ImageOptions options = new ImageOptions();
    
          //Set 500ms between each frame
          options.setDelay(500, TimeUnit.MILLISECONDS);
          //Use Floyd Steinberg dithering as it yields the best quality
          options.setDitherer(FloydSteinbergDitherer.INSTANCE);
    
          //Create GIF encoder with same dimension as of the source images
          new GifEncoder(outputStream, 500, 313, 0)
              .addImage(convertImageToArray(image1), options)
              .addImage(convertImageToArray(image2), options)
              .addImage(convertImageToArray(image3), options)
              .addImage(convertImageToArray(image4), options)
              .finishEncoding(); //Start the encoding
        }
      }
    
      /**
       * Convert BufferedImage into RGB pixel array
       */
      public int[][] convertImageToArray(File file) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(file);
        int[][] rgbArray = new int[bufferedImage.getHeight()][bufferedImage.getWidth()];
        for (int i = 0; i < bufferedImage.getHeight(); i++) {
          for (int j = 0; j < bufferedImage.getWidth(); j++) {
            rgbArray[i][j] = bufferedImage.getRGB(j, i);
          }
        }
        return rgbArray;
      }
    }
    

    GIF created using Java

    Create GIF from all the images from a folder

    Now, let’s see how we can improve the above code to take all the image files from a folder and use it to make an animated GIF. This way, you can simply put all the images you want to include the GIF image and run the code, no need to mention the individual images separately.

    To achieve this, we need to add code for filtering only image files from a directory.

    How to take only image files from a directory in Java?

    Let’s see how to filter-out only JPEG and PNG files from a directory in Java. We will write code to iterate through all the files present in a directory, and then filter out only the image files by their extension.

    private List<File> getAllImageFilesFromFolder(File directory) {
      //Get all the files from the folder
      File[] allFiles = directory.listFiles();
      if (allFiles == null || allFiles.length == 0) {
        throw new RuntimeException("No files present in the directory: " + directory.getAbsolutePath());
      }
    
      //Filter out only image files
      List<File> acceptedImages = new ArrayList<>();
      for (File file : allFiles) {
        String fileExtension = file.getName().substring(file.getName().lastIndexOf(".") + 1);
        if (fileExtension.equalsIgnoreCase("jpg") || fileExtension.equalsIgnoreCase("png")) {
          acceptedImages.add(file);
        }
      }
    
      //Return the filtered images
      return acceptedImages;
    }
    

    Create GIF from all the images in the folder

    Now, let’s make use of the above function to get all the images from a folder and encode it into a GIF image.

    //Trimmed code. Functions and imports used on the first section is excluded to keep the code compact
    
    private void createAnimatedGifFromFolder(String path) throws Exception {
      //The GIF image will be created with file name "gif_from_folder.gif"
      try (FileOutputStream outputStream = new FileOutputStream("my_animated_image.gif")) {
        //Create GIF encoder with same dimension as of the source images
        GifEncoder encoder = new GifEncoder(outputStream, 1920, 1200, 0);
    
        //Get all the image files
        List<File> imageFiles = getAllImageFilesFromFolder(new File(path));
    
        //Make sure that at least one image is present
        if (imageFiles.isEmpty()) {
          throw new RuntimeException("No image files present!");
        }
    
        //Add all the images to the GifEncoder
        ImageOptions options = new ImageOptions();
        for (File imageFile : imageFiles) {
          encoder.addImage(convertImageToArray(imageFile), options);
        }
    
        //Finish encoding and create the file
        encoder.finishEncoding();
      }
    }
    
    private List<File> getAllImageFilesFromFolder(File directory) {
      File[] allFiles = directory.listFiles();
      List<File> acceptedImages = new ArrayList<>();
      for (File file : allFiles) {
        String fileExtension = file.getName().substring(file.getName().lastIndexOf(".") + 1);
        if (fileExtension.equalsIgnoreCase("jpg") || fileExtension.equalsIgnoreCase("png")) {
          acceptedImages.add(file);
        }
      }
      return acceptedImages;
    }
    

    That’s all. The function createAnimatedGifFromFolder(String folder) will go through all the JPG and PNG images available inside it and then use it to make the GIF animated image.

    Conclusion

    In this tutorial, we have learned how to create an animated GIF image a set of images in Java. Java GIF creation is done with the help of the gifencoder library. If you have liked this article, you might want to check out some of the other articles I have written.

  • How to create excel files in Java

    How to create excel files in Java

    In this tutorial, we will explore the Java spreadsheet support and learn how to create Microsoft Excel .xlsx spreadsheet files using java program. For handling Excel files, we need to make use of the well known Apache POI library. This tutorial has been updated for Java 17 and work with Apache POI modules.

    Add Apache POI library into your project

    The first step is adding the Apache POI library into your project.

    If you are using Gradle, add the following dependency into your build.gradle file.

    // https://mvnrepository.com/artifact/org.apache.poi/poi
    implementation group: 'org.apache.poi', name: 'poi', version: '5.2.2'
    

    If you are using Maven, add the following dependency into your pom.xml file.

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.2</version>
    </dependency>
    

    Issue with Java modules

    Q. I am getting the exception “java.lang.module.FindException: Module SparseBitSet not found, required by org.apache.poi.poi”. How can I fix it?
    Another similar exception you might come across is: “java.lang.module.FindException: Module commons.math3 not found, required by org.apache.poi.poi”.

    Since you are using Java 9+ with modules, you need to set the module configuration correctly. First, make sure that your module-info.java contains the following lines to have your application access the org.apache.poi.poi module.

    requires org.apache.poi.poi;
    

    Then, we must allow the Apache poi module to access two if its dependencies. This needs to be added as a JVM argument. Add the following JVM argument into your project.

    --add-opens=org.apache.poi.poi/com.zaxxer=ALL-UNNAMED 
    --add-opens=org.apache.poi.poi/org.apache.commons.math3=ALL-UNNAMED"
    

    Create Excel Workbook

    The first thing to do when creating a spreadsheet file is creating a workbook. We will create an org.apache.poi.hssf.usermodel.HSSFWorkbook and then save it as an .xlsx file.

    import java.io.File;
    import java.io.OutputStream;
    import java.nio.file.Files;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Workbook;
    
    public class ExcelTutorial {
    
      public static void main(String[] args) throws Exception {
        ExcelTutorial excelTutorial = new ExcelTutorial();
        excelTutorial.createWorkbook();
      }
    
      private void createWorkbook() throws Exception {
        //Create a workbook
        try (Workbook wb = new HSSFWorkbook()) {
          //Add two sheets into the workbook
          wb.createSheet("My Excel Sheet 1");
          wb.createSheet("My Excel Sheet 2");
          //Save the workbook to a file
          try (OutputStream fileOut = Files.newOutputStream(new File("my_first_java_spreadsheet.xlsx").toPath())) {
            wb.write(fileOut);
          }
        }
      }
    

    This will create a new file Excel file. We can open it on the MS Excel or any compatible spreadsheet program you have installed, including LibreOffice Calc, Google Sheet, etc. The following screenshot shows the Excel file opened with LibreOffice.

    Java Excel Workbook
    Excel workbook we have created with two sheets

    Create Excel spreadsheet with actual data

    Now that we have learned how to create Excel spreadsheet with sheets, let’s learn how to add tabular data into the sheets.

    We can add rows into the sheets using the method sheet.createRow(rowIndex);. For each row, we can then set the value for each cell using the setCellValue(String); function. Let’s understand this better with the following example code.

    import java.io.File;
    import java.io.OutputStream;
    import java.nio.file.Files;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.CellType;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    
    public class ExcelTutorial {
    
      public static void main(String[] args) throws Exception {
        ExcelTutorial excelTutorial = new ExcelTutorial();
        excelTutorial.createEmployeeSheet();
      }
    
      private void createEmployeeSheet() throws Exception {
        try (Workbook wb = new HSSFWorkbook()) {
          Sheet sheet = wb.createSheet("Building#1");
          //Create table header
          Row headerRow = sheet.createRow(0); //First row - Heading
          headerRow.createCell(0).setCellValue("Employee No.");
          headerRow.createCell(1).setCellValue("Name");
          headerRow.createCell(2).setCellValue("Department");
          headerRow.createCell(3, CellType.BOOLEAN).setCellValue("Promoted");
          //Add first employee
          Row firstEmployee = sheet.createRow(1); //Second row
          firstEmployee.createCell(0).setCellValue(1); //Giving numeric value to cell
          firstEmployee.createCell(1).setCellValue("Genuine Coder");
          firstEmployee.createCell(2).setCellValue("IT");
          firstEmployee.createCell(3).setCellValue(false); //Giving a boolean value to the cell
          //Add second employee
          Row secondEmployee = sheet.createRow(2); //Third row
          secondEmployee.createCell(0).setCellValue(2);
          secondEmployee.createCell(1).setCellValue("Anya");
          secondEmployee.createCell(2).setCellValue("IT");
          secondEmployee.createCell(3).setCellValue(true); //Giving a boolean value to the cell
    
          //Write workbook into file
          try (OutputStream fileOut = Files.newOutputStream(new File("employee_data.xlsx").toPath())) {
            wb.write(fileOut);
          }
        }
      }
    }
    

    We have created a new Excel file with the name employee_data.xlsx and two employees. For each employee, we have given numeric, string and boolean cell data. You can see the generated file in the below screenshot.

    Java Excel Workbook with data
    Java Excel Workbook With Worksheet and Data

    Q. How to set background color of a row with Apache POI?

    Now, let’s say we want to set the background color of the title. We need to set background color and text color for the entire first row.
    Let’s see how we can do the styling per-cell for the entire header row. This way, in future if you would like to have customization within a row itself, it will be much easier.

    private void applyStyleForRow(Row row) {
      //Create new style for the row
      CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
      //Configure single solid color fill
      cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
      //Set font color
      cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
      //Set background color
      cellStyle.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
      //Apply style for each cell
      row.forEach(cell -> cell.setCellStyle(cellStyle));
    }
    
    Java Spreadsheet with customized header
    Spreadsheet with customized header

    Q. How to change font with Apache POI?

    Often we would want to change the font for particular rows or cells. This is also easily achievable with Apache POI. We can set font family, font weight and of course font size with style attribute. Let’s see how we can do that with a code example.

    private void applyFontForRow(Row row) {
      Workbook workbook = row.getSheet().getWorkbook();
    
      //Create and style font
      Font font = workbook.createFont();
      //Set bold
      font.setBold(true);
      //Set font family
      font.setFontName("Roboto");
      //Set font size
      font.setFontHeightInPoints((short) 20);
    
      //Apply style for each cell
      CellStyle cellStyle = workbook.createCellStyle();
      cellStyle.setFont(font); //Attach font to style
      row.forEach(cell -> cell.setCellStyle(cellStyle));
    }
    

    We have customized the header font by using Bold Roboto Font of 20pt size. The generated file looks as shown below.

    Java Spreadsheet with customized font
    Spreadsheet with customized fonts

    Conclusion

    In this tutorial, we have learned how to create Excel spreadsheets in Java. We have seen how to create workbook with multiple sheets, adding contents to sheets, customizing font and background style of cells and rows. If you have liked this article, you might want to check some of my other tutorial articles given below.

  • Java WebP Image Support – Read and Save WebP Images

    Java WebP Image Support – Read and Save WebP Images

    WebP is a new generation image format developed by Google exclusively for the Web. It has both lossy and lossless compression support. When compared to the commonly used formats like JPEG, WebP has 25%-34% lesser file size for the same quality. WebP provides much better compression and reduced file sizes, which are crucial for the web since every byte of data saved matters.

    In this tutorial, let’s see how we can process WebP images in the Java program. We will learn how to read and save/encode/write WebP images.

    Add support for WebP into your Java program

    As of writing this article in 2022, Java doesn’t natively support WebP images. However, there is a there is a library called webp-imageio that can support WebP image-io for Java. Let’s make use of this web-imageio library to process WebP images.

    Add the dependency on to your project.

    If you are using the Gradle, add the dependency as follows.

    // https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio
    implementation group: 'org.sejda.imageio', name: 'webp-imageio', version: '0.1.6'
    

    If you are using the Maven, add the dependency as follows.

    <!-- https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio -->
    <dependency>
        <groupId>org.sejda.imageio</groupId>
        <artifactId>webp-imageio</artifactId>
        <version>0.1.6</version>
    </dependency>
    

    Read WebP image from Java

    Now, let’s see how we can read WebP images to a BufferedImage object. You can read the WebP image just like reading any other JPEG or PNGs with ImageIO.read() function. Let’s understand it with a sample code snippet.

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class JavaWebpSupportTest {
    
      public static void main(String[] args) throws IOException {
        JavaWebpSupportTest webPSupportTest = new JavaWebpSupportTest();
        webPSupportTest.readWebpImage();
      }
    
      private void readWebpImage() throws IOException {
        BufferedImage image = ImageIO.read(new File("/home/gc/my_image.webp"));
        System.out.printf("\nDimension of the image:%dx%d", image.getWidth(), image.getHeight());
      }
    }
    

    This will read the WebP image as BufferedImage and will print the dimension of the image. You can see a sample output below. You can then continue processing the BufferedImage just you do with any other image format.

    Java Webp Image Read
    Java program successfully read WebP image and displayed its dimension.

    Save WebP image from Java

    Now that we have learned how to read WebP images, let’s see how we can save a BufferedImage as a WebP image. For this, we will read a JPEG image and then save it as a WebP image. WebP supports for both lossless and lossy compressions. So, let’s explore how to do both cases with Java.

    private void convertJpegToWebp() throws Exception {
      //Read JPG image
      BufferedImage image = ImageIO.read(new File("/home/afsal/sample.jpg"));
      // Encode it as webp using default settings and save it as webp file
      ImageIO.write(image, "webp", new File("/home/afsal/sample.webp"));
    }
    

    1. Save WebP with lossless encoding

    In the above code snippet, we have used the default settings for writing the WebP image file. Now, let’s see how we can customize the encoding parameters to customize the image. First, let’s save the image in lossless format. We will set the compression type as LOSSLESS_COMPRESSION using the WebPWriteParam#setCompressionType option.

    import com.luciad.imageio.webp.WebPWriteParam;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.IIOImage;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageWriteParam;
    import javax.imageio.ImageWriter;
    import javax.imageio.stream.FileImageOutputStream;
    
    public class JavaWebpSupportTest {
    
      public static void main(String[] args) throws Exception {
        JavaWebpSupportTest webPSupportTest = new JavaWebpSupportTest();
        webPSupportTest.convertJpegToWebpLossless();
      }
    
      private void convertJpegToWebpLossless() throws Exception {
        BufferedImage image = ImageIO.read(new File("/home/gc/sample.jpg"));
    
        ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();
    
        WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
        //Notify encoder to consider WebPWriteParams
        writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        //Set lossless compression
        writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSLESS_COMPRESSION]);
    
        // Save the image
        writer.setOutput(new FileImageOutputStream(new File("/home/gc/sample.webp")));
        writer.write(null, new IIOImage(image, null, null), writeParam);
      }
    }
    

    2. Save WebP with lossy encoding

    Now, let’s see how we can configure the compression ratio and use lossy compression to encode an image as WebP. The following code will read a JPEG image and save it as WebP with 80% compression quality (0% has the highest compression and 100% has the highest quality).

    private void convertJpegToWebpWithLossyCompression() throws Exception {
      BufferedImage image = ImageIO.read(new File("/home/gc/sample.jpg"));
    
      ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();
    
      WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
      //Notify encoder to consider WebPWriteParams
      writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
      //Set lossy compression
      writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSY_COMPRESSION]);
      //Set 80% quality. Allowed values are between 0 and 1
      writeParam.setCompressionQuality(0.8f);
    
      // Save the image
      writer.setOutput(new FileImageOutputStream(new File("/home/gc/sample.webp")));
      writer.write(null, new IIOImage(image, null, null), writeParam);
    }
    

    Conclusion

    In this tutorial, we have learned how to process WebP images using Java. With the help of the webp-imageio library, we can read and write WebP images easily. Since the library works with ImageIO.read() and ImageIO.write(), the processing of images is a breeze. If you are using Linux and wants to enable WebP thumbnail support on your platform, learn how to do that from the following tutorial: How to add WebP thumbnail support on Ubuntu and Linux Mint.

  • JavaFX Image Button Tutorial

    JavaFX Image Button Tutorial

    In this tutorial, we will learn how to create image buttons in JavaFX. By default, there is no special component named ImageButton. However, we can easily extend the default JavaFX Button to create a button with image.

    JavaFX Image Button with Text

    To create a button with image, we can make use of the Button#setGraphic(Node) function. We can set the image we want to attach to the button through the setGraphic() method. Let’s see an example code to understand it better.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ContentDisplay;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.Region;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class JavaFXImageButton extends Application {
    
      private static final double BUTTON_HEIGHT = 100;
    
      public void start(Stage stage) {
        //Create a Button
        Button button = new Button("Download!");
        button.setPrefSize(Region.USE_COMPUTED_SIZE, BUTTON_HEIGHT);
    
        //Create imageview with background image
        ImageView view = new ImageView(new Image("download.png"));
        view.setFitHeight(BUTTON_HEIGHT);
        view.setPreserveRatio(true);
    
        //Attach image to the button
        button.setGraphic(view);
        //Set the image to the top
        button.setContentDisplay(ContentDisplay.TOP);
    
        Scene scene = new Scene(new StackPane(button), 600, 300);
        stage.setTitle("JavaFX Button with Image!");
        stage.setScene(scene);
        stage.show();
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    }
    
    JavaFX Image Button With Text
    JavaFX Image Button With Text

    You can see the result above. We have added the image icon on the top and text on the bottom. You can change the position of the text with respect to the image by changing the setContentDisplay() option.

    //Set button text to the right of the image
    button.setContentDisplay(ContentDisplay.RIGHT);
    //Set button text to the left of the image
    button.setContentDisplay(ContentDisplay.LEFT);
    

    JavaFX Image Only Button

    Now, if you would like to implement a button without any text that fits the button fully, let’s see how that can be achieved. When you want a clickable button with just an image and no text, you can still use the setGraphic(Node) function. Let’s understand it with an example code. We should make use of the no-arg constructor of javafx.scene.control.Button() to create button without any text.

    private static final double BUTTON_HEIGHT = 100;
    
    public void start(Stage stage) {
    
      //Creating a Button without any text
      Button button = new Button();
      button.setPrefSize(Region.USE_COMPUTED_SIZE, BUTTON_HEIGHT);
    
      //Create imageview with background image
      ImageView view = new ImageView(new Image("background.jpg"));
      view.setFitHeight(BUTTON_HEIGHT);
      view.setPreserveRatio(true);
    
      //Attach image to the button
      button.setGraphic(view);
    
      Scene scene = new Scene(new StackPane(button), 600, 300);
      stage.setTitle("JavaFX Button with Image!");
      stage.setScene(scene);
      stage.show();
    }
    

    This will create the following output.

    JavaFX Image Only Button
    JavaFX Image Only Button

    Q. How to remove the padding between the button graphic image and the button edge?
    If you want to get rid of the padding between the image and the button edge, you need to manually set the button padding to zero. This can be done using the setPadding(Inset) option.

    button.setPadding(Insets.EMPTY);
    

    JavaFX Image Button with CSS

    We can also create ImageButton with the help of JavaFX CSS. Set the image as the background for the Button using -fx-background-image CSS styling. Let’s see how we can create our ImageButton with CSS. The disadvantage of using a background image is that, the text will be on top of the image, and we will have to make sure that the text color is readable in the image background.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class JavaFXImageButton extends Application {
    
      public void start(Stage stage) {
        //Creating a Button without any text
        Button button = new Button();
        button.setPrefSize(200, 100);
        button.setStyle("-fx-background-image: url(background.jpg);-fx-text-fill: white;-fx-font-size: 20px");
        button.setText("Download!");
    
        Scene scene = new Scene(new StackPane(button), 600, 300);
        stage.setTitle("JavaFX Image Button with CSS!");
        stage.setScene(scene);
        stage.show();
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    }
    
    JavaFX Image Button using CSS
    JavaFX Image Button using CSS

    Conclusion

    In this tutorial, we have learned how to create JavaFX Buttons with Images. We have created button with image and text, button with only image and Image Button with CSS styling. If you liked this article, you might also want to checkout some of the other interesting articles I have written.

  • Create your first flutter desktop application – Flutter tutorial

    Create your first flutter desktop application – Flutter tutorial

    Flutter is the new developer-friendly cross-platform open-source framework developed by Google. The flutter community is growing and as of 2022, with flutter 3, it is stable for Android, iOS, Web and Desktop platforms! Flutter 3 added stable support for Linux desktop and macOS platform.

    In this tutorial for beginners, we will learn how to create a simple flutter application and build it as a native desktop application for Linux.

    Setting up the flutter development environment

    Based on your platform, the steps for installing the flutter and configuring is a bit different. In this tutorial, we will cover the setting up of flutter SDK for Linux Ubuntu platform.

    Install and configure flutter for Ubuntu/Mint

    • Step 1: Install the flutter SDK using snap

            sudo snap install flutter --classic
          
    • Step 2: Install additional dependencies for flutter-sdk’s development

            sudo apt-get install clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev
          
    • Step 3 [OPTIONAL]: Link Android Studio to the flutter by providing Android Studio installed path

            flutter config --android-studio-dir /your/path/to/android-studio/
          
    • Step 4 [OPTIONAL]: Link Android SDK to the flutter by providing Android SDK installed path

            flutter config --android-sdk /usr/local/bin/Programs/android-sdk/
          
    • Step 5 [OPTIONAL]: Accept Android SDK licenses by running the following command

            flutter doctor --android-licenses
          
    • Step 6: Run the flutter doctor command again. Now all dependencies should be ticked!

            flutter doctor --android-licenses
          

    Flutter Ubuntu Install

    Enable flutter desktop development support

    First things first, let’s confirm that flutter SDK desktop support is enabled. To do this, run the command “flutter devices” and make sure that your desktop platform is indeed listed as shown in the below screenshot. Note that, based on your platform, it could be Linux (desktop), Windows (desktop) or macOS (desktop).
    Flutter devices command

    #Command format
    flutter config --enable-<platform>-desktop
    #Enable Linux desktop support for flutter
    flutter config --enable-windows-desktop
    #Enable Windows desktop support for flutter
    flutter config --enable-linux-desktop
    #Enable macOS desktop support for flutter
    flutter config --enable-macOS-desktop
    

    Create a new flutter desktop project

    Now, let’s create our flutter project. Flutter project can be created with “flutter create” command. Let’s create a new flutter project named “genuine_flutter_app” with the following command. You can use your project’s name.

    flutter create genuine_flutter_app
    

    Now, you will see the following output mentioning that the flutter app is indeed created.

    Creating project genuine_flutter_app...
    Running "flutter pub get" in genuine_flutter_app...              1,968ms
    Wrote 127 files.
    
    All done!
    To run your application, type:
    
      $ cd genuine_flutter_app
      $ flutter run
    
    Your application code is in genuine_flutter_app/lib/main.dart.
    

    The flutter SDK has just created a new folder with default project template and added all the required files for us. We can run the project right away and see if all is good so far.

    Run the flutter app on desktop

    Since the “flutter create” command created the project with template code, we can run it and test it right away. So, let’s run the project with flutter run command. Since I am using Linux, I will be using “-d linux”. If you are using Windows or Mac, make sure to use your platform’s name there.

    #cd into the project directory
    cd genuine_flutter_app
    #Run the project
    flutter run -d linux
    

    and…here we go! You will now see the application window opened. This is the default template project.

    Flutter Linux Desktop Application Running
    Flutter Linux Desktop Application Running

    Customize the default project

    Let’s customize the default project and see how we can program in dart. To achieve this, let’s open the project on our IDE. I am using VSCode. You can choose your own editor.

    Open the file lib/main.dart. This is the source file that is generating the sample demo project. Let’s then customize the project.

    Flutter main.dart
    Flutter main.dart file inside lib directory

    Q1. How to set title of the flutter AppBar?

    Currently, the AppBar title is ‘Flutter Demo Home Page’ that is passed on to the MyHomePage widget. So, let’s change it into ‘My First Flutter Desktop App!’ by changing it in the MyApp widget.

    Q2. How to change the AppBar color?

    The AppBar color can be changed from the theme attribute. Set the primarySwatch as your preferred color and that will update the AppBar color.

    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
    
          //Set the app color there
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          
          // Set the title here
          home: const MyHomePage(title: 'My First Flutter Desktop App!'),
        );
      }
    

    Q3. How to change the app body color?

    To set the background color for the body section, you need to set the background for the scaffold.

    ....
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        //This is where the AppBar title is set
        appBar: AppBar(
          title: Text(widget.title),
        ),
        //Set your preferred background color
        backgroundColor: Colors.grey,
    ....
    

    Build and release executable file from flutter project

    Now that we have a working flutter application, let’s see how we can build an executable file for our project. Thanks to flutter, this is also an easy step. You can execute the “flutter build” command to start the building. If you are using Windows or Mac, make sure to use your platform instead of Linux.

    #Generates Linux runnable image (application/x-executable)
    flutter build linux
    #Generates Windows runnable .exe file
    flutter build windows
    #Generates macOS runnable .app file
    flutter build macos
    

    This will build an executable file for the project. The executable can then be found at the following path. [your_platform] can be Linux, windows and macOS.

    build/[your_platform]/x64/release/bundle
    
    Flutter project released as Linux desktop executable
    Flutter project released as Linux desktop executable

    Conclusion

    In this tutorial, we have learned how to create a Flutter desktop application and build a release executable file from the project. We will be posting more flutter related tutorials in the future. If you liked this tutorial, you might also be interested in checking out the following articles.

  • How to add WebP thumbnail support on Ubuntu and Linux Mint

    How to add WebP thumbnail support on Ubuntu and Linux Mint

    As of writing this article, on 2022 June, there is no default support for WebP images in the Ubuntu/Linux Mint file explorers. As a result, the image file thumbnails are not rendered and we have to open the image file to see what’s in it. In this tutorial, we will see how to add support for the WebP files so that the image thumbnails are generated just like any other supported image format.

    What is WebP Image Format?

    WebP is an image format developed specifically for the web by Google. The primary goal of the WebP format is to replace other conventional formats like JPEG, PNG, etc. and provide better compression and quality/file-size ratio. When compared to JPEG, WebP file size is 25%-34% for the comparable quality JPEG image.

    Adding WebP thumbnail support

    To add support for WebP images, we need to install the package named “webp-pixbuf-loader”. You can install them by executing the following commands.

    #Add the PPA
    sudo add-apt-repository ppa:krifa75/eog-ordissimo
    
    #Refresh apt
    sudo apt update
    
    #Install the package
    sudo apt install webp-pixbuf-loader
    

    After the installation is done, just refresh the file-manager and you will see the thumbnails generated for the WebP images.

    Webp Thumbnails shown on Nemo file manager

    Image Viewer with WebP Support

    Web browsers like Google Chrome can act as WebP image-viewer. However, if you are looking for image-viewer program itself with support for WebP, you can try gThumb.

    How to install gThumb on Ubuntu/Mint

    sudo apt install gthumb
    

    This will install the gThum image viewer and you can use it to open WebP images.

    Conclusion

    In this tutorial, we have seen how to add native support for WebP images so that the file manager can show thumbnails for them. Also, gThumb image viewer has good support for the new WebP image standard. You might also be interested in reading the following articles.

  • JavaFX ObservableList Tutorial

    JavaFX ObservableList Tutorial

    JavaFX ObservableList is JavaFX SDK’s special implementation of List interface. It is basically an observable list that provides option to attach listeners for list content change. ObservableList has an integral role in the JavaFX development because it is used for major components like TableView, ComboBox, etc.

    In this tutorial, we will explore the JavaFX ObservableList and see how we can utilize its special functionalities.

    Creating and using ObservableList

    You cannot instantiate an ObservableList class directly using a new call. Instead, FXCollections should be used to create an ObservableList. Let’s see how we can create an ObservableList of String type. The most common way to create it is using observableArrayList() function.

    ObservableList<String> listInstance = FXCollections.observableArrayList();
    //Add a single entry
    listInstance.add("Java");
    System.out.println(listInstance);
    
    //Add multiple entries
    listInstance.addAll("Cpp", "C#", "Python");
    System.out.println(listInstance);
    
    //Remove entry
    listInstance.remove("Cpp");
    System.out.println(listInstance);
    

    As you have already noted, the basic operations are similar to normal lists, except the addAll() function availability. You can add multiple entries to the ObservableList using comma separated values. The output of the above code is as follows.

    [Java]
    [Java, Cpp, C#, Python]
    [Java, C#, Python]
    

    Now, let’s see some special advanced features of the ObservableList.

    Listening for changes in the ObservableList

    ObservableList allows us to add listeners into the list so that we can execute some logic when the contents of the list is changed. In the previous code snippet, we have added system.out.println after each change we made into the list to print the list. Instead, we can actually attach a listener to the ObservableList and it will help us to print the list as soon as it is changed. To achieve this, we will make use of javafx.collections.ListChangeListener. The onChanged() function will be called as soon as any change is made to the contents of the list.

    ObservableList<String> listInstance = FXCollections.observableArrayList();
    
    //Add a ListChangeListener that will execute as soon as the contents of the list is changed.
    listInstance.addListener(new ListChangeListener<String>() {
      @Override
      public void onChanged(Change<? extends String> c) {
        System.out.println(listInstance);
      }
    });
    
    //Manipulate contents of the list
    listInstance.add("Java");
    listInstance.addAll("Cpp", "C#", "Python");
    listInstance.remove("Cpp");
    

    The output of the code snippet is given below. The “System.out.println(listInstance);” was executed three times, once after each list change.

    [Java]
    [Java, Cpp, C#, Python]
    [Java, C#, Python]
    

    ObservableList also provides a second type of listener called InvalidationListener. Unlike ListChangeListener that provides the actual change as a parameter to the onChanged function, it doesn’t provide the change. You can read more about it in the dedicated observable tutorial.

    Getting values from the ObserableList is similar to getting value from normal lists in Java. To get an item at specific index, you can call “observableList.get(index)”. You can also get iterator through observableList.iterator() function.

    Video Tutorial for JavaFX ObservableList

    If you are interested in learning more about the ObservableList, please have a look into this ObservableList video tutorial. The video shows how to make use of the ObservableList for JavaFX ListView with realworld example.

    ObservableList size change listener

    Thanks to JavaFX Binding class, we can add listeners to some observable properties of the list, like the size of the list. This way, similar to how we tracked the list-content-change using a listener, we can also easily track the list size. Let’s adjust the above list content listener code to list size listener code and learn how to create a binding value to an ObservableList’s size and add a listener to it.

    ObservableList<String> listInstance = FXCollections.observableArrayList();
    
    //Listen for changes in list size
    IntegerBinding listSizeProperty = Bindings.size(listInstance);
    listSizeProperty.addListener(new ChangeListener<Number>() {
      @Override
      public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
        System.out.println("Updated list size: " + newValue);
      }
    });
    
    listInstance.add("something");
    listInstance.addAll("something1", "random_data", "something2");
    listInstance.remove("somethin3");
    

    Output of the above code snippet is given below. As you can see, the size of the list is tracked and printed for each 3 changes we have made.

    Updated list size: 1
    Updated list size: 4
    Updated list size: 3
    

    JavaFX Observable Program Output

    Conclusion

    In this tutorial, we have learned how to create and use ObservableList in JavaFX. It provides all the functionalities of normal Java lists along with cool observable support. You might also be interested in the other articles on JavaFX like JavaFX Scene Switch Animation and JavaFX Animation Tutorial.

  • JavaFX CheckComboBox

    JavaFX CheckComboBox

    JavaFX CheckComboBox is a special UI control provided by ControlsFX. It is used for giving option to users to select more than one item from a Combobox. It is an extremely useful component you can use to provide a rich user experience. In this tutorial, we will explore the component usage with an example and style it using CSS. The basic idea of this component is Checkbox + ComboBox so that ComboBox can have multiple parallel selections.

    Adding ControlsFX to Project

    Since the component is from ControlsFX library, you need to include the dependency on your project. If you are using the maven, add the dependency as follows.

    <dependency>
      <groupId>org.controlsfx</groupId>
      <artifactId>controlsfx</artifactId>
      <version>11.1.1</version>
    </dependency>
    

    If you are using Gradle, add the following line to your build.gradle file.

    implementation 'org.controlsfx:controlsfx:11.1.1'
    

    How to use CheckComboBox

    The usage of CheckComboBox is similar to the normal JavaFX ComboBox. You can create a list of item you want to add and then attach it to the CheckComboBox.
    In the following example, we will make use of CheckComboBox to keep things simlpe. Let’s see the usage on a sample code.

    JavaFX CheckComboBox<String> Example

    //Reduced import code
    import org.controlsfx.control.CheckComboBox;
    
    public class JavaFXCheckComboBoxTutorial extends Application {
    
      private CheckComboBox<String> createCheckComboBox() {
        // Create the list of items to be shown on the combobox
        ObservableList<String> programmingLanguages = FXCollections.observableArrayList(
            "Java",
            "Cpp",
            "Python",
            "C#",
            "Kotlin"
        );
        // Attach the list to the Combobox
        CheckComboBox<String> checkComboBox = new CheckComboBox<>(programmingLanguages);
        //As soon as an item is selected or selection is changed, display all the selected items
        checkComboBox.getCheckModel().getCheckedItems().addListener(new InvalidationListener() {
          @Override
          public void invalidated(Observable observable) {
            System.out.printf("\nSelected items: %s", getSelectedItems(checkComboBox));
          }
        });
        return checkComboBox;
      }
    
      /**
       * Get the currently selected items from the CheckComboBox
       */
      private List<String> getSelectedItems(CheckComboBox<String> checkComboBox) {
        return checkComboBox.getCheckModel().getCheckedItems();
      }
    
      @Override
      public void start(Stage stage) {
        VBox contentPane = new VBox(10);
        contentPane.setAlignment(Pos.CENTER);
        contentPane.getChildren().addAll(new Label("CheckComboBox Example"), createCheckComboBox());
    
        Scene scene = new Scene(contentPane, 400, 500);
        stage.setScene(scene);
        stage.show();
      }
    
      public static void main(String[] args) {
        launch();
      }
    }
    

    To get the list of selected items from the CheckComboBox, you can use

    checkComboBox.getCheckModel().getCheckedItems()

    It will return an ObservableList with all the selected items.

    Customize CheckComboBox with CSS

    Now, let’s see how to style the CheckComboBox with JavafX CSS styling. This component can be styled using combo-box-base CSS. By applying the following CSS, the CheckComboBox is customized with a lighter background.

    .combo-box-base {
      -fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, white;
      -fx-background-insets: 0 0 -1 0, 0, 1, 2;
      -fx-background-radius: 3px, 3px, 2px, 1px;
    }
    
    .combo-box .combo-box-popup .list-cell:hover {
      -fx-text-fill: yellow ;
      -fx-background-color: green ;
    }
    
    .combo-box .combo-box-popup .list-view, .combo-box .combo-box-popup .list-cell {
      -fx-background-color: black ;
      -fx-text-fill: white ;
    }
    

    JavaFX CheckComboBox Custom CSS

    Conclusion

    In this tutorial, we have discussed how to make use of the CheckComboBox in code and style it using CSS. ControlsFX has a lot of such useful extra components. You might also be interested to checkout the following articles.

  • JavaFX Custom Shaped Buttons

    JavaFX Custom Shaped Buttons

    JavaFX provides effective ways to customize the shape of most of the components. In this tutorial, we will learn how to set a custom shape for the JavaFX Button component.

    Creating custom shaped buttons

    We can make use of -fx-shape CSS property to set the button to any custom shape. Let’s see how we can make a heart shaped button using -fx-shape styling. We will create two buttons, one normal button without any special styling and one with heart-shape. The -fx-shape css property accepts SVG paths as shape. Thanks to the SVG support, we can actually make any shape by providing the corresponding svg parameters. You can learn more about SVG shapes from w3.org.

    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class HeartShapedButtonTutorial extends Application {
    
      @Override
      public void start(Stage stage) {
        HBox contentContainer = new HBox(50d, createNormalButton(), createHeartShapedButton());
        contentContainer.setAlignment(Pos.CENTER);
        Scene scene = new Scene(contentContainer, 500, 500);
        stage.setTitle("JavaFX Button Tutorial");
        stage.setScene(scene);
        stage.show();
      }
    
      private Button createNormalButton() {
        Button button = new Button("Normal Button!");
        button.setPrefSize(200, 200);
        return button;
      }
    
      private Button createHeartShapedButton() {
        Button button = new Button("Custom Button!");
        button.setStyle(
            "-fx-shape: \"M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.26.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z\";"
        );
        button.setPrefSize(200, 200);
        return button;
      }
    
      public static void main(String[] args) {
        launch();
      }
    }
    

    This will create the following output. The normal JavaFX button is added as the first child to the HBox. The custom shaped button is added as the second child.
    JavaFX Custom Shaped Button

    You can also make use of this styling on special buttons like JavaFX Material Design Button, JFXButton.

    Full tutorial on JavaFX Custom Shaped Button

    If you are interested in exploring this further, checkout my video tutorial on Genuine Coder YouTube channel.

    Circular Button

    We can make the button circular by applying the following CSS code.

    .circlular-javafx-button{
        -fx-background-radius: 150;
        -fx-pref-width: 300;
        -fx-pref-height: 300;
        -fx-background-color: green;        
    }
    
    JavaFX Circular Button
    JavaFX Circle Shaped Button

    Triangle Shaped Button

    We can make triangle buttons by applying the following CSS code.

    .balloon{
        -fx-shape: "M150 0 L75 200 L225 200 Z";
        -fx-background-color: #4DD0E1;
    }
    
    JavaFX Triangle Shaped Button
    Triangle Shaped Button

    Conclusion

    JavaFX allows setting custom shapes for its GUI components via -fx-shape option. We have explored the option and created variety of different buttons with heart-shape, circle-shape and triangle shape. You can make the button any shape you want by giving proper SVG path. If you have enjoyed this article, you might also be interested in the following.

  • How to open file explorer in java

    How to open file explorer in java

    Sometimes, we come across requirements where we need to open a particular folder or directory in the native file explorer. In this tutorial, we will see how to do that, easy and effective.

    Opening file explorer with java in any platform – Windows, Linux and Mac

    The easiest way to do this is by using Java Desktop. The class provides an open() function Desktop#open(File) to open explorer when the given file is a directory.

    private static void openDirectory() throws IOException {
      File directory = new File("C://Program Files//");
      Desktop.getDesktop().open(directory);
    }
    

    Open file explorer and select/highlight a file

    Now, if you want to open the file explorer and have a file preselected, then there is a way for that as well! You can make use of Desktop.fileBrowseDirectory(File) to launch the file explorer and have a file highlighted. Let’s see a code example for the same.

    However, before using this function, you have to keep the following things in mind.

    • The code will not work on ALL platforms.
    • You need to check whether the feature is supported via Desktop.getDesktop().isSupported(Action.BROWSE_FILE_DIR) function.
    • Minimum Java 9 is required
    import java.awt.Desktop;
    import java.awt.Desktop.Action;
    import java.io.File;
    
    public class JavaFileExplorer {
    
      public static void main(String[] args) {
        openDirectory();
      }
    
      private static void openDirectory() {
        //Check if the feature supported on your platform
        if (Desktop.getDesktop().isSupported(Action.BROWSE_FILE_DIR)) {
          File directory = new File("/home/afsal/Downloads/jdk-8u212-linux-x64.tar.gz");
          //Open directory with browse_file_dir option
          Desktop.getDesktop().browseFileDirectory(directory);
        }
      }
    }
    

    Open file explorer in Linux

    Now, if you would like to add specific code for Linux/Ubuntu to open the file explorer, have a look into the following code snippet. We can make use of the xdg-open command to open any directory. The good thing about using this method is that, it works across multiple Linux distributions. They just have to have the xdg-open support.

    private static void openDirectoryInLinux() throws Exception {
      Runtime.getRuntime().exec(
         new String[]{"sh", "-c", "/usr/bin/xdg-open '/home/genuinecoder/Downloads/'"}
       );
    }
    

    Open file explorer in Windows

    Now, if you would like to add specific code for Windows OS to open the file explorer, have a look into the following code snippet. We will execute a cmd command to open Windows explorer.exe.

    private static void openDirectoryInWindows() throws Exception {
      Runtime.getRuntime().exec("explorer C:\\");
    }
    

    Open file explorer in MacOS

    If you are looking for specific code for macOS, you can make use of the /usr/bin/open function to open the directory on macOS.

    private static void openDirectoryInMac() throws Exception {
       Runtime.getRuntime().exec(new String[]{"/usr/bin/open", file.getAbsolutePath()});
    }
    

    Conclusion

    In this tutorial, we have learned how to open a directory from a java program. We have numerous methods to achieve the same goal here. We can use java.awt.Desktop for platform independent support and if platform-specific implementation is required, that is also available. You might also be interested in the following topics.

    1. Java TrayIcon Tutorial
    2. Directory / Folder Indexing Program Using Java
  • JavaFx DatePicker Tutorial

    JavaFx DatePicker Tutorial

    In this tutorial, we will talk about JavaFX DatePicker basic usage, how to format the selected date using DateTimeFormatter to string value, and customize the component using JavaFX CSS.

    Using JavaFx DatePicker

    DatePicker is a standard component in the JavaFX SDK. It can be added from the SceneBuilder via FXML file as well. In the following code snippet, we will add the DatePicker to a simple scene without using FXML.

    import java.time.LocalDate;
    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.scene.Scene;
    import javafx.scene.control.DatePicker;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class JavaFXDatePickerTutorial extends Application {
    
      @Override
      public void start(Stage stage) {
        Scene scene = new Scene(new StackPane(createDatePicker()), 400, 500);
        stage.setTitle("JavaFX DatePicker Tutorial");
        stage.setScene(scene);
        stage.show();
      }
    
      //Create a date picker and attach and event listener.
      private DatePicker createDatePicker() {
        DatePicker datePicker = new DatePicker();
        //Set Current date to today's date
        datePicker.setValue(LocalDate.now());
        //Add an event listener for date selection change
        datePicker.valueProperty().addListener(new ChangeListener<LocalDate>() {
          @Override
          public void changed(ObservableValue<? extends LocalDate> observable, LocalDate oldValue, LocalDate newValue) {
            //Print date change to console
            System.out.println("New date selected: " + newValue);
          }
        });
        return datePicker;
      }
    
      public static void main(String[] args) {
        launch();
      }
    }
    

    JavaFX DatePicker

    Get date as string from JavaFX DatePicker

    We can get the currently selected date anytime from the DatePicker calendar using the datePicker.getValue() function. It will return a LocalDate object representing the currently selected date.

    When we need to display these dates somewhere, we need to convert the object to a formatted string. It can be easily done through Java DateTimeFormatter.

    As an example, let’s convert the selected date to dd/MM/yyyy format.

    private String getFormattedDateFromDatePicker(DatePicker datePicker) {
      //Get the selected date
      LocalDate selectedDate = datePicker.getValue();
      //Create DateTimeFormatter
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
      //Convert LocalDate to formatted String
      return selectedDate.format(formatter);
    }
    

    If you want the date to be formatted in some other way, DateTimeFormatter can be customized. You can prepare your own pattern using the DateTimeFormatter.ofPattern() function. Following section shows some supported symbols you can use to prepare your own date format.

      Symbol  Meaning                     Presentation      Examples
      ------  -------                     ------------      -------
       G       era                         text              AD; Anno Domini; A
       u       year                        year              2004; 04
       y       year-of-era                 year              2004; 04
       D       day-of-year                 number            189
       M/L     month-of-year               number/text       7; 07; Jul; July; J
       d       day-of-month                number            10
    
       Q/q     quarter-of-year             number/text       3; 03; Q3; 3rd quarter
       Y       week-based-year             year              1996; 96
       w       week-of-week-based-year     number            27
       W       week-of-month               number            4
       E       day-of-week                 text              Tue; Tuesday; T
       e/c     localized day-of-week       number/text       2; 02; Tue; Tuesday; T
       F       week-of-month               number            3
    
       V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
       z       time-zone name              zone-name         Pacific Standard Time; PST
       O       localized zone-offset       offset-O          GMT+8; GMT+08:00; UTC-08:00;
       X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15;
       x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15;
       Z       zone-offset                 offset-Z          +0000; -0800; -08:00;
    

    Customize DatePicker with CSS

    Now, let’s say you would like to customize the DatePicker. It is easily possible with JavaFX CSS styling. The following code snippet shows all the CSS customizations you can apply on the DatePicker for full customization. With the following CSS applied, the DatePicker will look as shown in this screenshot.

    JavaFX CSS Customized DatePicker

    * {
      -fx-primary-color: #c26000;
      -fx-secondary-color: #864500;
      -fx-light-grey-color: #d1d1d1;
      /*Set focus color*/
      -fx-focus-color: -fx-secondary-color;
      /*Increase font size*/
      -fx-font-size: 1.5em;
    }
    
    /*-----------------------------------------
    Customize the right button background color
    -------------------------------------------*/
    .date-picker .arrow-button {
      -fx-background-color: -fx-primary-color;
      -fx-background-radius: 0;
    }
    
    .date-picker .arrow-button:hover {
      -fx-background-color: -fx-secondary-color;
    }
    
    .date-picker .arrow-button .arrow {
      -fx-background-color: white;
    }
    
    /*-----------------------------------------
    Customize popup content
    -------------------------------------------*/
    .date-picker .cell {
      -fx-background-color: white;
    }
    
    .date-picker .cell:hover {
      -fx-background-color: -fx-primary-color;
    }
    
    .date-picker .cell:focused {
      -fx-background-color: -fx-primary-color;
    }
    
    /*-----------------------------------------
    Customize the selected cell
    -------------------------------------------*/
    .date-picker .selected {
      -fx-background-color: -fx-primary-color;
      -fx-text-fill: white;
    }
    
    .date-picker .selected:focused {
      -fx-background-color: -fx-primary-color;
      -fx-text-fill: white;
    }
    
    .date-picker-popup {
      -fx-border-color: transparent;
    }
    
    .date-picker-popup .month-year-pane {
      -fx-background-color: -fx-primary-color;
    }
    
    .date-picker-popup .month-year-pane .label {
      -fx-text-fill: white;
    }
    
    .date-picker-popup .week-number-cell {
      -fx-text-fill: -fx-secondary-color;
    }
    
    /*-----------------------------------------
    Customize left and right arrow
    -------------------------------------------*/
    .date-picker-popup .spinner .button .left-arrow,
    .date-picker-popup .spinner .button .right-arrow {
      -fx-background-color: #c2d70e;
    }
    

    Conclusion

    In this tutorial, we have learned how to use DatePicker, format the date, and customize it fully with CSS. You might also be interested in having a look into the following tutorials as well, or checkout our JavaFX tutorial page.

    1. JavaFX animation tutorial
    2. JavaFX scene switching animation
    3. JavaFX icon setup
  • Java TrayIcon Tutorial (With popupmenu and images)

    Java TrayIcon Tutorial (With popupmenu and images)

    Trayicons are really useful widgets for interacting with the application when the main dialog is minimized/hidden or the application is running in the background. In this tutorial, let’s see how to add a Java Tray Icon with example code.

    Note: JavaFX does not have a separate TrayIcon system. If your project is using JavaFX, then the tutorial is still fully applicable. Because the java.awt.SystemTray can be used for JavaFX as well!

    Creating a simple TrayIcon

    The first step on adding a TrayIcon is checking whether the platform supports it. As you know, java supports a wide variety of platforms and not all platforms will support SystemTray feature. So, the first step on configuring it is to check if it is really supported. Let’s see the basic algorithm here

    1. Make sure the platform supports SystemTray
    2. Load image for the trayicon
    3. Create trayicon object and add it to the tray

    Let’s see how it is done in code.

    public void createTrayIcon() {
      //Check for SystemTray support
      if (!SystemTray.isSupported()) {
        System.err.println("System tray feature is not supported");
        return;
      }
    
      //Get system tray object
      SystemTray tray = SystemTray.getSystemTray();
    
      //Create TrayIcon instance
      Image image = ImageIO.read(TrayIconService.class.getResourceAsStream("/path/to/your/icon.png"));
      TrayIcon trayIcon = new TrayIcon(image, "Genuine Coder", null);
      trayIcon.setImageAutoSize(true);
    
      //Attach TrayIcon to SystemTray
      tray.add(trayIcon);
    }
    

    and…That’s it. When you run the code, you will get a nice TrayIcon in your system’s taskbar. Below, you can find the output of running the code on my system.

    Java TrayIcon on windows 10 taskbar

    Creating TrayIcon with PopupMenu

    Now, let’s see how to add a PopupMenu into the TrayIcon to add some functionality. Let’s iterate the steps first.

    1. Create PopumMenu
    2. Create one Menu and multiple MenuItems and organize them in a tree structure
    3. Attach Menu to PopumMenu
    4. Attach PopupMenu to TrayIcon

    Basically, we need to prepare the MenuItems as we intend to and then attcah it to the PopupMenu. As an example, let’s create the following menu.

    Java TrayIcon With PopupMenu

    public void createTrayIcon() {
      //Check for SystemTray support
      if (!SystemTray.isSupported()) {
        System.err.println("System tray feature is not supported");
        return;
      }
      SystemTray tray = SystemTray.getSystemTray();
      Image image = ImageIO.read(TrayIconService.class.getResourceAsStream("/path/to/your/icon.png"));
      TrayIcon trayIcon = new TrayIcon(image, "Genuine Coder", null);
      trayIcon.setImageAutoSize(true);
    
      //Create root menu
      PopupMenu rootMenu = new PopupMenu();
      
      //Add about and restart items
      MenuItem about = new MenuItem("About");
      rootMenu.add(about);
      Menu restartMenu = new Menu("Restart");
      rootMenu.add(restartMenu);
    
      //Add sub-items to server
      MenuItem restartClient = new MenuItem("Client");
      MenuItem restartServer = new MenuItem("Server");
      restartMenu.add(restartClient);
      restartMenu.add(restartServer);
    
      //Attach to trayIcon
      trayIcon.setPopupMenu(rootMenu);
    
      tray.add(trayIcon);
    }
    

    Adding event handling to the TrayIcon

    Now, let’s see how to attach event-handlers to the TrayIcon menu. As an example, let’s show an alert when the “about” MenuItem is clicked.

    about.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Wow! Java TrayIcon is working!");
        }
    });
    

    With this event-handler, when you click on the “about” MenuItem in the TrayIcon menu, you will get the following alert.
    TrayIcon action with event handler

    Conclusion

    In this tutorial, we have familiarized with Java TrayIcon and added PopupMenu and action listeners into it. TrayIcons can be customized even further by customizing the MenuItem. You can add special MenuItems like CheckBoxMenuItem to add switch behaviour into your SystemTray. You may also want to checkout the following articles

    1. How to send email using Java
    2. Convert JAR to EXE
  • How to fix “trying to overwrite …, which is also in package …” issue in linux

    How to fix “trying to overwrite …, which is also in package …” issue in linux

    Let’s see how to solve the problem of apt or dpkg failing to install packages due to errors like the following. This is happening because the new package you are trying to install is overwriting a file that is part of another installed package.

    dpkg_error trying to overwrite '/usr/lib/x86_64-linux-gnu/libldacBT_enc.so.2.0.2.3', which is also in package libldac:amd64 2.0.2.3~r26478861

    Sample Error Message

    dpkg: error processing archive /tmp/apt-dpkg-install-rf5BLm/15-libldacbt-enc2_2.0.2.3.r4.gaf2dd23-5~ubuntu20.04_amd64.deb (--unpack):
     trying to overwrite '/usr/lib/x86_64-linux-gnu/libldacBT_enc.so.2.0.2.3', which is also in package libldac:amd64 2.0.2.3~r26478861
    Preparing to unpack .../16-libldacbt-abr2_2.0.2.3.r4.gaf2dd23-5~ubuntu20.04_amd64.deb ...
    Unpacking libldacbt-abr2:amd64 (2.0.2.3.r4.gaf2dd23-5~ubuntu20.04) over (2.0.2.3+git20200429+ed310a0-5) ...
    dpkg: error processing archive /tmp/apt-dpkg-install-rf5BLm/16-libldacbt-abr2_2.0.2.3.r4.gaf2dd23-5~ubuntu20.04_amd64.deb (--unpack):
     trying to overwrite '/usr/lib/x86_64-linux-gnu/libldacBT_abr.so.2.0.2.3', which is also in package libldac:amd64 2.0.2.3~r26478861
    

    How to resolve the “trying to overwrite” Issue?

    There are multiple solutions below based on when the error occurred. If you got the issue while running an apt command, then check the apt section. If it happened while running a dpkg command, then check the dpkg section.

    With DPKG

    If the error occurred while you were executing the apt command, then use the command as follows.

    sudo dpkg -i --force-overwrite theDebYouWantToInstall.deb
    sudo apt-get --fix-broken install
    

    This command will install the deb file with file overwriting when needed.

    With APT

    If the error occurred while you were executing the dpkg command, then use the command as follows.

    sudo apt-get -o Dpkg::Options::="--force-overwrite" [YourAptActionCommand]
    
    #An Example
    sudo apt-get -o Dpkg::Options::="--force-overwrite" dist-upgrade
    
    #Another Example
    sudo apt-get -o Dpkg::Options::="--force-overwrite" apt-get install nmap
    

    This command will install the deb file with file overwriting when needed.

  • JavaFX Application Icon Setup

    JavaFX Application Icon Setup

    All programs require a nice-looking icon or logo on the taskbar and the window to make it look professional. JavaFX provides easy ways to set up an application icon using any image of your choice, whether it be JPG or PNG or something else. In this tutorial, let’s see how to set up an icon for the application

    Set JavaFX Window/Stage icon from an image file

    The usual method of attaching an icon to the JavaFX application is that, you simply include the icon image inside the JAR file as a resource and then use it. Let’s see how you can do that.

    Step 1: Add the icon to the resource directory

    First, add the image into the resource directory. You can use any supported image formats, like PNG, JPEG etc.

    JavaFX Add Image Resource

    Step 2: Attach the icon to the application window

    Now, let’s attach the image from the resource directory to the window, aka stage. The following code snippets show how to do that in an example program.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.image.Image;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class JavaFXIconExample extends Application {
    
      @Override
      public void start(Stage stage) {
        Scene scene = new Scene(new StackPane(), 320, 320);    
    
        //Attach the icon to the stage/window
        stage.getIcons().add(new Image(JavaFXIconExample.class.getResourceAsStream("/logo.png")));
    
        stage.setTitle("Sample program!");
        stage.setScene(scene);
        stage.show();
      }
    
      public static void main(String[] args) {
        launch();
      }
    }
    

    So in the above example, we are attaching the icon to the primary stage (also known as root stage). If you want to add logo to the other stages you create, you can repeat the same process again to add icons to them as well, with just one line of code.

    stage.getIcons().add(new Image(JavaFXIconExample.class.getResourceAsStream("/logo.png")));
    

    JavaFX Application With Custom Icon

    Set icon from an image URL

    Now, let’s say you want to add the image dynamically from a URL instead of adding from an image file, that is also easily possible. This can be done with just one line of code, as shown below.

    stage.getIcons().add(new Image("https://genuinecoder.com/wp-content/uploads/2022/06/genuine_coder-3.png"));
    

    In this example, I have used genuine coder logo to dynamically load the application icon. The advantage of using the logo from a URL is that, you can change the logo in the server without touching your program once it is rolled out to your customers.

    Conclusion

    JavaFX provides a one-line option to attach images/logo to the windows. The logo image can be loaded from an image file or from a URL.

  • Downloading a file from spring controller with spring boot

    Downloading a file from spring controller with spring boot

    Sometimes, we will have to provide rest API endpoint for downloading certain files. In this article, we will see how to provide an option to download any file from a java spring rest API controller, with example code snippet. We will use spring-boot annotations here to make things easier and straightforward. This method provides the file download option with resume support.

    Configure a file for download with spring controller

    Let’s assume that you have a file that you want to provide for download at “data/file_to_download.mp4”. Then you can write the controller as follows to make it downloadable by any browser. Also, the REST API endpoint is given in the @GetMapping annotation.

    @GetMapping(path = "/your-api/get-video-file")
    public ResponseEntity<Resource> downloadFile() throws Exception {
        File downloadFile = new File("data/file_to_download.mp4");
        InputStreamResource resource = new InputStreamResource(new FileInputStream(downloadFile));
        HttpHeaders header = new HttpHeaders();
        header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + downloadFile.getName());
        header.add("Cache-Control", "no-cache, no-store, must-revalidate");
        header.add("Pragma", "no-cache");
        header.add("Expires", "0");
        return ResponseEntity.ok()
            .headers(header)
            .contentLength(downloadFile.length())
            .contentType(MediaType.parseMediaType("application/octet-stream"))
            .body(resource);
    }
    

    The “application/octet-stream” media type is useful for providing the file as a raw type. The HttpHeader provides information about the file metadata, including the size of the file so that the browser can properly determine its size and create a proper download progress bar.

    Providing spring boot file download based on GET parameter

    Now, let’s say you want to provide multiple files for download and don’t want to write separate functions for each. In this case, you can simply accept the name of the file to be downloaded as a request parameter and use it to process the file downloading. Let’s see how this can be done in the following code snippet.

    Firefox download dialog from the spring boot file download link

    @GetMapping(path = "/your-api/download-file")
    public ResponseEntity<Resource> downloadBenchmarkFile10Mb(@RequestParam("file-name") String fileName) throws Exception {
      File downloadFile = new File(fileName);
      //Return 404 error if the file is not found
      if (!downloadFile.exists()) {
        return ResponseEntity.noContent()
            .build();
      }
      InputStreamResource resource = new InputStreamResource(new FileInputStream(downloadFile));
      HttpHeaders header = new HttpHeaders();
      header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + downloadFile.getName());
      header.add("Cache-Control", "no-cache, no-store, must-revalidate");
      header.add("Pragma", "no-cache");
      header.add("Expires", "0");
      header.add("hash", FileUtil.createFileHash(downloadFile));
      return ResponseEntity.ok()
          .headers(header)
          .contentLength(downloadFile.length())
          .contentType(MediaType.parseMediaType("application/octet-stream"))
          .body(resource);
    }
    

    Here, the file-name to be downloaded will be taken as an argument from the GET request and then will be used for resolving the file. If such a file is not available, then a 404 response will be returned, as you can see in the code comment. To download from the following REST GET endpoint, you can make a request as follows.

    https://yourserver/your-api/download-file?file-name=zulu.tar.gz
    

    That’s all about spring boot file download