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.MultipleDatasetDemo1.java

/**
 * Constructs a new demonstration application.
 *
 * @param title  the frame title./*from   w w  w. j a  v  a 2s . c o m*/
 */
public MultipleDatasetDemo1(final String title) {

    super(title);
    final TimeSeriesCollection dataset1 = createRandomDataset("Series 1");
    final JFreeChart chart = ChartFactory.createTimeSeriesChart("Multiple Dataset Demo 1", "Time", "Value",
            dataset1, true, true, false);
    chart.setBackgroundPaint(Color.white);

    this.plot = chart.getXYPlot();
    this.plot.setBackgroundPaint(Color.lightGray);
    this.plot.setDomainGridlinePaint(Color.white);
    this.plot.setRangeGridlinePaint(Color.white);
    //        this.plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 4, 4, 4, 4));
    final ValueAxis axis = this.plot.getDomainAxis();
    axis.setAutoRange(true);

    final NumberAxis rangeAxis2 = new NumberAxis("Range Axis 2");
    rangeAxis2.setAutoRangeIncludesZero(false);

    final JPanel content = new JPanel(new BorderLayout());

    final ChartPanel chartPanel = new ChartPanel(chart);
    content.add(chartPanel);

    final JButton button1 = new JButton("Add Dataset");
    button1.setActionCommand("ADD_DATASET");
    button1.addActionListener(this);

    final JButton button2 = new JButton("Remove Dataset");
    button2.setActionCommand("REMOVE_DATASET");
    button2.addActionListener(this);

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

    content.add(buttonPanel, BorderLayout.SOUTH);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(content);

}

From source file:com.experiments.DynamicDataDemo.java

/**
 * Constructs a new demonstration application.
 * //  w  ww.j  a  v a  2s . c o m
 * @param title
 *            the frame title.
 */
public DynamicDataDemo(final String title) {

    super(title);
    this.series = new TimeSeries("Random Data", Millisecond.class);
    final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
    final JFreeChart chart = createChart(dataset);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setFillZoomRectangle(false);
    final JButton button = new JButton("Add New Data Item");
    button.setActionCommand("ADD_DATA");
    button.addActionListener(this);
    final JButton start_button = new JButton("Start adding");
    start_button.setActionCommand("START_ADDING");
    start_button.addActionListener(this);
    final JButton stop_button = new JButton("Stop adding");
    stop_button.setActionCommand("STOP_ADDING");
    stop_button.addActionListener(this);
    final JPanel button_panel = new JPanel();
    button_panel.add(button);
    button_panel.add(start_button);
    button_panel.add(stop_button);

    final JPanel content = new JPanel(new BorderLayout());
    content.add(chartPanel);
    content.add(button_panel, BorderLayout.SOUTH);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(content);

}

From source file:teambootje.A5.java

/**
 * Creates new form A5/*ww w . j  a  va 2 s.co m*/
 */
public A5() {
    initComponents();
    setLocationRelativeTo(null);
    setLayout(new BorderLayout());

    //Create and set up the window.
    setTitle("SS Rotterdam Analyse || Analyse 5");
    ImageIcon icon = new ImageIcon("img/bootje.jpg");
    setIconImage(icon.getImage());

    // back BTN
    JButton back = new JButton("Back");
    add(back, BorderLayout.NORTH);

    back.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
            // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    });

    // panel en Label
    JPanel ana = new JPanel();
    add(ana, BorderLayout.CENTER);

    //tabel
    String sql = "SELECT Leeftijd, COUNT(*) AS Aantal FROM persoon GROUP BY Leeftijd";
    List<Object[]> list = new ArrayList<Object[]>();
    ResultSet rs = null;
    try {
        rs = db.runSql(sql);
        while (rs.next()) {
            String age = rs.getString("Leeftijd");
            int aantal = rs.getInt("Aantal");
            String[] row = new String[rs.getMetaData().getColumnCount()];
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                row[i - 1] = rs.getString(i);
            }
            list.add(row);

            //chart
            JButton chart = new JButton("Chart");
            add(chart, BorderLayout.SOUTH);

            chart.addActionListener(new ActionListener() {
                String leeftijd = age;
                int a1 = aantal;

                @Override
                public void actionPerformed(ActionEvent e) {

                    DefaultPieDataset pieDataset = new DefaultPieDataset();
                    pieDataset.setValue("Niet vrijgegeven", a1);

                    JFreeChart chart = ChartFactory.createPieChart3D("Aantal mensen per leeftijd", pieDataset,
                            true, true, true);
                    PiePlot3D p = (PiePlot3D) chart.getPlot();
                    //p.setForegroundAlpha(TOP_ALIGNMENT);
                    ChartFrame pie = new ChartFrame("Aantal mensen per leeftijd", chart);
                    pie.setVisible(true);
                    pie.setSize(500, 500);
                    pie.setLocationRelativeTo(null);

                    // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }
            });
        }
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e);
    }

    Object[][] array = new Object[list.size()][];
    Object columnNames[] = { "Leeftijd", "Aantal" };
    list.toArray(array);

    JTable table = new JTable(array, columnNames);
    JScrollPane scroll = new JScrollPane(table);
    scroll.setPreferredSize(new Dimension(400, 400));
    ana.add(scroll);
}

