Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.Color;

import javax.swing.JComponent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;

import javax.swing.JPopupMenu;

import javax.swing.MenuElement;

public class Main {
    /**
     * change background and foreground color for menu bar
     * @param aMenuBar menu bar
     * @param aBackgroundColor background color
     * @param aForegroundColor foreground color
     */
    public final static void ChangeColor(MenuElement aMenuItem, Color aBackgroundColor, Color aForegroundColor) {
        if (aMenuItem instanceof JPopupMenu) {
            ((JPopupMenu) aMenuItem).setBorderPainted(true);
        }
        if (aBackgroundColor != null) {
            ((JComponent) aMenuItem).setBackground(aBackgroundColor);
        }
        if (aForegroundColor != null) {
            ((JComponent) aMenuItem).setForeground(aForegroundColor);
        }
        MenuElement[] elements = aMenuItem.getSubElements();
        for (int count = 0; count < elements.length; count++) {
            ChangeColor(elements[count], aBackgroundColor, aForegroundColor);
        }
    }

    /**
     * change background and foreground color for menu bar
     * @param aMenuBar menu bar
     * @param aBackgroundColor background color
     * @param aForegroundColor foreground color
     */
    public final static void ChangeColor(JMenuBar aMenuBar, Color aBackgroundColor, Color aForegroundColor) {
        if (aBackgroundColor != null) {
            aMenuBar.setBackground(aBackgroundColor);
        }
        if (aForegroundColor != null) {
            aMenuBar.setForeground(aForegroundColor);
        }
        JMenu menu = null;
        for (int count = 0; count < aMenuBar.getMenuCount(); count++) {
            menu = aMenuBar.getMenu(count);
            ChangeColor(menu, aBackgroundColor, aForegroundColor);
        }
    }
}