0

I generally use visual studio code or IntelliJ for coding.

In that, I see that the title bar is coloured. Not only in that but in many apps I have seen that. Now I wish to try it out for my own applications also. But I haven't found anything that can accomplish my work.

I generally want like this.

jframe.setTitleBarColor(...)

But the above type is not available. Is there any other technique that I can use to achieve that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ruthvik
  • 790
  • 5
  • 28
  • Have you tried to change the UIManager properties? https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html – jhamon Oct 06 '21 at 13:56
  • The color of the title bar is determined by the OS. – camickr Oct 06 '21 at 14:07
  • So is there no way to do that? But intellij has black(in darkula), vscode has black and MS teams has blue color. So why cant I keep that for my apps also.. – Ruthvik Oct 06 '21 at 14:52
  • I don't know what language Intellij is written in. Maybe Intellij uses OS system calls to control the background. The Java/Swing API does not give us access to the background of the titlebar. – camickr Oct 06 '21 at 15:13
  • Thank you everyone for spending your time on me. And for your suggestions ☺️☺️ – Ruthvik Oct 06 '21 at 15:17
  • @Ruthvik may be there is a start with https://stackoverflow.com/a/876954/4693472 but it means to draw it yourself – Laurent G Oct 06 '21 at 16:42
  • See [`setDefaultLookAndFeelDecorated`](https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html#setDefaultLookAndFeelDecorated) to have the title bar painted according to the PLAF. – Andrew Thompson Oct 06 '21 at 16:46

1 Answers1

2

I think it's not possible. Because the top-level JFrame acquires the look & feel of the machine's operating system.

By the way this program will help you to change the frame appearance with the help of LAF (Look And Feel):

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class Main {
  public static void main(final String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 300);
    f.setLocationRelativeTo(null);

    f.setUndecorated(true);
    f.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

    JPanel panel = new JPanel();
    panel.setBackground(java.awt.Color.white);
    f.setContentPane(panel);

    MetalLookAndFeel.setCurrentTheme(new MyDefaultMetalTheme());
    try {
      UIManager.setLookAndFeel(new MetalLookAndFeel());
    } catch (Exception e) {
      e.printStackTrace();
    }

    SwingUtilities.updateComponentTreeUI(f);

    f.setVisible(true);
  }
}

class MyDefaultMetalTheme extends DefaultMetalTheme {
  public ColorUIResource getWindowTitleInactiveBackground() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getWindowTitleBackground() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getPrimaryControlHighlight() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getPrimaryControlDarkShadow() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getPrimaryControl() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getControlHighlight() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getControlDarkShadow() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getControl() {
    return new ColorUIResource(java.awt.Color.orange);
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433