Example usage for javax.swing JButton setActionCommand

List of usage examples for javax.swing JButton setActionCommand

Introduction

In this page you can find the example usage for javax.swing JButton setActionCommand.

Prototype

public void setActionCommand(String actionCommand) 

Source Link

Document

Sets the action command for this button.

Usage

From source file:Main.java

public static JButton getButton(String displayText, ActionListener actionListener, String name) {
    JButton button = new JButton(displayText);
    button.setActionCommand(displayText);
    button.addActionListener(actionListener);

    if (name != null && !"".equals(name)) {
        button.setName(name);//  w  w  w  . ja  v a2s.  c om
    }

    return button;
}

From source file:Main.java

/**
 * @param okText//from   w  ww  .j ava2 s  . c om
 * @param listener
 * @param root 
 * @return newly created JPanel with ok and cancel buttons
 */
public static JPanel createOkPanel(String okText, ActionListener listener, JRootPane root) {
    JPanel panel = new JPanel();
    JButton ok = new JButton(okText);
    panel.add(ok);
    ok.addActionListener(listener);
    ok.setActionCommand(okText);
    if (root != null)
        root.setDefaultButton(ok);
    return panel;
}

From source file:Main.java

/**
 * @param label/*w  w  w.jav  a 2  s  .  co m*/
 * @param icon
 * @return createb JButton used in toolbar
 */
public static JButton createToolbarButton(String label, Icon icon) {
    JButton button = new JButton(icon);
    button.setFocusPainted(false);
    button.setMargin(new Insets(0, 0, 0, 0));
    button.setToolTipText(label);
    button.setActionCommand(label);
    return button;
}

From source file:Main.java

/**
 * @param label//www. ja  v a 2 s .c  o  m
 * @param iconFileName
 * @return createb JButton used in toolbar
 */
public static JButton createToolbarButton(String label, String iconFileName) {
    JButton button = new JButton(new ImageIcon(iconFileName));
    button.setFocusPainted(false);
    button.setMargin(new Insets(0, 0, 0, 0));
    button.setToolTipText(label);
    button.setActionCommand(label);
    return button;
}

From source file:Main.java

/**
 * @param okText//www. jav  a2  s. com
 * @param cancelText
 * @param listener
 * @param root 
 * @return newly created JPanel with ok and cancel buttons
 */
public static JPanel createOkCancelPanel(String okText, String cancelText, ActionListener listener,
        JRootPane root) {
    JPanel panel = new JPanel();
    JButton ok = new JButton(okText);
    JButton cancel = new JButton(cancelText);
    panel.add(ok);
    panel.add(cancel);
    ok.addActionListener(listener);
    ok.setActionCommand(okText);
    if (root != null)
        root.setDefaultButton(ok);
    cancel.addActionListener(listener);
    cancel.setActionCommand(cancelText);
    return panel;
}

From source file:levelBuilder.DialogMaker.java

/**
 * First window to interact with. Offers options of load graph or new graph.
 *//*from   w w  w  . j av  a  2s.  c  o  m*/
private static void splashWindow() {
    final JFrame frame = new JFrame("Dialog Maker");

    //Handles button pushes.
    class SplashActionHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
            if (e.getActionCommand().equals("loadGraph"))
                loadFilePopup();
            else
                loadGraph(true);
        }
    }

    //Loads and sets background image.
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File(imgDir + "splash.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    final BufferedImage img2 = img;
    JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img2, 0, 0, null);
        }
    };

    panel.setOpaque(true);
    panel.setLayout(new BorderLayout());
    panel.setPreferredSize(new Dimension(800, 600));
    panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 300, 0));

    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttonPanel.setOpaque(false);

    //Buttons
    JButton loadMap = new JButton("Load Graph");
    loadMap.setActionCommand("loadGraph");
    loadMap.addActionListener(new SplashActionHandler());
    buttonPanel.add(loadMap);

    JButton newMap = new JButton("New Graph");
    newMap.setActionCommand("newGraph");
    newMap.addActionListener(new SplashActionHandler());
    buttonPanel.add(newMap);

    panel.add(buttonPanel, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:levelBuilder.DialogMaker.java

/**
 * Provides other action buttons./*w  ww.j a v a 2s . co  m*/
 */
private static void actionWindow() {
    //Performs actions based on button pushes.
    class ActionHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            switch (e.getActionCommand()) {
            case "edge":
                if (addedEdgeID != 0.0) {
                    DialogNode n = g.getSource(addedEdgeID);
                    int length;
                    if (n.getChildren().length == 0)
                        length = 0;
                    else
                        length = n.getChildren().length;
                    DialogNode[] newChildren = new DialogNode[length + 1];
                    for (int c = 0; c < newChildren.length - 1; c++)
                        newChildren[c] = n.getChildren()[c];
                    newChildren[newChildren.length - 1] = nodeMap.get(g.getDest(addedEdgeID).getText());
                    n.setChildren(newChildren);
                    addedEdgeID = 0.0;
                }
            case "save":
                saveGraph();
            case "check":
                //Checks for errors and displays them if they exist.
                ArrayList<String> errorList = new ArrayList<String>();
                errorList = dg.check();
                if (errorList.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "No errors!", null, JOptionPane.PLAIN_MESSAGE);
                } else {
                    JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));
                    for (String error : errorList)
                        panel.add(new JLabel(error));
                    JOptionPane.showMessageDialog(null, panel, "Errors. Fix these to continue.",
                            JOptionPane.PLAIN_MESSAGE);
                }
            case "reload":
                loadGraph(false);
            }
        }
    }

    JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));

    //Buttons
    JButton button3 = new JButton("New edge");
    button3.setActionCommand("edge");
    button3.addActionListener(new ActionHandler());
    panel.add(button3);

    JButton button6 = new JButton("Save");
    button6.setActionCommand("save");
    button6.addActionListener(new ActionHandler());
    panel.add(button6);

    JButton button7 = new JButton("Check");
    button7.setActionCommand("check");
    button7.addActionListener(new ActionHandler());
    panel.add(button7);

    JButton button1 = new JButton("Reload");
    button1.setActionCommand("reload");
    button1.addActionListener(new ActionHandler());
    panel.add(button1);

    JFrame frame = new JFrame("Other Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(panel);
    frame.pack();
    frame.setLocation(windowWidth + horizontalWindowPlacement, 0);
    frame.setVisible(true);
}

