MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.accessibility.AccessibleAction;
import javax.accessibility.AccessibleContext;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class MainClass extends JFrame {
    MainClass(String title) {
        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JToolBar toolBar = new JToolBar();
        Action a = new AbstractAction("Demo") {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Action taken.");
            }
        };

        JButton b = toolBar.add(a);
        b.setText("Demo Button");
        b.setToolTipText("Press me to take action.");

        JMenu mainMenu = new JMenu("Menu");
        JMenuItem mi = mainMenu.add(a);
        mi.getAccessibleContext().setAccessibleName("Menu item");

        JMenuBar mb = new JMenuBar();
        mb.add(mainMenu);
        setJMenuBar(mb);

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.setPreferredSize(new Dimension(200, 100));
        pane.add(toolBar, BorderLayout.NORTH);
        setContentPane(pane);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        MainClass ad2;
        ad2 = new MainClass("Accessibility Demo2");

        ad2.dumpActionInfo(ad2.getAccessibleContext());
    }

    void dumpActionInfo(AccessibleContext ac) {
        AccessibleAction aa = ac.getAccessibleAction();

        if (aa != null) {
            String s = ac.getAccessibleName();
            System.out.println(s);

            int count = aa.getAccessibleActionCount();

            for (int i = 0; i < count; i++) {
                s = aa.getAccessibleActionDescription(i);
                System.out.println("Description = " + s);
            }
        }

        int nChildren = ac.getAccessibleChildrenCount();

        for (int i = 0; i < nChildren; i++)
            dumpActionInfo(ac.getAccessibleChild(i).getAccessibleContext());
    }
}