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:org.jfree.chart.demo.CyclicXYPlotDemo.java

/**
 * A demonstration application showing an XY plot, with a cyclic axis and renderer
 *
 * @param title  the frame title.//from w w  w .ja va2s .  c o m
 */
public CyclicXYPlotDemo(final String title) {

    super(title);

    this.series = new XYSeries("Random Data");
    this.series.setMaximumItemCount(50); // Only 50 items are visible at the same time. 
                                         // Keep more as a mean to test this.
    final XYSeriesCollection data = new XYSeriesCollection(this.series);

    final JFreeChart chart = ChartFactory.createXYLineChart("Cyclic XY Plot Demo", "X", "Y", data,
            PlotOrientation.VERTICAL, true, true, false);

    final XYPlot plot = chart.getXYPlot();
    plot.setDomainAxis(new CyclicNumberAxis(10, 0));
    plot.setRenderer(new CyclicXYItemRenderer());

    final NumberAxis axis = (NumberAxis) plot.getRangeAxis();
    axis.setAutoRangeIncludesZero(false);
    axis.setAutoRangeMinimumSize(1.0);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(400, 300));
    final JPanel content = new JPanel(new BorderLayout());
    content.add(chartPanel, BorderLayout.CENTER);

    final JButton button1 = new JButton("Start");
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            timer.start();
        }
    });

    final JButton button2 = new JButton("Stop");
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            timer.stop();
        }
    });

    final JButton button3 = new JButton("Step by step");
    button3.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            CyclicXYPlotDemo.this.actionPerformed(null);
        }
    });

    final JPanel buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(button1);
    buttonPanel.add(button2);
    buttonPanel.add(button3);

    content.add(buttonPanel, BorderLayout.SOUTH);
    setContentPane(content);

    this.timer = new Timer(200, this);
}

From source file:nl.umcg.qube.ui.StatisticsPanel.java

public StatisticsPanel(CAGPanel cag_panel) {
    super(new BorderLayout());
    cagPanel = cag_panel;/*from  w w w.  j a v a  2  s  .  c o  m*/
    cagPanel.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent arg0) {
            // redrawing the chart during sequence viewing is very slow
            // don't update it therefore.
            if (chart != null && !cagPanel.sequenceRunning()) {
                chart.getXYPlot().clearDomainMarkers();
                chart.getXYPlot().addDomainMarker(new ValueMarker(cagPanel.getCurrentFrameNo() + 1));
            }
        }
    });

    JPanel commandPanel = new JPanel();

    JButton calc = new JButton("Recalculate");
    calc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            recalc();
        }
    });
    commandPanel.add(calc);

    blushLabel = new JLabel("");
    commandPanel.add(blushLabel);

    add(commandPanel, BorderLayout.NORTH);

    dataSet = new DefaultXYDataset();
    chart = ChartFactory.createXYLineChart("Blush value", "Frame no.", "arbitrary units", dataSet,
            PlotOrientation.VERTICAL, true, false, false);
    chartPanel = new ChartPanel(chart);
    add(chartPanel);
}

From source file:com.idealista.solrmeter.view.statistic.PieChartPanel.java

private Component createCustomizePanel() {
    JButton jButtonCustomize = new JButton(I18n.get("statistic.pieChartPanel.customize"));
    jButtonCustomize.addActionListener(new ActionListener() {

        @Override/*from   w  ww. j a v  a  2 s  .c  o  m*/
        public void actionPerformed(ActionEvent e) {
            JDialogCustomizePieChart dialog = new JDialogCustomizePieChart(SolrMeterMain.mainFrame,
                    timeRangeStatistic);
            dialog.setVisible(true);
        }

    });

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
    panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 10));
    panel.add(Box.createHorizontalGlue());
    panel.add(jButtonCustomize);

    return panel;
}

From source file:com.jdom.util.patterns.mvp.swing.RadioButtonGroupDialog.java

private RadioButtonGroupDialog(Frame frame, Component locationComp, String labelText, String title,
        Collection<String> data, String initialValue, String longValue) {
    super(frame, title, true);

    final JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(this);

    final JButton setButton = new JButton("OK");
    setButton.setActionCommand("OK");
    setButton.addActionListener(this);
    getRootPane().setDefaultButton(setButton);

    ButtonGroup radioButtonGroup = new ButtonGroup();
    for (String option : data) {
        JRadioButton button = new JRadioButton(option);
        if (option.equals(initialValue)) {
            button.setSelected(true);/*  w  w  w.  j a v a 2  s .c o m*/
        }
        radioButtonGroup.add(button);
        panel.add(button);
    }

    JScrollPane listScroller = new JScrollPane(panel);
    listScroller.setPreferredSize(new Dimension(250, 80));
    listScroller.setAlignmentX(LEFT_ALIGNMENT);

    JPanel listPane = new JPanel();
    listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));

    if (!StringUtils.isEmpty(labelText)) {
        JLabel label = new JLabel(labelText);
        label.setText(labelText);
        listPane.add(label);
    }
    listPane.add(Box.createRigidArea(new Dimension(0, 5)));
    listPane.add(listScroller);
    listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
    buttonPane.add(Box.createHorizontalGlue());
    buttonPane.add(cancelButton);
    buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
    buttonPane.add(setButton);

    Container contentPane = getContentPane();
    contentPane.add(listPane, BorderLayout.CENTER);
    contentPane.add(buttonPane, BorderLayout.PAGE_END);

    pack();
    setLocationRelativeTo(locationComp);
}

