PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   27-07-2021

Menu

! You can attach a menu to a frame. By convention, the menus aren't placed with the other components in the UI. A menu usually appears either in a menu bar or as a popup menu.
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class App extends JFrame {

    public static void main(String[] args) {
    
        App frame = new App();
    
        frame.setBounds(200, 200, 400, 300);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
    }

    public App() {
        initMenu();
    }

    public void initMenu() {

        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        JMenuItem menuItemNew = new JMenuItem("New");
        JMenuItem menuItemOpen = new JMenuItem("Open");

        menu.setMnemonic('F');
        menuItemNew.setMnemonic('N');
        menuItemOpen.setMnemonic('O');

        menuBar.add(menu);
        menu.add(menuItemNew);
        menu.add(menuItemOpen);
            
        // View
        JMenu menuView = new JMenu("View") {{
            setMnemonic('V');
            add (new JMenu("Toolbars") {{
                 add (new JCheckBoxMenuItem("Buttons") {{
                    setMnemonic('B');
                    addActionListener(new ActionListener() {
                        @Override public void actionPerformed(ActionEvent e) {
                            //
                        }
                    });
                }});
            }});
        }};
        menuBar.add(menuView); 

        this.setJMenuBar(menuBar);  // Look Here
    }
}
... 38 lines
 
Switch panels on menu actions.
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class App extends JFrame {

    public static void main(String[] args) {
        App frame = new App();
        frame.setBounds(200, 200, 600, 400);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
    }

    public App() {
        initMenu();
    }

    public void initMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menuFile = new JMenu("File");
        JMenuItem menuItemNew = new JMenuItem("New");
        JMenu menuOpen = new JMenu("Open");
        JMenuItem submenuOpen1 = new JMenuItem("Open submenu");

        menuFile.setMnemonic('F');
        menuItemNew.setMnemonic('N');
        menuOpen.setMnemonic('O');

        menuBar.add(menuFile);
        menuFile.add(menuItemNew);
        menuFile.add(menuOpen);
        menuOpen.add(submenuOpen1);

        setJMenuBar(menuBar);
        
        // Look here
        menuItemNew.addActionListener(new MenuAction(new Action1()));
        submenuOpen1.addActionListener(new MenuAction(new Action2()));
    }

    private class MenuAction implements ActionListener {

        private JPanel panel;
        private MenuAction(JPanel panel) {
            this.panel = panel;
        }

        @Override
        public void actionPerformed(ActionEvent e) { // Look Here
            changePanel(panel);
        }
    }

    public void changePanel(JPanel panel) {
        getContentPane().removeAll();
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().doLayout();
        validate();
        repaint();
    }
}

class Action1 extends JPanel { // Look Here
    
    public Action1() {
        
        setBackground(Color.gray);

        JLabel message = new JLabel();
        message.setPreferredSize(new Dimension(440, 27));
        message.setForeground(Color.white);
        message.setBorder(BorderFactory.createLineBorder(Color.white));
        message.setText("Action 1");

        add(message);
    }
}

class Action2 extends JPanel { // Look Here
    
    public Action2() {
        
        setBackground(Color.black);

        JLabel message = new JLabel();
        message.setForeground(Color.white);
        message.setBorder(BorderFactory.createLineBorder(Color.red));
        message.setText("Action 2");

        add(message);
    }
}
... 65 lines
 
Default Action    (2/2)

Default action

Set the default action on window open
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class App extends JFrame {

    public static void main(String[] args) {
        App frame = new App();
        frame.setBounds(200, 200, 600, 400);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
    }

    public App() {
        
        initMenu();
                
        // default action
        this.addWindowListener( new WindowAdapter() { // Look Here
            @Override
            public void windowOpened( WindowEvent e ){
                changePanel(new Action2());
            }
        });
    }

    public void initMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menuFile = new JMenu("File");
        JMenuItem menuItemNew = new JMenuItem("New");
        JMenuItem menuItemOpen = new JMenuItem("Open");

        menuFile.setMnemonic('F');

        menuBar.add(menuFile);
        menuFile.add(menuItemNew);
        menuFile.add(menuItemOpen);

        setJMenuBar(menuBar);
        
        menuItemNew.addActionListener(new MenuAction(new Action1()));
        menuItemOpen.addActionListener(new MenuAction(new Action2()));
    }

    private class MenuAction implements ActionListener {

        private JPanel panel;
        private MenuAction(JPanel panel) {
            this.panel = panel;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            changePanel(panel);
        }
    }

    public void changePanel(JPanel panel) {
        getContentPane().removeAll();
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().doLayout();
        validate();
        repaint();
    }
}

class Action1 extends JPanel {
    
    public Action1() {
        
        setBackground(Color.gray);

        JLabel message = new JLabel();
        message.setPreferredSize(new Dimension(440, 27));
        message.setForeground(Color.white);
        message.setBorder(BorderFactory.createLineBorder(Color.white));
        message.setText("Action 1");

        add(message);
    }
}

class Action2 extends JPanel {
    
    public Action2() {
        
        setBackground(Color.black);

        JLabel message = new JLabel();
        message.setForeground(Color.white);
        message.setBorder(BorderFactory.createLineBorder(Color.red));
        message.setText("Action 2");

        add(message);
    }
}
... 67 lines
 

Look & Fill

! Look refers to the appearances of GUI widgets (or JComponents). Feel refers to the way the widgets behave.
 
import javax.swing.*;
import java.awt.*;

public class App extends JFrame {

    public static void main(String[] args) {

        setLookAndFill();
        
        App frame = new App();
        frame.setBounds(200, 200, 250, 200);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
    }

    private static void setLookAndFill() {
        
        /**
         * Nimbus look and fill (introduced in Java SE 6)
         * If not available keep default
         */
        try {
            /*UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
            );*/
              
            for (javax.swing.UIManager.LookAndFeelInfo info : 
                    javax.swing.UIManager.getInstalledLookAndFeels()) {

                if ("Nimbus".equals(info.getName())) { // Look Here
                    javax.swing.UIManager.setLookAndFeel(
                        info.getClassName()
                    );
                    break;
                }
            }
        } catch (Exception ex) {}
    }

    public App() {
        initMenu();
    }

    public void initMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        JMenuItem menuItemNew = new JMenuItem("New");
        JMenuItem menuItemOpen = new JMenuItem("Open");

        menu.setMnemonic('F');
        menuItemNew.setMnemonic('N');
        menuItemOpen.setMnemonic('O');

        menuBar.add(menu);
        menu.add(menuItemNew);
        menu.add(menuItemOpen);

        this.setJMenuBar(menuBar);
    }
}
... 42 lines
 

Questions    
Table

        A B C D E F
🔔
1/2