Example usage for javax.swing JMenuBar revalidate

List of usage examples for javax.swing JMenuBar revalidate

Introduction

In this page you can find the example usage for javax.swing JMenuBar revalidate.

Prototype

public void revalidate() 

Source Link

Document

Supports deferred automatic layout.

Usage

From source file:AddingRemovingJMenu.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);/*  www .  j a  v a 2s.  c o m*/

    JMenu editMenu = new JMenu("Edit");
    menuBar.add(editMenu);

    menuBar.remove(0);

    menuBar.revalidate();

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:ActionsMenuBar.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("TextAction Usage");
    Container contentPane = frame.getContentPane();
    final JScrollPane scrollPane = new JScrollPane();
    contentPane.add(scrollPane, BorderLayout.CENTER);

    final JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);/*from   ww w.  ja  v  a2  s.  com*/

    ActionListener actionListener = new ActionListener() {
        JTextComponent component;

        public void actionPerformed(ActionEvent actionEvent) {
            // Determine which component selected
            String command = actionEvent.getActionCommand();
            if (command.equals("JTextField")) {
                component = new JTextField();
            } else if (command.equals("JPasswordField")) {
                component = new JPasswordField();
            } else if (command.equals("JTextArea")) {
                component = new JTextArea();
            } else if (command.equals("JTextPane")) {
                component = new JTextPane();
            } else {
                component = new JEditorPane();
            }
            scrollPane.setViewportView(component);
            // Process action list
            Action actions[] = component.getActions();
            menuBar.removeAll();
            menuBar.revalidate();
            JMenu menu = null;
            for (int i = 0, n = actions.length; i < n; i++) {
                if ((i % 10) == 0) {
                    menu = new JMenu("From " + i);
                    menuBar.add(menu);
                }
                menu.add(actions[i]);
            }
            menuBar.revalidate();
        }
    };

    String components[] = { "JTextField", "JPasswordField", "JTextArea", "JTextPane", "JEditorPane" };
    final Container componentsContainer = RadioButtonUtils.createRadioButtonGrouping(components,
            "Pick to List Actions", actionListener);
    contentPane.add(componentsContainer, BorderLayout.WEST);

    frame.setSize(400, 300);
    frame.setVisible(true);
}

From source file:edu.umich.robot.GuiApplication.java

/**
 * Entry point.//www.j  a va 2s . c  o  m
 * 
 * @param args Args from command line.
 */