From source file:com.emental.mindraider.ui.dialogs.SearchConceptAnnotation.java

/**
 * Constructor.//w  w w  .  j a va 2s  .c o  m
 */
public SearchConceptAnnotation(AbstractTextAnnotationRenderer renderer) {
    super("Search Annotation");

    this.renderer = renderer;

    JPanel dialogPanel = new JPanel();
    dialogPanel.setBorder(new EmptyBorder(5, 10, 0, 10));
    dialogPanel.setLayout(new BorderLayout());

    JPanel contentAndButtons = new JPanel(new GridLayout(2, 1));
    JPanel contentPanel = new JPanel(new BorderLayout());

    // 1a.
    // TODO add help like in eclipse
    contentPanel.add(new JLabel(Messages.getString("FtsJDialog.searchString")), BorderLayout.NORTH);
    // 1b.
    searchCombo = new JComboBox(history.toArray());
    searchCombo.setSelectedItem("");
    searchCombo.setPreferredSize(new Dimension(200, 18));
    searchCombo.setEditable(true);
    searchCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if ("comboBoxEdited".equals(e.getActionCommand())) {
                search();
            }
        }
    });
    contentPanel.add(searchCombo, BorderLayout.SOUTH);
    contentAndButtons.add(contentPanel);

    // 2.
    JPanel p = new JPanel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 5));
    JButton searchButton = new JButton(Messages.getString("FtsJDialog.searchButton"));
    searchButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            search();
        }
    });
    p.add(searchButton);

    JButton cancelButton = new JButton(Messages.getString("FtsJDialog.cancel"));
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });
    p.add(cancelButton);

    contentAndButtons.add(p);

    dialogPanel.add(contentAndButtons, BorderLayout.CENTER);

    getContentPane().add(dialogPanel, BorderLayout.CENTER);

    // show
    pack();
    Gfx.centerAndShowWindow(this);
}

From source file:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);

    Box info = Box.createVerticalBox();
    info.add(new Label("Please wait 3 seconds"));
    final JButton continueButton = new JButton("Continue");
    info.add(continueButton);//ww  w .  j av  a2 s .  c om

    JDialog d = new JDialog();
    d.setModalityType(ModalityType.APPLICATION_MODAL);
    d.setContentPane(info);
    d.pack();

    continueButton.addActionListener(e -> d.dispose());
    continueButton.setVisible(false);

    SwingWorker sw = new SwingWorker<Integer, Integer>() {
        protected Integer doInBackground() throws Exception {
            int i = 0;
            while (i++ < 30) {
                System.out.println(i);
                Thread.sleep(100);
            }
            return null;
        }

        @Override
        protected void done() {
            continueButton.setVisible(true);
        }

    };
    JButton button = new JButton("Click Me");
    button.addActionListener(e -> {
        sw.execute();
        d.setVisible(true);
    });
    add(button);
}

From source file:com.intuit.tank.tools.debugger.VariableDialog.java

/**
 * @return//w  w  w. j a v  a 2s  .co  m
 */
private Component createButtonPanel() {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING, 10, 5));
    JButton saveBT = new JButton("Save");
    saveBT.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (table.isEditing()) {
                table.getCellEditor().stopCellEditing();
            }
            Map<String, String> ret = new HashMap<String, String>();
            for (int row = 0; row < table.getModel().getRowCount(); row++) {
                String key = (String) table.getModel().getValueAt(row, 0);
                String value = (String) table.getModel().getValueAt(row, 1);
                if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
                    ret.put(key, value);
                }
            }
            f.setProjectVariables(ret);
            setVisible(false);
        }
    });
    panel.add(saveBT);

    JButton cancelBT = new JButton("Close");
    cancelBT.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            setVisible(false);
        }
    });
    panel.add(cancelBT);

    JButton addBt = new JButton("Add Variable");
    addBt.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (table.isEditing()) {
                table.getCellEditor().stopCellEditing();
            }
            try {
                ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Key", "Value" });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    panel.add(addBt);

    deleteBT = new JButton("Delete Variable");
    deleteBT.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (table.isEditing()) {
                table.getCellEditor().stopCellEditing();
            }
            try {
                int selectedRow = table.getSelectedRow();
                ((DefaultTableModel) table.getModel()).removeRow(table.getSelectedRow());
                if (selectedRow > 0) {
                    table.getSelectionModel().setSelectionInterval(selectedRow - 1, selectedRow - 1);
                } else if (table.getRowCount() > 0) {
                    table.getSelectionModel().setSelectionInterval(0, 0);
                } else {
                    table.getSelectionModel().clearSelection();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    deleteBT.setEnabled(false);
    panel.add(deleteBT);
    return panel;
}

From source file:com.orthancserver.OrthancConfigurationDialog.java

public OrthancConfigurationDialog() {
    final JPanel contentPanel = new JPanel();
    contentPanel.setBorder(new EmptyBorder(20, 5, 5, 5));
    contentPanel.setLayout(new GridLayout2(0, 2, 20, 5));

    JLabel label = new JLabel("Name:");
    label.setHorizontalAlignment(JLabel.RIGHT);
    contentPanel.add(label);/* ww  w. j av  a 2 s.co m*/
    contentPanel.add(name_);

    label = new JLabel("URL:");
    label.setHorizontalAlignment(JLabel.RIGHT);
    contentPanel.add(label);
    contentPanel.add(url_);

    label = new JLabel("Username:");
    label.setHorizontalAlignment(JLabel.RIGHT);
    contentPanel.add(label);
    contentPanel.add(username_);

    label = new JLabel("Password:");
    label.setHorizontalAlignment(JLabel.RIGHT);
    contentPanel.add(label);
    contentPanel.add(password_);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(contentPanel, BorderLayout.NORTH);

    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
    getContentPane().add(buttonPane, BorderLayout.SOUTH);
    {
        {
            JButton test = new JButton("Test connection");
            test.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg) {
                    OrthancConnection orthanc = CreateConnection();
                    try {
                        JSONObject system = (JSONObject) orthanc.ReadJson("system");
                        JOptionPane.showMessageDialog(null,
                                "Successfully connected to this Orthanc server " + "(version: "
                                        + (String) system.get("Version") + ")!",
                                "Success", JOptionPane.INFORMATION_MESSAGE);
                    } catch (IOException e) {
                        JOptionPane.showMessageDialog(null, "Cannot connect to this Orthanc server!", "Error",
                                JOptionPane.ERROR_MESSAGE);
                    }
                }
            });
            buttonPane.add(test);
        }
        {
            JButton okButton = new JButton("Add");
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg) {
                    isSuccess_ = true;
                    setVisible(false);
                }
            });
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);
        }
        {
            JButton cancelButton = new JButton("Cancel");
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg) {
                    setVisible(false);
                }
            });
            buttonPane.add(cancelButton);
        }
    }

    setUndecorated(false);
    setSize(500, 500);
    setTitle("Add new server");
    setModal(true);
}