From source file:net.sf.taverna.raven.plugins.ui.CheckForUpdatesDialog.java

private void initComponents() {
    // Base font for all components on the form
    Font baseFont = new JLabel("base font").getFont().deriveFont(11f);

    // Message saying that updates are available
    JPanel messagePanel = new JPanel(new BorderLayout());
    messagePanel.setBorder(/*from  ww  w.ja va  2s .com*/
            new CompoundBorder(new EmptyBorder(10, 10, 10, 10), new EtchedBorder(EtchedBorder.LOWERED)));
    JLabel message = new JLabel(
            "<html><body>Updates are available for some Taverna components. To review and <br>install them go to 'Updates and plugins' in the 'Advanced' menu.</body><html>");
    message.setFont(baseFont.deriveFont(12f));
    message.setBorder(new EmptyBorder(5, 5, 5, 5));
    message.setIcon(UpdatesAvailableIcon.updateIcon);
    messagePanel.add(message, BorderLayout.CENTER);

    // Buttons
    JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    JButton okButton = new JButton("OK"); // we'll check for updates again in 2 weeks
    okButton.setFont(baseFont);
    okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            okPressed();
        }
    });

    buttonsPanel.add(okButton);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(messagePanel, BorderLayout.CENTER);
    getContentPane().add(buttonsPanel, BorderLayout.SOUTH);

    pack();
    setResizable(false);
    // Center the dialog on the screen (we do not have the parent)
    Dimension dimension = getToolkit().getScreenSize();
    Rectangle abounds = getBounds();
    setLocation((dimension.width - abounds.width) / 2, (dimension.height - abounds.height) / 2);
    setSize(getPreferredSize());
}

From source file:EditorPaneTest.java

public EditorPaneFrame() {
    setTitle("EditorPaneTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    final Stack<String> urlStack = new Stack<String>();
    final JEditorPane editorPane = new JEditorPane();
    final JTextField url = new JTextField(30);

    // set up hyperlink listener

    editorPane.setEditable(false);//from   www .  j  a v a  2s .co  m
    editorPane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent event) {
            if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                try {
                    // remember URL for back button
                    urlStack.push(event.getURL().toString());
                    // show URL in text field
                    url.setText(event.getURL().toString());
                    editorPane.setPage(event.getURL());
                } catch (IOException e) {
                    editorPane.setText("Exception: " + e);
                }
            }
        }
    });

    // set up checkbox for toggling edit mode

    final JCheckBox editable = new JCheckBox();
    editable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            editorPane.setEditable(editable.isSelected());
        }
    });

    // set up load button for loading URL

    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                // remember URL for back button
                urlStack.push(url.getText());
                editorPane.setPage(url.getText());
            } catch (IOException e) {
                editorPane.setText("Exception: " + e);
            }
        }
    };

    JButton loadButton = new JButton("Load");
    loadButton.addActionListener(listener);
    url.addActionListener(listener);

    // set up back button and button action

    JButton backButton = new JButton("Back");
    backButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (urlStack.size() <= 1)
                return;
            try {
                // get URL from back button
                urlStack.pop();
                // show URL in text field
                String urlString = urlStack.peek();
                url.setText(urlString);
                editorPane.setPage(urlString);
            } catch (IOException e) {
                editorPane.setText("Exception: " + e);
            }
        }
    });

    add(new JScrollPane(editorPane), BorderLayout.CENTER);

    // put all control components in a panel

    JPanel panel = new JPanel();
    panel.add(new JLabel("URL"));
    panel.add(url);
    panel.add(loadButton);
    panel.add(backButton);
    panel.add(new JLabel("Editable"));
    panel.add(editable);

    add(panel, BorderLayout.SOUTH);
}

From source file:PrintTest.java

