Example usage for java.awt.event ActionEvent getSource

List of usage examples for java.awt.event ActionEvent getSource

Introduction

In this page you can find the example usage for java.awt.event ActionEvent getSource.

Prototype

public Object getSource() 

Source Link

Document

The object on which the Event initially occurred.

Usage

From source file:components.CrayonPanel.java

public void actionPerformed(ActionEvent e) {
    Color newColor = null;//from www.ja v a 2 s .  c o m
    String command = ((JToggleButton) e.getSource()).getActionCommand();
    if ("green".equals(command))
        newColor = Color.green;
    else if ("red".equals(command))
        newColor = Color.red;
    else if ("yellow".equals(command))
        newColor = Color.yellow;
    else if ("blue".equals(command))
        newColor = Color.blue;
    getColorSelectionModel().setSelectedColor(newColor);
}

From source file:SaveImage.java

public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox) e.getSource();
    if (cb.getActionCommand().equals("SetFilter")) {
        setOpIndex(cb.getSelectedIndex());
        repaint();/*from  w  w  w.j  a va 2s .com*/
    } else if (cb.getActionCommand().equals("Formats")) {
        /*
         * Save the filtered image in the selected format. The selected item will
         * be the name of the format to use
         */
        String format = (String) cb.getSelectedItem();
        /*
         * Use the format name to initialise the file suffix. Format names
         * typically correspond to suffixes
         */
        File saveFile = new File("savedimage." + format);
        JFileChooser chooser = new JFileChooser();
        chooser.setSelectedFile(saveFile);
        int rval = chooser.showSaveDialog(cb);
        if (rval == JFileChooser.APPROVE_OPTION) {
            saveFile = chooser.getSelectedFile();
            /*
             * Write the filtered image in the selected format, to the file chosen
             * by the user.
             */
            try {
                ImageIO.write(biFiltered, format, saveFile);
            } catch (IOException ex) {
            }
        }
    }
}

From source file:SimpleSounds.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == exitButton) {
        dispose();/*from w w  w  .j  a  v  a2  s  .  c o  m*/
        System.exit(0);
    } else if (e.getSource() == sound1Button) {
        if (sound1.getEnable())
            sound1.setEnable(false);
        else
            sound1.setEnable(true);
    } else if (e.getSource() == sound2Button) {
        sound2.setEnable(!sound2.getEnable());
    }
}

From source file:MenuDemo.java

public void actionPerformed(ActionEvent e) {
    JMenuItem source = (JMenuItem) (e.getSource());
    String s = "Action event detected." + newline + "    Event source: " + source.getText()
            + " (an instance of " + getClassName(source) + ")";
    output.append(s + newline);/*  w w  w.  j a va  2 s . c o m*/
    output.setCaretPosition(output.getDocument().getLength());
}

From source file:EditableComboBox.java

public EditableComboBox() {
    // Build a mapping from book titles to their entries
    for (int i = 0; i < books.length; i++) {
        bookMap.put(books[i].getTitle(), books[i]);
    }//from   w  w  w .ja  v  a  2  s . c o m

    setLayout(new BorderLayout());

    JComboBox bookCombo = new JComboBox(books);
    bookCombo.setEditable(true);
    bookCombo.setEditor(new ComboBoxEditorExample(bookMap, books[0]));
    bookCombo.setMaximumRowCount(4);
    bookCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("You chose " + ((JComboBox) e.getSource()).getSelectedItem() + "!");
        }
    });
    bookCombo.setActionCommand("Hello");
    add(bookCombo, BorderLayout.CENTER);
}

From source file:de.codesourcery.eve.skills.ui.utils.PersistentDialog.java

