0

I'm trying to add a lock image to the title bar if the window has been set to always on top by using the stayOnTop(boolean status) method to call the displayLockIcon(boolean status) method. I've tried adding it to a JLabel and the adding that JLabel to the JFrame this.add(jLabelWithImage) and I've tried this.setImageIcon(image). So the question is how do I put an image on the title bar that will be displayed if the set always on top has been enabled? Oh and I'm using IntelliJ IDEA IDE and it's gui form.

package com.negassagisila.axeereraa;

import javax.swing.*;
import java.awt.*;

public class AxeereraaUI extends JFrame { 
    private JPanel axRootPanel;
    private JScrollPane axRootScrollPane;
    private JTextArea axRootTextArea;
    private JMenuBar axMenuBar;


    public AxeereraaUI() {
     UIManager.setLookAndFeel(axRunner.getLookAndFeel());

    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    add(axRootPanel);
    setSize(300, 250);
    setTitle("Axeereraa");

    buildUI();
    setJMenuBar(axMenuBar);
}  

private void buildUI() {
    axMenuBar = new JMenuBar();

 axRootScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

 axRootScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

 axRootScrollPane.getVerticalScrollBar().setPreferredSize(
            new Dimension(4, Integer.MAX_VALUE));
  /**
     * as the name suggests this sets up the JMenus and their 
 corresponding JMenuItems
     */
    SetUpMenuAndMenuItems setUpMenuAndMenuItems = new 
    SetUpMenuAndMenuItems().invoke();
    JMenu fileMenu = setUpMenuAndMenuItems.getFileMenu();
    JMenu editMenu = setUpMenuAndMenuItems.getEditMenu();
    JMenu viewMenu = setUpMenuAndMenuItems.getViewMenu();
    JMenu helpMenu = setUpMenuAndMenuItems.getHelpMenu();

    axMenuBar.add(fileMenu);
    axMenuBar.add(editMenu);
    axMenuBar.add(viewMenu);
    axMenuBar.add(helpMenu);

    axRootTextArea.setLineWrap(true);
    axRootTextArea.setWrapStyleWord(true);
}

   /**
   * This method is responsible for setting the application always on top
   * @param status boolean value to be passed to the instance of the UI.
   */

  private void stayOnTop(boolean status) {

    /**
     * called on every instance of the UI, method from the JFrame class.
     */

    this.setAlwaysOnTop(status);

    /**
     * changes the icon to lock to show that the result
     */

    displayLockIcon(status);
  }

  /**
   * changes the icon to lock to show that the result
   * @param status boolean value of that checks if the always on top has been set
   */