public GuiApplication(Config config) {
    // Heavyweight is not desirable but it is the only thing that will
    // render in front of the Viewer on all platforms. Blame OpenGL
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);

    // must have config
    //Config config = (args.length > 0) ? ConfigUtil.getDefaultConfig(args) : promptForConfig(frame);
    if (config == null)
        System.exit(1);

    // Add more stuff to the config file that doesn't change between runs.
    Application.setupSimulatorConfig(config);
    setupViewerConfig(config);

    Configs.toLog(logger, config);

    controller = new Controller(config, new Gamepad());
    controller.initializeGamepad();

    viewer = new Viewer(config, frame);
    // This puts us in full 3d mode by default. The Viewer GUI doesn't
    // reflect this in its right click drop-down, a bug.
    viewer.getVisCanvas().getViewManager().setInterfaceMode(3);

    controller.addListener(RobotAddedEvent.class, listener);
    controller.addListener(RobotRemovedEvent.class, listener);
    controller.addListener(AfterResetEvent.class, listener);
    controller.addListener(ControllerActivatedEvent.class, listener);
    controller.addListener(ControllerDeactivatedEvent.class, listener);

    actionManager = new ActionManager(this);
    initActions();

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosed(WindowEvent e) {
            controller.shutdown();
            System.exit(0);
        }
    });
    frame.setLayout(new BorderLayout());

    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    fileMenu.add(actionManager.getAction(CreateSplinterRobotAction.class));
    fileMenu.add(actionManager.getAction(CreateSuperdroidRobotAction.class));
    fileMenu.add(new JSeparator());
    fileMenu.add(actionManager.getAction(ConnectSuperdroidAction.class));
    fileMenu.add(new JSeparator());
    fileMenu.add(actionManager.getAction(ResetPreferencesAction.class));

    /*
    fileMenu.add(new JSeparator());
    fileMenu.add(actionManager.getAction(TextMessageAction.class));
    */

    fileMenu.add(new JSeparator());
    fileMenu.add(actionManager.getAction(SaveMapAction.class));
    fileMenu.add(new JSeparator());
    fileMenu.add(actionManager.getAction(ExitAction.class));
    menuBar.add(fileMenu);

    JMenu cameraMenu = new JMenu("Camera");
    cameraMenu.add(actionManager.getAction(DisableFollowAction.class));
    cameraMenu.add(actionManager.getAction(FollowPositionAction.class));
    cameraMenu.add(actionManager.getAction(FollowPositionAndThetaAction.class));
    cameraMenu.add(new JSeparator());
    cameraMenu.add(actionManager.getAction(MoveCameraBehindAction.class));
    cameraMenu.add(actionManager.getAction(MoveCameraAboveAction.class));
    menuBar.add(cameraMenu);

    JMenu objectMenu = new JMenu("Objects");
    boolean added = false;
    for (String objectName : controller.getObjectNames()) {
        added = true;
        objectMenu.add(new AddObjectAction(this, objectName));
    }
    if (!added)
        objectMenu.add(new JLabel("No objects available"));
    menuBar.add(objectMenu);

    menuBar.revalidate();
    frame.setJMenuBar(menuBar);

    JToolBar toolBar = new JToolBar();
    toolBar.setFloatable(false);
    toolBar.setRollover(true);
    toolBar.add(actionManager.getAction(SoarParametersAction.class));
    toolBar.add(actionManager.getAction(SoarDataAction.class));
    toolBar.add(actionManager.getAction(ResetAction.class));
    toolBar.add(actionManager.getAction(SoarToggleAction.class));
    toolBar.add(actionManager.getAction(SoarStepAction.class));
    toolBar.add(actionManager.getAction(SimSpeedAction.class));
    frame.add(toolBar, BorderLayout.PAGE_START);

    viewerView = new ViewerView(viewer.getVisCanvas());
    robotsView = new RobotsView(this, actionManager);

    final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, viewerView, robotsView);
    splitPane.setDividerLocation(0.75);

    // TODO SoarApril
    /*
    viewer.addRobotSelectionChangedListener(robotsView);
    viewer.getVisCanvas().setDrawGround(true);
    */

    viewer.getVisCanvas().addEventHandler(new VisCanvasEventAdapter() {

        public String getName() {
            return "Place Object";
        }

        @Override
        public boolean mouseClicked(VisCanvas vc, GRay3D ray, MouseEvent e) {
            boolean ret = false;
            synchronized (GuiApplication.this) {
                if (objectToAdd != null && controller != null) {
                    controller.addObject(objectToAdd, ray.intersectPlaneXY());
                    objectToAdd = null;
                    ret = true;
                } else {
                    double[] click = ray.intersectPlaneXY();
                    chatView.setClick(new Point2D.Double(click[0], click[1]));
                }
            }
            status.setMessage(
                    String.format("%3.1f,%3.1f", ray.intersectPlaneXY()[0], ray.intersectPlaneXY()[1]));
            return ret;
        }
    });

    consoleView = new ConsoleView();
    chatView = new ChatView(this);
    controller.getRadio().addRadioHandler(chatView);

    final JSplitPane bottomPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, consoleView, chatView);
    bottomPane.setDividerLocation(200);

    final JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, splitPane, bottomPane);
    splitPane2.setDividerLocation(0.75);

    frame.add(splitPane2, BorderLayout.CENTER);

    status = new StatusBar();
    frame.add(status, BorderLayout.SOUTH);

    /*
    frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e)
    {
        final Preferences windowPrefs = getWindowPreferences();
        final Rectangle r = frame.getBounds();
        if(frame.getExtendedState() == JFrame.NORMAL)
        {
            windowPrefs.putInt("x", r.x);
            windowPrefs.putInt("y", r.y);
            windowPrefs.putInt("width", r.width);
            windowPrefs.putInt("height", r.height);
            windowPrefs.putInt("divider", splitPane.getDividerLocation());
        }
                
        exit();
    }});
            
    Preferences windowPrefs = getWindowPreferences();
    if (windowPrefs.get("x", null) != null)
    {
    frame.setBounds(
            windowPrefs.getInt("x", 0), 
            windowPrefs.getInt("y", 0), 
            windowPrefs.getInt("width", 1200), 
            windowPrefs.getInt("height", 900));
    splitPane.setDividerLocation(windowPrefs.getInt("divider", 500));
    }
    else
    {
    frame.setBounds(
            windowPrefs.getInt("x", 0), 
            windowPrefs.getInt("y", 0), 
            windowPrefs.getInt("width", 1200), 
            windowPrefs.getInt("height", 900));
    splitPane.setDividerLocation(0.75);
    frame.setLocationRelativeTo(null); // center
    }
    */

    frame.getRootPane().setBounds(0, 0, 1600, 1200);

    frame.getRootPane().registerKeyboardAction(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);

    frame.pack();

    frame.setVisible(true);

    for (String s : config.getStrings("splinters", new String[0])) {
        double[] pos = config.getDoubles(s + ".position");
        if (pos == null) {
            logger.error("Splinter indexed in config file but no position defined: " + s);
            continue;
        }

        Pose pose = new Pose(pos);
        String prods = config.getString(s + ".productions");
        boolean collisions = config.getBoolean(s + ".wallCollisions", true);

        controller.createSplinterRobot(s, pose, collisions);
        boolean simulated = config.getBoolean(s + ".simulated", true);
        if (simulated)
            controller.createSimSplinter(s);
        else
            controller.createRealSplinter(s);
        controller.createSimLaser(s);
        if (prods != null) {
            controller.createSoarController(s, s, prods, config.getChild(s + ".properties"));
            PREFERENCES.put("lastProductions", prods);
        }
    }

    for (String s : config.getStrings("superdroids", new String[0])) {
        double[] pos = config.getDoubles(s + ".position");
        if (pos == null) {
            logger.error("Superdroid indexed in config file but no position defined: " + s);
            continue;
        }

        Pose pose = new Pose(pos);
        String prods = config.getString(s + ".productions");
        boolean collisions = config.getBoolean(s + ".wallCollisions", true);

        controller.createSuperdroidRobot(s, pose, collisions);
        boolean simulated = config.getBoolean(s + ".simulated", true);
        if (simulated)
            controller.createSimSuperdroid(s);
        else {
            try {
                controller.createRealSuperdroid(s, "192.168.1.165", 3192);
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (SocketException e1) {
                e1.printStackTrace();
            }
        }
        controller.createSimLaser(s);
        if (prods != null) {
            // wait a sec
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
            controller.createSoarController(s, s, prods, config.getChild(s + ".properties"));
            PREFERENCES.put("lastProductions", prods);
        }
    }

}