public PersistentDialog(String id, Icon icon, String title, String label, PersistenceType type, Kind kind) {
    super(null, title, ModalityType.APPLICATION_MODAL);

    if (kind == null) {
        throw new IllegalArgumentException("kind cannot be NULL");
    }/* www .  ja  va  2  s.  c  o m*/

    if (StringUtils.isBlank(id)) {
        throw new IllegalArgumentException("id cannot be blank.");
    }

    if (type == null) {
        throw new IllegalArgumentException("type cannot be NULL");
    }

    if (StringUtils.isBlank(label)) {
        throw new IllegalArgumentException("label cannot be blank.");
    }

    this.id = id;
    this.type = type;

    // configure checkbox
    isDialogEnabled = new JCheckBox("Always show this dialog ?", true);
    isDialogEnabled.setHorizontalTextPosition(SwingConstants.RIGHT);

    // add panel
    final JPanel content = new JPanel();
    content.setLayout(new GridBagLayout());

    content.add(new JLabel(icon),
            new ConstraintsBuilder().x(0).y(0).anchorWest().noResizing().useRelativeWidth().end());
    content.add(new JLabel(label),
            new ConstraintsBuilder().x(1).y(0).anchorWest().resizeBoth().useRemainingWidth().end());
    content.add(isDialogEnabled, new ConstraintsBuilder().x(0).y(1).width(2).anchorWest().noResizing().end());

    final JPanel buttonPanel = new JPanel();
    buttonPanel.add(okButton);

    final ActionListener listener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == cancelButton) {
                wasCancelled = true;
            }
            dispose();
        }
    };

    okButton.addActionListener(listener);

    if (kind == Kind.CANCEL) {
        buttonPanel.add(cancelButton);
        cancelButton.addActionListener(listener);
    }

    content.add(buttonPanel, new ConstraintsBuilder().x(0).y(2).useRemainingSpace().end());

    getContentPane().add(content);

    pack();

    setLocationRelativeTo(null);

    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            wasCancelled = true;
            dispose();
        }
    });
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand() != null) {
        String command = e.getActionCommand();
        if (command != null) {
            command = command.toUpperCase();
        }/*from w w  w.  ja va2 s  .c o  m*/
        e = new ActionEvent(e.getSource(), e.getID(), command, e.getModifiers());
    }

    if (defAction != null) {
        defAction.actionPerformed(e);
    }
}

From source file:Transfer.java

public Transfer() {

    // Establish the GUI
    Container cp = new Box(BoxLayout.X_AXIS);
    setContentPane(cp);/*from w ww  . jav a  2  s . c om*/
    JPanel firstPanel = new JPanel();
    propertyComboBox = new JComboBox();
    propertyComboBox.addItem("text");
    propertyComboBox.addItem("font");
    propertyComboBox.addItem("background");
    propertyComboBox.addItem("foreground");
    firstPanel.add(propertyComboBox);
    cp.add(firstPanel);
    cp.add(Box.createGlue());

    tf = new JTextField("Hello");
    tf.setForeground(Color.RED);
    tf.setDragEnabled(true);
    cp.add(tf);

    cp.add(Box.createGlue());

    l = new JLabel("Hello");
    l.setBackground(Color.YELLOW);
    cp.add(l);

    cp.add(Box.createGlue());

    JSlider stryder = new JSlider(SwingConstants.VERTICAL);
    stryder.setMinimum(10);
    stryder.setValue(14);
    stryder.setMaximum(72);
    stryder.setMajorTickSpacing(10);
    stryder.setPaintTicks(true);

    cp.add(stryder);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 300);

    // Add Listeners and Converters
    setMyTransferHandlers((String) propertyComboBox.getSelectedItem());

    // Mousing in the Label starts a Drag.
    MouseListener myDragListener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
    };
    l.addMouseListener(myDragListener);

    // Selecting in the ComboBox makes that the property that is xfered.
    propertyComboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ce) {
            JComboBox bx = (JComboBox) ce.getSource();
            String prop = (String) bx.getSelectedItem();
            setMyTransferHandlers(prop);
        }
    });

    // Typing a word and pressing enter in the TextField tries
    // to set that as the font name.
    tf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            JTextField jtf = (JTextField) evt.getSource();
            String fontName = jtf.getText();
            Font font = new Font(fontName, Font.BOLD, 18);
            tf.setFont(font);
        }
    });

    // Setting the Slider sets that font into the textfield.
    stryder.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            JSlider sl = (JSlider) evt.getSource();
            Font oldf = tf.getFont();
            Font newf = oldf.deriveFont((float) sl.getValue());
            tf.setFont(newf);
        }
    });

}

From source file:de.dfki.owlsmx.gui.ShowResultVisualization.java