public PrintTestFrame() {
    setTitle("PrintTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    canvas = new PrintComponent();
    add(canvas, BorderLayout.CENTER);

    attributes = new HashPrintRequestAttributeSet();

    JPanel buttonPanel = new JPanel();
    JButton printButton = new JButton("Print");
    buttonPanel.add(printButton);/*ww w.ja va 2s  .  c  om*/
    printButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPrintable(canvas);
                if (job.printDialog(attributes))
                    job.print(attributes);
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(PrintTestFrame.this, e);
            }
        }
    });

    JButton pageSetupButton = new JButton("Page setup");
    buttonPanel.add(pageSetupButton);
    pageSetupButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.pageDialog(attributes);
        }
    });

    add(buttonPanel, BorderLayout.NORTH);
}

From source file:com.game.ui.views.UserDialog.java

public UserDialog(String message, JFrame frame) {
    setLayout(new BorderLayout(5, 5));
    setModalityType(ModalityType.APPLICATION_MODAL);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setResizable(false);/*w w  w . j a  va  2s. co  m*/
    ImageIcon icon = null;
    try {
        icon = GameUtils.shrinkImage("warning.gif", 30, 30);
    } catch (IOException e) {
        System.out.println("Dialog : showDialogForMap(): Exception occured :" + e);
        e.printStackTrace();
    }
    JPanel panel = new JPanel();
    JLabel label = new JLabel(icon);
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    label.setText(message);
    label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    label.setHorizontalAlignment(0);
    panel.add(label);
    add(panel, BorderLayout.NORTH);
    JPanel contentPanel = new JPanel();
    contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
    txt = new JTextField();
    txt.setPreferredSize(new Dimension(150, 30));
    txt.setAlignmentX(.5f);
    txt.setMaximumSize(new Dimension(150, 30));
    contentPanel.add(txt);
    contentPanel.add(Box.createVerticalStrut(10));
    JButton btn = new JButton("Submit.");
    btn.setAlignmentX(.5f);
    btn.setPreferredSize(new Dimension(50, 25));
    btn.addActionListener(this);
    validationMess = new JLabel("All fields are mandatory");
    validationMess.setVisible(false);
    validationMess.setForeground(Color.red);
    validationMess.setAlignmentX(.5f);
    contentPanel.add(btn);
    contentPanel.add(Box.createVerticalStrut(10));
    contentPanel.add(validationMess);
    contentPanel.add(Box.createVerticalGlue());
    add(contentPanel, BorderLayout.CENTER);
    pack();
    setSize(new Dimension(300, 200));
    setLocationRelativeTo(frame);
    setVisible(true);
}

From source file:TreeEditTest.java

/**
 * Makes the buttons to add a sibling, add a child, and delete a node.
 *//*from   w  ww.  java  2 s  .c  o  m*/
public void makeButtons() {
    JPanel panel = new JPanel();
    JButton addSiblingButton = new JButton("Add Sibling");
    addSiblingButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (selectedNode == null)
                return;

            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();

            if (parent == null)
                return;

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");

            int selectedIndex = parent.getIndex(selectedNode);
            model.insertNodeInto(newNode, parent, selectedIndex + 1);

            // now display new node

            TreeNode[] nodes = model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            tree.scrollPathToVisible(path);
        }
    });
    panel.add(addSiblingButton);

    JButton addChildButton = new JButton("Add Child");
    addChildButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (selectedNode == null)
                return;

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
            model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());

            // now display new node

            TreeNode[] nodes = model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            tree.scrollPathToVisible(path);
        }
    });
    panel.add(addChildButton);

    JButton deleteButton = new JButton("Delete");
    deleteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (selectedNode != null && selectedNode.getParent() != null)
                model.removeNodeFromParent(selectedNode);
        }
    });
    panel.add(deleteButton);
    add(panel, BorderLayout.SOUTH);
}

From source file:be.fedict.eid.tsl.tool.SignSelectPkcs11FinishablePanel.java

@Override
public Component getComponent() {
    LOG.debug("get component");
    if (null == this.component) {
        /*// w ww  .  j av  a2  s  .c  o  m
         * We need to return the same component each time, else the
         * validate() logic doesn't work as expected.
         */
        JPanel panel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
        panel.setLayout(boxLayout);
        JPanel infoPanel = new JPanel();
        infoPanel.add(new JLabel("Please select a PKCS#11 library."));
        panel.add(infoPanel);

        JPanel browsePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.add(browsePanel);
        browsePanel.add(new JLabel("PKCS#11 library:"));
        this.pkcs11TextField = new JTextField(30);
        browsePanel.add(this.pkcs11TextField);
        JButton browseButton = new JButton("Browse...");
        browseButton.addActionListener(this);
        browsePanel.add(browseButton);

        JPanel slotIdxPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.add(slotIdxPanel);
        slotIdxPanel.add(new JLabel("Slot index:"));
        SpinnerModel spinnerModel = new SpinnerNumberModel(0, 0, 10, 1);
        this.slotIdxSpinner = new JSpinner(spinnerModel);
        slotIdxPanel.add(this.slotIdxSpinner);

        this.component = panel;
    }
    return this.component;
}