From source file:org.apache.oodt.cas.workflow.gui.WorkflowGUI.java

public JMenuBar generateMenuBar() {
    JMenuBar bar = new JMenuBar();
    FileMenu fileMenu = new FileMenu();
    bar.add(fileMenu);//from   ww  w .  ja  v a2s.c  o  m
    fileMenu.getExit().addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            System.exit(1);
        }
    });

    fileMenu.getOpenWorkspace().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                JFileChooser chooser = new JFileChooser(new File(".")) {
                    boolean acceptFile(File f) {
                        return f.isDirectory();
                    }
                };
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int value = chooser.showOpenDialog(WorkflowGUI.this);
                if (value == JFileChooser.APPROVE_OPTION) {
                    workspace = chooser.getSelectedFile();
                    updateWorkspaceText();
                    perspective.reset();
                    loadProjects();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });
    fileMenu.getImport().addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            try {
                if (workspace == null) {
                    return;
                }
                JFileChooser chooser = new JFileChooser(new File("."));
                int value = chooser.showOpenDialog(WorkflowGUI.this);
                if (value == JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    XmlWorkflowModelRepositoryFactory factory = new XmlWorkflowModelRepositoryFactory();
                    factory.setWorkspace(workspace.getAbsolutePath());
                    View activeView = perspective.getActiveView();

                    if (activeView != null) {
                        // TODO: add code for import
                    }
                }
            } catch (Exception e) {
                LOG.log(Level.SEVERE, e.getMessage());
            }
        }

    });
    fileMenu.getNewWorkspace().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            JFileChooser chooser = new JFileChooser(new File(".")) {
                boolean acceptFile(File f) {
                    return f.isDirectory();
                }
            };
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int value = chooser.showOpenDialog(WorkflowGUI.this);
            if (value == JFileChooser.APPROVE_OPTION) {
                workspace = chooser.getSelectedFile();
                updateWorkspaceText();
                perspective.reset();
                loadProjects();
                perspective.refresh();
            }
        }
    });

    fileMenu.getNewProject().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            // TODO: add new project code
        }
    });
    fileMenu.getSave().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                repo.save();
            } catch (Exception e) {
                LOG.log(Level.SEVERE, e.getMessage());
            }
        }
    });
    EditMenu editMenu = new EditMenu();
    bar.add(editMenu);
    editMenu.getUndo().addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                perspective.undo();
            } catch (Exception e) {
                LOG.log(Level.SEVERE, e.getMessage());
            }
        }
    });
    bar.revalidate();
    return bar;
}

From source file:org.parosproxy.paros.extension.ExtensionLoader.java

private void addMenuHelper(JMenuBar menuBar, List<JMenuItem> items, int existingCount) {
    for (JMenuItem item : items) {
        if (item != null) {
            menuBar.add(item, menuBar.getMenuCount() - existingCount);
        }/* w  w  w .ja  v  a2s. c  o  m*/
    }
    menuBar.revalidate();
}

From source file:org.parosproxy.paros.extension.ExtensionLoader.java

private void removeMenuHelper(JMenuBar menuBar, List<JMenuItem> items) {
    for (JMenuItem item : items) {
        if (item != null) {
            menuBar.remove(item);//from   w  ww .j  a  v  a  2 s  .  c o m
        }
    }
    menuBar.revalidate();
}