Example usage for java.awt.event ActionListener ActionListener

List of usage examples for java.awt.event ActionListener ActionListener

Introduction

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

Prototype

ActionListener

Source Link

Usage

From source file:JProgressBarDemo.java

public JProgressBarDemo() {
    super("JProgressBar Demo");
    setSize(300, 100);/* w ww.j a  v  a2s .  c om*/

    UIManager.put("ProgressBar.selectionBackground", Color.black);
    UIManager.put("ProgressBar.selectionForeground", Color.white);
    UIManager.put("ProgressBar.foreground", new Color(8, 32, 128));

    progressBar = new JProgressBar();
    progressBar.setMinimum(minValue);
    progressBar.setMaximum(maxValue);
    progressBar.setStringPainted(true);

    JButton start = new JButton("Start");
    start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Thread runner = new Thread() {
                public void run() {
                    counter = minValue;
                    while (counter <= maxValue) {
                        Runnable runme = new Runnable() {
                            public void run() {
                                progressBar.setValue(counter);
                            }
                        };
                        SwingUtilities.invokeLater(runme);
                        counter++;
                        try {
                            Thread.sleep(100);
                        } catch (Exception ex) {
                        }
                    }
                }
            };
            runner.start();
        }
    });

    getContentPane().add(progressBar, BorderLayout.CENTER);
    getContentPane().add(start, BorderLayout.WEST);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:MyDialog.java

public void init() {
    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dlg.show();/*from ww w .  j  a v  a2 s  .  c  o  m*/
        }
    });
    getContentPane().add(b1);
}

From source file:Main.java

public TestPane() {
    add(pbProgress);//from  w ww. j av a 2s .c o  m
    start = new JButton("Start");
    add(start);
    start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            start.setEnabled(false);
            ProgressWorker pw = new ProgressWorker();
            pw.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    String name = evt.getPropertyName();
                    if (name.equals("progress")) {
                        int progress = (int) evt.getNewValue();
                        pbProgress.setValue(progress);
                        repaint();
                    } else if (name.equals("state")) {
                        SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
                        switch (state) {
                        case DONE:
                            start.setEnabled(true);
                            break;
                        }
                    }
                }
            });
            pw.execute();
        }
    });
}

From source file:Main.java

private void initComponents() {
    JFrame frame = new JFrame("JFormattedTextField Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    MaskFormatter mask = null;//  w  ww  . j a va  2s  .c o  m
    try {
        mask = new MaskFormatter("##h##min##s");// the # is for numeric values
        mask.setPlaceholderCharacter('#');
    } catch (ParseException e) {
        e.printStackTrace();
    }
    final JFormattedTextField timeField = new JFormattedTextField(mask);

    // ActionListener for when enter is pressed
    timeField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            Object source = ae.getSource();
            if (source == timeField) {
                // parse to a valid time here
                System.out.println(timeField.getText());
            }
        }
    });
    frame.add(timeField);
    frame.pack();
    frame.setVisible(true);
}

From source file:Test.java

public ApplicationWindow() {
    this.setBounds(100, 100, 200, 200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setType(Type.NORMAL);

    JButton exitButton = new JButton("Exit");
    this.add(exitButton);
    exitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);/*from ww w  .  ja  v a2 s. com*/
        }
    });
}

From source file:SimpleFileChooser.java

public SimpleFileChooser() {
    super("File Chooser Test Frame");
    setSize(350, 200);/*from w w  w .  jav  a2  s  . c om*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JButton openButton = new JButton("Open");
    JButton saveButton = new JButton("Save");
    JButton dirButton = new JButton("Pick Dir");
    final JLabel statusbar = new JLabel("Output of your selection will go here");

    // Create a file chooser that opens up as an Open dialog
    openButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(true);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                File[] sf = chooser.getSelectedFiles();
                String filelist = "nothing";
                if (sf.length > 0)
                    filelist = sf[0].getName();
                for (int i = 1; i < sf.length; i++) {
                    filelist += ", " + sf[i].getName();
                }
                statusbar.setText("You chose " + filelist);
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that opens up as a Save dialog
    saveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You saved "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that allows you to pick a directory
    // rather than a file
    dirButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You opened "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    c.add(openButton);
    c.add(saveButton);
    c.add(dirButton);
    c.add(statusbar);
}

From source file:Test.java

public ApplicationWindow() {
    this.setSize(200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());
    JButton exitButton = new JButton("Exit");
    this.add(exitButton);

    int totalButtons = MouseInfo.getNumberOfButtons();
    System.out.println(Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled());
    System.out.println("You have " + totalButtons + " total buttons");

    this.addMouseListener(this);
    this.addMouseWheelListener(this);

    exitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);// w  ww.  java  2s. co m
        }
    });

}

From source file:Faces.java

public void init() {
    faces = new Icon[] { new ImageIcon(getClass().getResource("Face0.gif")),
            new ImageIcon(getClass().getResource("Face1.gif")),
            new ImageIcon(getClass().getResource("Face2.gif")),
            new ImageIcon(getClass().getResource("Face3.gif")),
            new ImageIcon(getClass().getResource("Face4.gif")), };
    jb = new JButton("JButton", faces[3]);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (mad) {
                jb.setIcon(faces[3]);/*ww w  .  j ava  2 s . c  o m*/
                mad = false;
            } else {
                jb.setIcon(faces[0]);
                mad = true;
            }
            jb.setVerticalAlignment(JButton.TOP);
            jb.setHorizontalAlignment(JButton.LEFT);
        }
    });
    jb.setRolloverEnabled(true);
    jb.setRolloverIcon(faces[1]);
    jb.setPressedIcon(faces[2]);
    jb.setDisabledIcon(faces[4]);
    jb.setToolTipText("Yow!");
    cp.add(jb);
    jb2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (jb.isEnabled()) {
                jb.setEnabled(false);
                jb2.setText("Enable");
            } else {
                jb.setEnabled(true);
                jb2.setText("Disable");
            }
        }
    });
    cp.add(jb2);
}

From source file:EditorPaneExample1.java

public EditorPaneExample1() {
    super("JEditorPane Example 1");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout(4, 4));
    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, "West");
    textField = new JTextField(32);
    panel.add(textField, "Center");

    getContentPane().add(panel, "South");

    // Change page based on text field
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            String url = textField.getText();
            try {
                // Try to display the page
                pane.setPage(url);/*from   w  w  w .j  a  v a 2 s .  co  m*/
            } catch (IOException e) {
                JOptionPane.showMessageDialog(pane, new String[] { "Unable to open file", url },
                        "File Open Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
}

From source file:Main.java

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

    setLayout(new BorderLayout());

    JComboBox bookCombo = new JComboBox(items);
    bookCombo.setEditable(true);
    bookCombo.setEditor(new MyComboBoxEditor(itemMap, items[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);
}