From source file:levelBuilder.DialogMaker.java

/**
 * Displays properties of the currently selected node.
 * Also allows changing of most node properties. 
 *//* www .ja v  a 2  s  .  co  m*/
private static void nodePropertiesWindow() {
    //Performs actions based on button pushes.
    class ActionHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            switch (e.getActionCommand()) {
            case "probSet":
                //parsing new probSet string to double[]
                ArrayList<double[]> probSets = selectedNode.getProbSets();
                if (probSetField.getText().equals(""))
                    probSets.remove(strategy);
                else {
                    String[] probSetInput = probSetField.getText().split(",");
                    int numChildren = probSetInput.length;
                    double[] newProbSet = new double[numChildren];
                    for (int c = 0; c < numChildren; c++)
                        newProbSet[c] = Double.parseDouble(probSetInput[c]);
                    if (probSets.size() > strategy)//TODO is this right?
                        probSets.add(strategy, newProbSet);
                    else
                        probSets.set(strategy, newProbSet);
                }
                selectedNode.setProbSets(probSets);
            case "npc":
                if (isNPCBoxChecked)
                    selectedNode.setNPC();
                else
                    selectedNode.setPC();
            case "text":
                dg.changeNodeText(selectedNode, textField.getText());
            case "strategy":
                strategy = Integer.parseInt(strategyField.getText());
            }
        }
    }

    JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 0, 0));
    JPanel labelPanel = new JPanel(new GridLayout(0, 1, 0, 0));
    JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 0, 0));
    JPanel masterPanel = new JPanel(new GridLayout(0, 3, 0, 0));

    //Buttons, ckeckboxes, textboxes, and labels
    textField = new JTextField("May I buy your wand?", 10);
    fieldPanel.add(textField);
    labelPanel.add(new JLabel("Node Text"));
    JButton button1 = new JButton("Change");
    button1.setActionCommand("text");
    button1.addActionListener(new ActionHandler());
    buttonPanel.add(button1);

    npcBox = new JCheckBox();
    npcBox.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.DESELECTED)
                isNPCBoxChecked = false;
            else
                isNPCBoxChecked = true;
        }
    });
    fieldPanel.add(npcBox);
    labelPanel.add(new JLabel("NPC"));
    JButton button2 = new JButton("Change");
    button2.setActionCommand("npc");
    button2.addActionListener(new ActionHandler());
    buttonPanel.add(button2);

    childrenField = new JTextField("child1,child2", 10);
    fieldPanel.add(childrenField);
    labelPanel.add(new JLabel("Children"));
    JButton button3 = new JButton("");
    buttonPanel.add(button3);

    probSetField = new JTextField("0.1,0.9", 10);
    fieldPanel.add(probSetField);
    labelPanel.add(new JLabel("Probability set"));
    JButton button4 = new JButton("Change");
    button4.setActionCommand("probSet");
    button4.addActionListener(new ActionHandler());
    buttonPanel.add(button4);

    strategyField = new JTextField("0", 10);
    fieldPanel.add(strategyField);
    labelPanel.add(new JLabel("Strategy"));
    JButton button5 = new JButton("Change");
    button5.setActionCommand("strategy");
    button5.addActionListener(new ActionHandler());
    buttonPanel.add(button5);

    masterPanel.add(buttonPanel);
    masterPanel.add(fieldPanel);
    masterPanel.add(labelPanel);

    JFrame frame = new JFrame("Node Properties");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fieldPanel.setOpaque(true);
    frame.setContentPane(masterPanel);
    frame.pack();
    frame.setLocation(windowWidth, verticalWindowPlacement);
    frame.setVisible(true);

    verticalWindowPlacement += frame.getBounds().height;
}

From source file:MainClass.java

public MainClass() {

    JButton jb = new JButton(new MyIcon());
    jb.setActionCommand("France");
    jb.addActionListener(this);
    add(jb);/*from  w  w  w.  j  a v a  2 s .  c  om*/
}

From source file:Main.java

public Main() {

    JButton jb = new JButton(new MyIcon());
    jb.setActionCommand("France");
    jb.addActionListener(this);
    add(jb);/* w w  w  . j a va 2  s. co  m*/
}