  private void displayLockIcon(boolean status) { 
    if(status) {
    final JLabel jLabelWithImage = new JLabel(new 
    ImageIcon("src/main/resources/images/lock.png"));
    this.add(jLabelWithImage);
    //ImageIcon imageIcon = new 
    ImageIcon("src/main/resources/images/lock.png");
    //this.setImageIcon(imageIcon); 
 }
 }  

Thanks.

NegassaB
  • 379
  • 7
  • 24
  • related? https://stackoverflow.com/questions/6403821/how-to-add-an-image-to-a-jframe-title-bar – Jocke Mar 23 '19 at 12:34
  • "and I've tried jFrame.setImageIcon(image)" that looks like correct way to do what you want. What happened when you used it? – Pshemo Mar 23 '19 at 12:48
  • @Pshemo absolutely nothing. The image didn't appear on the title bar of my application. – NegassaB Mar 23 '19 at 13:29
  • What kind of image did you try to use as icon? What is its type and size (in pixels)? How *exactly* did you read it and what was passed as `setImageIcon` argument? – Pshemo Mar 23 '19 at 13:32
  • png file, size was 12.5 by 12.5. I read it by instantiating a new ImageIcon and chaining it to getImage() and passing that as an argument to setImageIcon(). – NegassaB Mar 23 '19 at 14:21
  • 1
    *"size was 12.5 by 12.5"* Furlongs or cubits? Tip: Add @Pshemo (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Mar 23 '19 at 14:52
  • Also don't *only* describe your code. Use [edit] option and *show* it, preferably as [mcve]. – Pshemo Mar 23 '19 at 14:54
  • Pixels. And sorry about the code, I'm using mobile that's why I can't put here. Will do as soon as I can. – NegassaB Mar 23 '19 at 15:22
  • @Pshemo size is 18px by 18px, not 12.5px by 12.5px. Sorry – NegassaB Mar 25 '19 at 12:14
  • What you posted is still not [mcve]. "Minimal" mens that it shouldn't contain anything unrelated to the problem, and "Complete" means that it must contain all elements needed to reproduce it. Your example uses `Note` which you didn't provide so we can't compile it, but tbh you shouldn't provide it, you should probably remove it from example since it most likely has nothing to do with preparing frame and setting its icon. BTW your example has commented `this.setImageIcon(imageIcon);` so it can't be executed which is *current* cause of icon not being set. – Pshemo Mar 25 '19 at 12:31
  • So please take your time and invest it in creating proper [MCVE] (a.k.a. [SSCCE](http://sscce.org)). Preferably start from scratch and focus only on setting icon to JFrame. Moment when you face *first* problem should be moment when you ask a question and show your current code. EDIT, actually let me correct myself. Moment when you face first problem should be moment when you start *researching it* (like search difference between approach in your code and one from duplicate question - linked above yours). – Pshemo Mar 25 '19 at 12:36
  • @Pshemo I commented this.setImageIcon(imageIcon) b/c I was trying this.add(jLabelWithImage) on this code but rest assured both don't work. I'll edit the question to make it more descriptive for you. – NegassaB Mar 25 '19 at 13:06
  • OK, it looks like there is no `setImageIcon` but `setIconImage` method and that method expects `Image` not `ImageIcon` (which isn't subtype of `Image`). To create object of Image you can use `ImageIO.read(..)` like `ImageIO.read(new File("src/main/resources/images/lock.png"))` (you will need to handle exceptions in case image is not at specified location). BUT preferred way would be using instead of `File` URL of *resource* because most likely you will change your code into JAR from which you will not be able to use `File` as resource locator (more info at https://stackoverflow.com/q/9864267). – Pshemo Mar 25 '19 at 13:42
  • @Pshemo but why isn't it working when I use the JLabel? I thought that would have made it very simple. – NegassaB Mar 25 '19 at 14:10
  • @Pshemo ...and by using setIconImage(image), it sets the Icon of the application. It doesn't add an image to the title bar (which is what I want) – NegassaB Mar 25 '19 at 14:19
  • Sorry but I am not sure what you mean by "but why isn't it working when I use the JLabel?". *What* isn't working, *how* and *when* are you using JLablel? What would you expect to happen then and what happens instead? About: "It doesn't add an image to the title bar" I can't reproduce this problem. If I use code like https://pastebin.com/KDy0cQup I can change icon on titlebar from https://i.stack.imgur.com/qfcDa.png to https://i.stack.imgur.com/X0ILJ.png. – Pshemo Mar 25 '19 at 14:29
  • @Pshemo what I mean when I say "but why isn't it working when I use the JLabel?" is ```final JLabel jLabelWithImage = new JLabel(new ImageIcon("src/main/resources/images/lock.png")); this.add(jLabelWithImage);``` doesn't addn't add the jLabelWithImage to the title bar. – NegassaB Mar 26 '19 at 07:42
  • @Pshemo and I'm using ubuntu 18.04 with Gnome desktop env't, could that be what's affecting it? – NegassaB Mar 26 '19 at 07:42
  • @Pshemo as to the code you posted it changes the layout from GridLayout to FlowLayout. Do I have to do the same to get it to work? – NegassaB Mar 26 '19 at 07:43
  • "doesn't add the jLabelWithImage to the title bar" but why *should* it add it to title bar? Label and title bar are unrelated things/concepts. Label is used to place some image/text on form's *canvas* (like before each JTextField to describe what data should be placed in them), NOT in form's title. Ad. changing GridLayout to FlowLayout: it shouldn't matter. I used FlowLayout because it was easier for me at the moment. "I'm using ubuntu 18.04 with Gnome" IMO it also shouldn't matter since Java should work same way on all systems (but that is only my guess and I can't confirm it). – Pshemo Mar 26 '19 at 11:57
  • @Pshemo so this where I'm basically at. the ```this.setIconImage()``` method does change the icon of the application. But that's not what i wanted. I wanted the image to appear on the title bar. Ad. I'll try changing the layout and see how that works. Ad. for the ubuntu thing, perhaps I'll run it on a win 7 computer and see how it goes. – NegassaB Mar 27 '19 at 11:22
  • @Pshemo But I'm seriously considering changing the place where the image will appear, perhaps on the top right corner of the JPanel I'm using. – NegassaB Mar 27 '19 at 11:23
  • Well, title's icon can't change size of title-bar so if it is larger than that bar it will be scaled down to height of title-bar. If you don't want to reduce its size, then you are right about not placing it in there. – Pshemo Mar 27 '19 at 12:03
  • @Pshemo yea I'm just gonna try to put it on the top right side of the JPanel or the JTextArea. – NegassaB Mar 27 '19 at 13:50

0 Answers0