Search code examples
javaswingjtablejpaneljdialog

How to remove red hat icon from top left corner Java Swing


Is there a way of removing the rhel red hat icon from top left corner in JDialog window ? Purpose is to have only the text "Activities". red hat icon

I have tried to search from where is coming that image, but no success.

Can you please help ?

Thanks


Solution

  • The only thing I can think of of hand is the same thing as what was suggested by @user16320675. Create and use a small transparent empty image to replace the default image. This does however leave an icon sized gap between the left edge of the frame and the text. It may be a good idea to try and center the text within the title Bar.

    enter image description here

    You can try something like this provided method. Read comments in any of the following code:

    /**
     * Replaces the Title Bar Icon with a created blank (tranparent) .png image.
     * 
     * @param comp (java.awt.Component) Any Component variable name related to 
     * the JFrame or JDialog.
     */
    public void noFormIcon(java.awt.Component comp) {
        // Create an empty .png image
        java.awt.image.BufferedImage image = 
                new java.awt.image.BufferedImage(5, 5, java.awt.image.BufferedImage.TYPE_INT_ARGB);
        java.awt.Graphics2D g2d = image.createGraphics();
        g2d.setComposite(java.awt.AlphaComposite.Clear);
        g2d.fillRect(0, 0, 5, 5);
        g2d.dispose();
    
        // Apply the created Image to the Component's Parent Window.
        java.awt.Window win = javax.swing.SwingUtilities.getWindowAncestor(comp);
        if (win != null) {
            win.setIconImage(image);
        }
    }
    

    To use this method you might have something like this:

    // Instantiate a JDialog Window:
    javax.swing.JDialog dia = new javax.swing.JDialog();
    dia.setTitle("Some Dialog Window"); // Set dialog title.
    // Set its size:
    dia.setSize(300, 200);              // Set dialog size.
    dia.setAlwaysOnTop(true);           // Set 'Always On Top'
    dia.setModal(true);                 // Set dialog to be Modal.
    // Dispose of dialog when closed.
    dia.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    dia.setLocationRelativeTo(null);    // center dialog to Screen
    
    // Instantiate a JLabel and applpy a caption:
    javax.swing.JLabel label = new javax.swing.JLabel("Some JLabel In Dialog Window");
        
    // Set the Horizontal Alignment for the Caption text to be Center.
    label.setHorizontalAlignment(javax.swing.JLabel.CENTER);
        
    // Add the JLabel to the JDialog
    dia.add(label);
        
    // Replace the dialog title Bar Icon Image with an empty .png:
    noFormIcon(dia);
        
    // Display the dialog.
    java.awt.EventQueue.invokeLater(() -> {
        dia.setVisible(true);   
    });