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.