Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Change Title bar color of JDialog box?

800299Aug 7 2009 — edited Aug 7 2009
the following code change the title bar color of JDialog box but also show dots of default Title bar color
       JDialog.setDefaultLookAndFeelDecorated(true);
      UIManager.put("activeCaption", new ColorUIResource(Color.RED));
complete Code
import java.awt.*;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalInternalFrameTitlePane;
import javax.swing.plaf.metal.MetalInternalFrameUI;
/**
 *
 * @author muneer.ahmed
 */
public class NewJDialog extends javax.swing.JDialog {

    /** Creates new form NewJDialog */
    public NewJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
      JDialog.setDefaultLookAndFeelDecorated(true);
      UIManager.put("activeCaption", new ColorUIResource(Color.RED));
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewJDialog dialog = new NewJDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    // End of variables declaration

     class ColorTitlebarInternalFrameUI extends MetalInternalFrameUI {

        JInternalFrame frame1 = null;

        ColorTitlebarInternalFrameUI(JInternalFrame jif) {
            super(jif);
            frame1 = jif;
        }

        protected JComponent createNorthPane(JInternalFrame jif) {

            MetalInternalFrameTitlePane titlePane = new MetalInternalFrameTitlePane(jif) {

                @Override
                public void paintComponent(Graphics g) {
                    //super.paintComponent (g);

                    Graphics2D g2 = (Graphics2D) g;
                    g2.setBackground(new Color(getBackground().getRGB() | 0x01000000));
                    g2.clearRect(0, 0, getWidth(), getHeight());

                    g.setColor(getForeground());
                    g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);

                    int titleLength = 0;
                    int xOffset = 5;
                    String frameTitle = frame1.getTitle();

                    Icon icon = frame1.getFrameIcon();
                    if (icon != null) {
                        int iconY = ((getHeight() / 2) - (icon.getIconHeight() / 2));
                        icon.paintIcon(frame1, g, xOffset, iconY);
                        xOffset += icon.getIconWidth() + 5;
                    }

                    if (frameTitle != null) {
                        Font f = getFont();
                        g.setFont(f);
                        FontMetrics fm = getFontMetrics(getFont());
                        int fHeight = fm.getHeight();
                        int yOffset = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();

                        Rectangle rect = new Rectangle(0, 0, 0, 0);
                        if (frame1.isIconifiable()) {
                            rect = iconButton.getBounds();
//                        } else if (frame1.isMaximizable()) {
//                            rect = maxButton.getBounds();
                        } else if (frame1.isClosable()) {
                            rect = closeButton.getBounds();
                        }
                        int titleW;

                        if (rect.x == 0) {
                            rect.x = frame.getWidth() - frame.getInsets().right - 2;
                        }
                        titleW = rect.x - xOffset - 4;
                        frameTitle = getTitle(frameTitle, fm, titleW);

                        titleLength = fm.stringWidth(frameTitle);
                        g2.drawString(frameTitle, xOffset, yOffset);
                        xOffset += titleLength + 5;
                    }
                }
            };
            return titlePane;
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 4 2009
Added on Aug 7 2009
1 comment
1,495 views