Example usage for javax.swing JButton addActionListener

List of usage examples for javax.swing JButton addActionListener

Introduction

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

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the button.

Usage

From source file:de.ipk_gatersleben.ag_nw.graffiti.services.GUIhelper.java

public static JComponent getWebsiteButton(String title, final String url, final String opt_local_folder,
        final String optIntroText, final String optIntroDialogTitle) {
    JButton res = new JButton(title);
    res.setToolTipText("<html>Click button to open URL:<br><code><b>" + url);
    res.addActionListener(getDialogAction(url, opt_local_folder, optIntroText, optIntroDialogTitle));
    return res;//from  w  ww.  j a v a2s .  c om
}

From source file:com.diversityarrays.kdxplore.vistool.VisToolbarFactory.java

static public VisToolToolBar create(final String title, final JComponent comp, final Closure<File> snapshotter,
        final VisToolDataProvider visToolDataProvider, boolean floatable, final String[] imageSuffixes) {
    Window window = GuiUtil.getOwnerWindow(comp);

    boolean anyButtons = false;

    final JCheckBox keepOnTop;

    if (window == null) {
        keepOnTop = null;/*from   w  w  w.  j a  v  a2s .c om*/
    } else {
        anyButtons = true;
        keepOnTop = new JCheckBox(Msg.OPTION_KEEP_ON_TOP(), true);

        keepOnTop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                window.setAlwaysOnTop(keepOnTop.isSelected());
            }
        });
        window.setAlwaysOnTop(keepOnTop.isSelected());

        //         buttons.add(keepOnTop);

        final PropertyChangeListener alwaysOnTopListener = new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                keepOnTop.setSelected(window.isAlwaysOnTop());
            }
        };
        window.addPropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);

        window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                window.removeWindowListener(this);
                window.removePropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);
            }
        });
    }

    final JButton cameraButton;
    if (snapshotter == null) {
        cameraButton = null;
    } else {
        Action cameraAction = new AbstractAction(Msg.ACTION_SNAPSHOT()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (chooser == null) {
                    chooser = new JFileChooser();
                    chooser.setFileFilter(new FileFilter() {
                        @Override
                        public boolean accept(File f) {
                            if (!f.isFile()) {
                                return true;
                            }
                            String loname = f.getName().toLowerCase();
                            for (String sfx : imageSuffixes) {
                                if (loname.endsWith(sfx)) {
                                    return true;
                                }
                            }
                            return false;
                        }

                        @Override
                        public String getDescription() {
                            return Msg.DESC_IMAGE_FILE();
                        }
                    });
                    chooser.setMultiSelectionEnabled(false);
                    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                }

                if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(comp)) {
                    File file = chooser.getSelectedFile();
                    snapshotter.execute(file);
                }
            }
        };

        ImageIcon icon = loadIcon("camera-24.png"); //$NON-NLS-1$
        if (icon != null) {
            cameraAction.putValue(Action.SMALL_ICON, icon);
            cameraAction.putValue(Action.NAME, null);
        }

        anyButtons = true;
        cameraButton = new JButton(cameraAction);
    }

    final JButton refreshButton;
    if (visToolDataProvider == null) {
        refreshButton = null;
    } else {
        anyButtons = true;

        refreshButton = new JButton(Msg.ACTION_REFRESH());

        ImageIcon icon = loadIcon("refresh-24.png"); //$NON-NLS-1$
        if (icon != null) {
            refreshButton.setIcon(icon);
            // don't remove the name
        }

        refreshButton.setForeground(Color.RED);
        refreshButton.setEnabled(false);

        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (visToolDataProvider.refreshData()) {
                    refreshButton.setEnabled(false);
                }
            }
        });

        visToolDataProvider.addVisToolDataChangedListener(new VisToolDataChangedListener() {
            @Override
            public void visToolDataChanged(Object source) {
                refreshButton.setEnabled(true);
            }
        });
    }

    VisToolToolBar toolBar = null;

    if (anyButtons) {
        toolBar = new VisToolToolBar(keepOnTop, cameraButton, refreshButton);
        toolBar.setFloatable(floatable);
    }
    return toolBar;

}

From source file:Main.java

License:asdf

public void capturingVariable() {
    String name = getUserName();//from  w w w  .j a v a 2 s.  c om
    JButton button = new JButton();
    button.addActionListener(event -> System.out.println("hi " + name));
}

From source file:de.ipk_gatersleben.ag_nw.graffiti.services.GUIhelper.java