private void saveActionPerformed(java.awt.event.ActionEvent event) {//GEN-FIRST:event_saveActionPerformed
    try {/*w ww. j  av a  2  s  . c om*/
        if (event.getSource().equals(save)) {
            if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                if (fc.getFileFilter().getClass().equals((new PNGFilter()).getClass()))
                    if (fc.getSelectedFile().getAbsolutePath().toLowerCase().endsWith(".png"))
                        ChartUtilities.saveChartAsPNG(fc.getSelectedFile(), chart,
                                ResultVisualization.graphPrintWidth, ResultVisualization.graphPrintHeight);
                    else
                        ChartUtilities.saveChartAsPNG(new File(fc.getSelectedFile().getAbsolutePath() + ".png"),
                                chart, ResultVisualization.graphPrintWidth,
                                ResultVisualization.graphPrintHeight);

                else if (fc.getFileFilter().getClass().equals((new JPGFilter()).getClass()))
                    if (fc.getSelectedFile().getAbsolutePath().toLowerCase().endsWith(".png"))
                        ChartUtilities.saveChartAsJPEG(fc.getSelectedFile(), chart,
                                ResultVisualization.graphPrintWidth, ResultVisualization.graphPrintHeight);
                    else
                        ChartUtilities.saveChartAsJPEG(
                                new File(fc.getSelectedFile().getAbsolutePath() + ".png"), chart,
                                ResultVisualization.graphPrintWidth, ResultVisualization.graphPrintHeight);

                else if (fc.getFileFilter().getClass().equals((new PDFFilter()).getClass()))
                    if (fc.getSelectedFile().getAbsolutePath().toLowerCase().endsWith(".pdf"))
                        Converter.convertToPdf(chart, ResultVisualization.graphPrintWidth,
                                ResultVisualization.graphPrintHeight, fc.getSelectedFile().getAbsolutePath());
                    else
                        Converter.convertToPdf(chart, ResultVisualization.graphPrintWidth,
                                ResultVisualization.graphPrintHeight,
                                fc.getSelectedFile().getAbsolutePath() + ".pdf");
                else if (fc.getFileFilter().getClass().equals((new EPSFilter()).getClass()))
                    printGraphics2DtoEPS(fc.getSelectedFile().getAbsolutePath());
            }
        }
    } catch (Exception e) {
        GUIState.displayWarning(e.getClass().toString(),
                "Couldn't save file " + fc.getSelectedFile().getAbsolutePath());
        e.printStackTrace();
    }
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();
    if (o == num1) {
        textField.setText(s.concat("1"));
        s = textField.getText();/*from   w  w  w.j  a v a2  s  . c  o  m*/
    } else if (o == num2) {
        textField.setText(s.concat("2"));
        s = textField.getText();
    } else if (o == num3) {
        textField.setText(s.concat("3"));
        s = textField.getText();
    } else if (o == num4) {
        textField.setText(s.concat("4"));
        s = textField.getText();
    } else if (o == num5) {
        textField.setText(s.concat("5"));
        s = textField.getText();
    } else if (o == num6) {
        textField.setText(s.concat("6"));
        s = textField.getText();
    } else if (o == num7) {
        textField.setText(s.concat("7"));
        s = textField.getText();
    } else if (o == num8) {
        textField.setText(s.concat("8"));
        s = textField.getText();
    } else if (o == num9) {
        textField.setText(s.concat("9"));
        s = textField.getText();
    } else if (o == num0) {
        textField.setText(s.concat("0"));
        s = textField.getText();
    } else if (o == add) {

        textField.setText("");
        first = Double.parseDouble(s);
        System.out.println(first);
        s = "";
        ope = "+";
    } else if (o == sub) {
        textField.setText("");
        first = Double.parseDouble(s);
        s = "";
        ope = "-";
    } else if (o == mult) {
        textField.setText("");
        first = Double.parseDouble(s);
        s = "";
        ope = "*";
    } else if (o == div) {
        textField.setText("");
        first = Double.parseDouble(s);
        s = "";
        ope = "/";
    } else if (o == equalto) {
        if (flag == 0) {
            second = Double.parseDouble(s);
            total(first, second, ope);
            flag = 1;
        } else if (flag == 1) {
            second = Double.parseDouble(s);
            total(first, second, ope);
        }
        System.out.println(first);
    } else if (o == exit) {
        System.exit(0);
    } else if (o == point) {
        textField.setText(s.concat("."));
        s = textField.getText();
    }
    if (o == reset) {
        textField.setText("");
        s = textField.getText();
        total1 = 0;
    }
}