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:Main.java

public Main() throws HeadlessException {
    setSize(200, 200);// w  w w  .  j  a va2s  . c o m
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button1 = new JButton("Message dialog");
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog((Component) e.getSource(), "Thank you!");
        }
    });

    JButton button2 = new JButton("Input dialog");
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = JOptionPane.showInputDialog((Component) e.getSource(), "What is your name?");
            if (text != null && !text.equals("")) {
                JOptionPane.showMessageDialog((Component) e.getSource(), "Hello " + text);
            }
        }
    });

    JButton button3 = new JButton("Yes no dialog");
    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int result = JOptionPane.showConfirmDialog((Component) e.getSource(), "Close this application?");
            if (result == JOptionPane.YES_OPTION) {
                System.exit(0);
            } else if (result == JOptionPane.NO_OPTION) {
                System.out.println("Do nothing");
            }
        }
    });

    setLayout(new FlowLayout(FlowLayout.CENTER));
    getContentPane().add(button1);
    getContentPane().add(button2);
    getContentPane().add(button3);
}

From source file:TextEditor.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == openButton) {
        textArea.setText(FileHandler.open());
    } else if (e.getSource() == saveButton) {
        FileHandler.save(textArea.getText());
    } else if (e.getSource() == saveAsButton) {
        FileHandler.saveAs(textArea.getText());
    }//from   w  ww  .ja  va 2  s  .  co m

}

From source file:Main.java

protected JMenuBar createMenuBar() {
    final JMenuBar menuBar = new JMenuBar();

    JMenu mFont = new JMenu("Font");
    mFont.setMnemonic('o');

    ButtonGroup group = new ButtonGroup();
    fontMenus = new JMenuItem[FontName.length];
    for (int k = 0; k < FontName.length; k++) {
        int m = k + 1;
        fontMenus[k] = new JRadioButtonMenuItem(m + " " + FontName[k]);
        boolean selected = (k == 0);
        fontMenus[k].setSelected(selected);
        fontMenus[k].setMnemonic('1' + k);
        fontMenus[k].setFont(fonts[k]);//from w ww  .j a v a 2  s.c  o  m
        fontMenus[k].addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                System.out.println(((JComponent) arg0.getSource()).getFont());

            }

        });
        group.add(fontMenus[k]);
        mFont.add(fontMenus[k]);
    }

    menuBar.add(mFont);

    return menuBar;
}

From source file:uk.co.petertribble.jkstat.demo.JLoadChart.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == exitItem) {
        System.exit(0);/*from w  w w.  ja  va 2s  .c om*/
    } else {
        updateAccessory();
    }
}

From source file:Main.java

void addTab() {
    JEditorPane ep = new JEditorPane();
    ep.setEditable(false);//from   ww w  . j a v  a2  s .  c o  m
    tp.addTab(null, new JScrollPane(ep));

    JButton tabCloseButton = new JButton("Close");
    tabCloseButton.setActionCommand("" + tabCounter);

    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JButton btn = (JButton) ae.getSource();
            String s1 = btn.getActionCommand();
            for (int i = 1; i < tp.getTabCount(); i++) {
                JPanel pnl = (JPanel) tp.getTabComponentAt(i);
                btn = (JButton) pnl.getComponent(0);
                String s2 = btn.getActionCommand();
                if (s1.equals(s2)) {
                    tp.removeTabAt(i);
                    break;
                }
            }
        }
    };
    tabCloseButton.addActionListener(al);

    if (tabCounter != 0) {
        JPanel pnl = new JPanel();
        pnl.setOpaque(false);
        pnl.add(tabCloseButton);
        tp.setTabComponentAt(tp.getTabCount() - 1, pnl);
        tp.setSelectedIndex(tp.getTabCount() - 1);
    }

    tabCounter++;
}

From source file:SaveYourDrawingToFile.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == clearBtn) {
        displayList = new Vector();
        repaint();//from  www  .jav  a2 s . co  m
    } else if (e.getSource() == saveBtn) {
        try {
            FileOutputStream fos = new FileOutputStream(pathname);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(displayList);
            oos.flush();
            oos.close();
            fos.close();
        } catch (Exception ex) {
            System.out.println("Trouble writing display list vector");
        }
    } else if (e.getSource() == restoreBtn) {
        try {
            FileInputStream fis = new FileInputStream(pathname);
            ObjectInputStream ois = new ObjectInputStream(fis);
            displayList = (Vector) (ois.readObject());
            ois.close();
            fis.close();
            repaint();
        } catch (Exception ex) {
            System.out.println("Trouble reading display list vector");
        }
    } else if (e.getSource() == quitBtn) {
        setVisible(false);
        dispose();
        System.exit(0);
    }
}

From source file:SelectionHandler.java

public void actionPerformed(ActionEvent a) {
    JComboBox cb = (JComboBox) a.getSource();
    String selected = (String) cb.getSelectedItem();
    String current = direction.getText();
    if (!selected.equals(current)) {
        direction.setText(selected);//  www  . j a  va2s. c o m
        JList temp = source;
        source = destination;
        destination = temp;
        source.clearSelection();
        destination.clearSelection();
    }

}

From source file:org.encog.workbench.dialogs.common.ChartField.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.refreshButton) {
        if (listener != null) {
            listener.refresh(this);
        }/*from  w w  w .  j a  v a2  s  .c o  m*/
    }

}

From source file:ComboBoxDemo.java

/** Listens to the combo box. */
public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox) e.getSource();
    String petName = (String) cb.getSelectedItem();
    updateLabel(petName);//w  w w . java 2  s.  co  m
}

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

public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(save))
        saveActionPerformed(e);
}