public static JComponent getWebsiteDownloadButton(final String title, final String optUrlManualDownloadWebsite,
        final String target_dir_null_ask_user, final String optIntroText, final String[] downloadURLs,
        final String optIntroDialogTitle, final FileDownloadStatusInformationProvider statusProvider,
        final Runnable optFinishSwingTask) {

    final JButton res = new JMButton("Download/Update");
    res.setToolTipText(//from  w w w.  ja v a2s  .  c o m
            "<html>Click button to start automatic download<br><code><b>Check License/Disclaimers first!");
    res.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final String opt_local_folder;
            if (target_dir_null_ask_user == null) {
                File file = OpenFileDialogService.getDirectoryFromUser("Select folder");
                if (file == null)
                    return;
                else
                    opt_local_folder = file.getAbsolutePath();
            } else {
                opt_local_folder = target_dir_null_ask_user;
            }

            res.setEnabled(false);
            res.setText("Downloading");
            final BackgroundTaskStatusProviderSupportingExternalCallImpl status = new BackgroundTaskStatusProviderSupportingExternalCallImpl(
                    "Please wait...", "Downloading files...");
            BackgroundTaskHelper.issueSimpleTask(title, "Please wait...", new Runnable() {
                public void run() {
                    boolean allOK = true;
                    for (String downloadURL : downloadURLs) {
                        allOK = performDownload(downloadURL, opt_local_folder, status);
                        if (status.wantsToStop()) {
                            break;
                        }
                    }
                    if (status.wantsToStop()) {
                        allOK = true;
                        try {
                            status.setCurrentStatusText1("Cancel...");
                            for (String downloadURL : downloadURLs) {
                                String fileName = downloadURL.substring(downloadURL.lastIndexOf("/") + 1);
                                if (downloadURL.contains("|"))
                                    fileName = downloadURL.substring(downloadURL.lastIndexOf("|") + 1);
                                String targetFileName = ReleaseInfo.getAppFolderWithFinalSep() + fileName;
                                if (new File(targetFileName).exists()) {
                                    new File(targetFileName).delete();
                                    status.setCurrentStatusText2("Delete " + targetFileName);
                                }
                            }
                        } catch (Exception e) {
                            //
                        }
                    }

                    if (!allOK) {
                        res.setEnabled(true);
                        res.setText(
                                "<html><small>Automatic download failure<br>Click here for manual download");
                        res.removeActionListener(res.getActionListeners()[0]);
                        res.addActionListener(getDialogAction(optUrlManualDownloadWebsite, opt_local_folder,
                                optIntroText, optIntroDialogTitle));
                        res.requestFocus();
                        final JDialog jd = (JDialog) ErrorMsg.findParentComponent(res, JDialog.class);
                        if (jd != null) {
                            SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                    jd.pack();
                                }
                            });
                        }
                    } else {
                        if (status.wantsToStop()) {
                            res.setText("Canceled");
                            status.pleaseContinueRun();
                            status.setCurrentStatusText2("Canceled!");
                        } else {
                            res.setText("Downloaded");
                        }
                        res.setEnabled(false);
                        if (statusProvider != null)
                            statusProvider.finishedNewDownload();
                        if (optFinishSwingTask != null)
                            SwingUtilities.invokeLater(optFinishSwingTask);
                    }
                }
            }, null, status);
        }
    });
    return res;
}

From source file:Main.java

public Main() {
    JButton btn1 = new JButton("Button1");
    btn1.addActionListener(new ButtonListener());
    add(btn1);/*from w w w.  ja v a 2  s.co m*/
}

From source file:Main.java

public Main() throws HeadlessException {
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    JButton button = new JButton("Close");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);/*  w w w.j a  v a 2s  .  co m*/
        }
    });

    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.setSize(new Dimension(100, 100));
    this.getContentPane().add(button);
}

From source file:ButtonListener.java

public Main() {

    JButton btn1 = new JButton("Button1");
    btn1.addActionListener(new ButtonListener());
    add(btn1);//www  . j a  v a 2s.co  m

}

From source file:MainClass.java

public MainClass() {

    JButton btn1 = new JButton("Button1");
    btn1.addActionListener(new ButtonListener());
    add(btn1);/*from w w w.j ava  2 s.  com*/

}

From source file:MainClass.java

    public SwingTest() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JButton button = new JButton("Press me");
  button.addActionListener(new ButtonListener());
  this.getContentPane().setLayout(new BorderLayout());
  this.getContentPane().add(button, BorderLayout.SOUTH);
  this.getContentPane().add(field, BorderLayout.NORTH);
  this.pack();//  ww  w .java  2  s .  c  o  m
  this.setVisible(true);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel all = new JPanel();
    JButton button = new JButton("Expand");
    button.addActionListener(this);

    all.add(button);//from   www .j  a v a 2  s . c  om
    all.add(f);
    getContentPane().add(all);
}