From source file:teambootje.A6.java

/**
 * Creates new form A6/*from  w w w.  j  a v  a 2 s  .c o m*/
 */
public A6() {
    initComponents();
    setLocationRelativeTo(null);
    setLayout(new BorderLayout());

    //Create and set up the window.
    setTitle("SS Rotterdam Analyse || Analyse 6");
    ImageIcon icon = new ImageIcon("img/bootje.jpg");
    setIconImage(icon.getImage());

    // back BTN
    JButton back = new JButton("Back");
    add(back, BorderLayout.NORTH);

    back.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
            //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    });

    // panel en Label
    JPanel ana = new JPanel();
    add(ana, BorderLayout.CENTER);

    //tabel
    String sql = "SELECT Locatie.land, locatie.stad, count(persoon.LID) as Aantal FROM persoon, Locatie WHERE persoon.LID = locatie.LID GROUP BY stad";
    List<Object[]> list = new ArrayList<Object[]>();
    ResultSet rs = null;
    try {
        rs = db.runSql(sql);
        while (rs.next()) {
            String city = rs.getString("locatie.stad");
            int amount = rs.getInt("Aantal");
            String[] row = new String[rs.getMetaData().getColumnCount()];
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                row[i - 1] = rs.getString(i);
            }
            list.add(row);

            //chart
            JButton chart = new JButton("Chart");
            add(chart, BorderLayout.SOUTH);

            chart.addActionListener(new ActionListener() {
                String c1 = city;
                int a1 = amount;

                @Override
                public void actionPerformed(ActionEvent e) {
                    DefaultPieDataset pieDataset = new DefaultPieDataset();
                    pieDataset.setValue(c1, a1);
                    pieDataset.setValue("Rotterdam", new Integer(1));
                    pieDataset.setValue("Bergen op zoom", new Integer(1));

                    JFreeChart chart = ChartFactory.createPieChart3D("Waar komen bezoekers vandaan", pieDataset,
                            true, true, true);
                    PiePlot3D p = (PiePlot3D) chart.getPlot();
                    //p.setForegroundAlpha(TOP_ALIGNMENT);
                    ChartFrame pie = new ChartFrame("Waar komen bezoekers vandaan", chart);
                    pie.setVisible(true);
                    pie.setSize(500, 500);
                    pie.setLocationRelativeTo(null);

                    //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }
            });
        }
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e);
    }

    Object[][] array = new Object[list.size()][];
    Object columnNames[] = { "Land", "stad", "Aantal" };
    list.toArray(array);

    JTable table = new JTable(array, columnNames);
    JScrollPane scroll = new JScrollPane(table);
    scroll.setPreferredSize(new Dimension(400, 400));
    ana.add(scroll);
}

From source file:com.l2jfree.config.gui.Configurator.java

private JButton getStoreJButton() {
    if (_storeJButton == null) {
        final JButton jButton = new JButton("Store to file");

        jButton.addActionListener(new ActionListener() {
            @Override//from w  w w.  ja  v  a  2s . co m
            public void actionPerformed(ActionEvent e) {
                // LOW implement
            }
        });

        _storeJButton = jButton;
    }

    return _storeJButton;
}