0

jframe title bar image (run time)

1

I don't want it undecorated, want black color and change icons.

I am making my college project, I tried making a customized title bar with an undecorated frame, and it works fine, but I have problems dragging the frame and re-sizing it. So I thought maybe I could edit the original frame but didn't find any solutions. Can someone guide me through this?

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • You can add an icon to the title bar, but that's the only change you can make. The workaround, as you've already found, is to create your own title bar with an undecorated `JFrame`. You then have to implement all the functionality of a title bar yourself. – Gilbert Le Blanc Feb 04 '23 at 19:25
  • I am having problems adding all the functionality in the custom title bar, and also I lack the knowledge. Can you help me with this? – luci_the_coder Feb 04 '23 at 20:10
  • 1
    Check out: https://stackoverflow.com/a/47871969/131872 for the basic logic for dragging any component around a JPanel. However you will need to make a single change because the component you want to drag is the frame, not the component you click on. So in the mouseDragged logic you will need to find the parent frame of the panel you click on. Check out the `SwingUtilities` class for a method to help you. – camickr Feb 04 '23 at 21:49
  • 1
    Resizing the frame will be more complicated because you need to know which side of the frame you are resizing. You will also need to know if the mouse is positioned in a corner of the frame in which case you will need to resize two sides of the frame. But the concept is the same, you need to add the listeners to the frame and then do you calculations based on the mouse position in the frame. – camickr Feb 04 '23 at 21:51

1 Answers1

-1

Assumption : You need a by-default decorated JFrame, which acquires the look & feel of the machine's operating system.

Setting an icon and customizing text on Title bar is easy.It can be done as shown below,

class TitleBar {     
TitleBar(){     
Frame f=new Frame(); 

//Setting TitleBar icon with "YourImage.png" 
       f.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("YourImage.png")));

//set other parameters as per your choice viz size layout etc.  

//Set the Title Bar text
f.setTitle("YOUR CUSTOM TEXT");

f.setVisible(true);     

}     

Now with the frame decoration turned "ON", it is not possible to change the color of titlebar, font of the top-level JFrame (to the best of my knowledge). You CAN change the color of an InternalFrame however.

If you want to customise these then turn off windows decorations with f.setUndecorated(true) and you can change the title bar appearance with the help of LAF (Look And Feel). This is better explained in the answer below :

Change the color of the title bar in javax.swing

You may also try modifying the UIDefaults as shown here :

Modifying UIDefaults | For Windows 10 and above

Do upvote if my answer is useful !!