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 void actionPerformed(ActionEvent evt) {
    JComboBox cb = (JComboBox) evt.getSource();
    Object newItem = cb.getSelectedItem();

    boolean same = newItem.equals(oldItem);
    oldItem = newItem;/*from   w w  w  .  j a v  a 2  s  .co m*/

    if ("comboBoxEdited".equals(evt.getActionCommand())) {
        // User has typed in a string; only possible with an editable combobox
    } else if ("comboBoxChanged".equals(evt.getActionCommand())) {
        // User has selected an item; it may be the same item
    }
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == b) {
        if (i == 0) {
            JOptionPane.showMessageDialog(null, "This is First Image");
        } else {//from   www. j av  a  2s  . c o  m
            i = i - 1;
            l.setIcon(m[i]);
        }
    }
    if (e.getSource() == b1) {
        if (i == m.length - 1) {
            JOptionPane.showMessageDialog(null, "This is LastImage");
        } else {
            i = i + 1;
            l.setIcon(m[i]);
        }
    }
}

From source file:Main.java

public Main() {
    this.setSize(400, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Change Frame Color");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = (Component) e.getSource();
            JFrame frame = (JFrame) SwingUtilities.getRoot(component);
            frame.setBackground(Color.RED);
        }//ww  w.  j  a va  2  s  . com
    });
    this.getContentPane().add(button);
}

From source file:Main.java

public Main() {
    this.setSize(400, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Change Frame Color");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = (Component) e.getSource();
            JFrame frame = (JFrame) SwingUtilities.getRoot(component);
            frame.getContentPane().setBackground(Color.RED);
        }//from www  . j  a  va2 s  .  com
    });
    this.getContentPane().add(button);
}

From source file:Main.java

public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    Color color = getBackground();
    if (source == yellowButton)
        color = Color.yellow;/*from  w ww. jav  a2  s  .co  m*/
    else if (source == blueButton)
        color = Color.blue;
    else if (source == redButton)
        color = Color.red;
    setBackground(color);
    repaint();
}

From source file:Main.java

public Main() {
    String[] comboTypes = { "Numbers", "Alphabets", "Symbols" };
    JComboBox<String> comboTypesList = new JComboBox<>(comboTypes);
    comboTypesList.setSelectedIndex(2);/*from  ww  w  . ja va 2  s .c  om*/
    comboTypesList.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JComboBox jcmbType = (JComboBox) e.getSource();
            String cmbType = (String) jcmbType.getSelectedItem();
            System.out.println(cmbType);
        }
    });
    setLayout(new BorderLayout());
    add(comboTypesList, BorderLayout.NORTH);
}

From source file:FontComboBox.java

public void actionPerformed(ActionEvent evt) {
    JComboBox source = (JComboBox) evt.getSource();
    String item = (String) source.getSelectedItem();
    fontLabel.setFont(new Font(item, Font.PLAIN, 12));
}

From source file:InsertAction.java

public void actionPerformed(ActionEvent evt) {
    JTextComponent c = (JTextComponent) evt.getSource();

    try {/*from   w ww .  j a va2  s.co  m*/
        c.getDocument().insertString(c.getCaretPosition(), " space", null);
    } catch (BadLocationException e) {
    }
}

From source file:Main.java

public Main() {
    this.setLayout(new GridLayout(0, 1));
    this.add(label);
    this.add(new JButton(new AbstractAction("OK") {

        @Override//from  w w  w  .  j a  va 2s .  c o m
        public void actionPerformed(ActionEvent e) {
            JButton b = (JButton) e.getSource();
            b.setText("123");
        }
    }));
    new Timer(100, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setText(String.valueOf(System.nanoTime()));
        }
    }).start();
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    int i = 0;/* w w w. j  ava2  s . co m*/
    if (e.getSource() == b1) {
        pb.setVisible(true);
        try {
            while (i <= 100) {
                Thread.sleep(50);
                pb.paintImmediately(0, 0, 200, 25);
                pb.setValue(i);
                i++;
            }
        } catch (Exception e1) {
            System.out.print("Caughted exception is =" + e1);
        }
    }
}