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:SwingToolBarDemo.java

protected JButton makeNavigationButton(String imageName, String actionCommand, String toolTipText,
        String altText) {// w  w w  . j  a v  a2  s  . c  om
    //Look for the image.
    String imgLocation = "toolbarButtonGraphics/navigation/" + imageName + ".gif";
    URL imageURL = SwingToolBarDemo.class.getResource(imgLocation);

    //Create and initialize the button.
    JButton button = new JButton();
    button.setActionCommand(actionCommand);
    button.setToolTipText(toolTipText);
    button.addActionListener(this);

    if (imageURL != null) { //image found
        button.setIcon(new ImageIcon(imageURL, altText));
    } else { //no image found
        button.setText(altText);
        System.err.println("Resource not found: " + imgLocation);
    }

    return button;
}

From source file:org.jfree.chart.demo.SecondaryDatasetDemo1.java

/**
 * Constructs a new demonstration application.
 *
 * @param title  the frame title.//from w w  w .  j a va2s . com
 */
public SecondaryDatasetDemo1(String title) {

    super(title);
    TimeSeriesCollection dataset1 = createRandomDataset("Series 1");
    JFreeChart chart = ChartFactory.createTimeSeriesChart("Secondary 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));
    ValueAxis axis = this.plot.getDomainAxis();
    axis.setAutoRange(true);

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

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

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

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

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

    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.emental.mindraider.ui.dialogs.DownloadModelJDialog.java

/**
 * Constructor./*from  ww w .j  a va 2  s  .  co m*/
 * 
 * @param union
 *            made union of models.
 */
public DownloadModelJDialog(boolean union) {
    super(Messages.getString("DownloadModelJDialog.title"));

    JPanel p = new JPanel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 5));

    p.add(new JLabel(Messages.getString("DownloadModelJDialog.url")));

    String[] knowUris = new String[] { "http://", "http://wymiwyg.org/", "http://www.osar.ch",
            "http://wymiwyg.org/.rdf?appendLang=en&till=50", "http://www.osar.ch/.rdf?appendLang=de&till=50" };
    modelUrlCombo = new JComboBox(knowUris);
    modelUrlCombo.setEditable(true);
    modelUrlCombo.addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent keyEvent) {
            if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
                upload();
            }
        }

        public void keyReleased(KeyEvent keyEvent) {
        }

        public void keyTyped(KeyEvent keyEvent) {
        }
    });
    p.add(modelUrlCombo);

    JButton uploadButton = new JButton(Messages.getString("DownloadModelJDialog.download"));
    uploadButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            upload();
        }
    });
    p.add(uploadButton);

    JButton cancelButton = new JButton(Messages.getString("DownloadModelJDialog.cancel"));
    cancelButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });
    p.add(cancelButton);

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

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

From source file:DataExchangeTest.java

public PasswordChooser() {
    setLayout(new BorderLayout());

    // construct a panel with user name and password fields

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2, 2));
    panel.add(new JLabel("User name:"));
    panel.add(username = new JTextField(""));
    panel.add(new JLabel("Password:"));
    panel.add(password = new JPasswordField(""));
    add(panel, BorderLayout.CENTER);

    // create Ok and Cancel buttons that terminate the dialog

    okButton = new JButton("Ok");
    okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            ok = true;/*from  ww w .ja v  a2s.c  om*/
            dialog.setVisible(false);
        }
    });

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            dialog.setVisible(false);
        }
    });

    // add buttons to southern border

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(okButton);
    buttonPanel.add(cancelButton);
    add(buttonPanel, BorderLayout.SOUTH);
}

From source file:org.jfree.chart.demo.SecondaryDatasetDemo2.java

/**
 * Constructs a new demonstration application.
 *
 * @param title  the frame title./*ww w .ja  va2s .  c  o m*/
 */
public SecondaryDatasetDemo2(final String title) {

    super(title);
    final CategoryDataset dataset1 = createRandomDataset("Series 1");
    final JFreeChart chart = ChartFactory.createLineChart("Secondary Dataset Demo 2", "Category", "Value",
            dataset1, PlotOrientation.VERTICAL, true, true, false);
    chart.setBackgroundPaint(Color.white);

    this.plot = chart.getCategoryPlot();
    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 NumberAxis rangeAxis = (NumberAxis) this.plot.getRangeAxis();
    rangeAxis.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.google.code.facebook.graph.sna.applet.DrawnIconVertexDemo.java

public DrawnIconVertexDemo() {

    // create a simple graph for the demo
    graph = new DirectedSparseGraph<Integer, Number>();
    Integer[] v = createVertices(10);
    createEdges(v);//from   ww w. ja va2 s. c om

    vv = new VisualizationViewer<Integer, Number>(new FRLayout<Integer, Number>(graph));
    vv.getRenderContext().setVertexLabelTransformer(new Transformer<Integer, String>() {

        public String transform(Integer v) {
            return "Vertex " + v;
        }
    });
    vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.cyan));
    vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.cyan));

    vv.getRenderContext().setVertexIconTransformer(new Transformer<Integer, Icon>() {

        /*
         * Implements the Icon interface to draw an Icon with background color and
         * a text label
         */
        public Icon transform(final Integer v) {
            return new Icon() {

                public int getIconHeight() {
                    return 20;
                }

                public int getIconWidth() {
                    return 20;
                }

                public void paintIcon(Component c, Graphics g, int x, int y) {
                    if (vv.getPickedVertexState().isPicked(v)) {
                        g.setColor(Color.yellow);
                    } else {
                        g.setColor(Color.red);
                    }
                    g.fillOval(x, y, 20, 20);
                    if (vv.getPickedVertexState().isPicked(v)) {
                        g.setColor(Color.black);
                    } else {
                        g.setColor(Color.white);
                    }
                    g.drawString("" + v, x + 6, y + 15);

                }
            };
        }
    });

    vv.getRenderContext().setVertexFillPaintTransformer(
            new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.white, Color.yellow));
    vv.getRenderContext().setEdgeDrawPaintTransformer(
            new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.lightGray));

    vv.setBackground(Color.white);

    // add my listener for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller<Integer>());

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final ModalGraphMouse gm = new DefaultModalGraphMouse<Integer, Number>();
    vv.setGraphMouse(gm);

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(((DefaultModalGraphMouse<Integer, Number>) gm).getModeComboBox());
    content.add(controls, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
}

From source file:ClassTree.java

/**
 * Add the text field and "Add" button to add a new class.
 *//*from  w w w  . j a v a  2 s .  com*/
public void addTextField() {
    JPanel panel = new JPanel();

    ActionListener addListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            // add the class whose name is in the text field
            try {
                String text = textField.getText();
                addClass(Class.forName(text)); // clear text field to indicate success
                textField.setText("");
            } catch (ClassNotFoundException e) {
                JOptionPane.showMessageDialog(null, "Class not found");
            }
        }
    };

    // new class names are typed into this text field
    textField = new JTextField(20);
    textField.addActionListener(addListener);
    panel.add(textField);

    JButton addButton = new JButton("Add");
    addButton.addActionListener(addListener);
    panel.add(addButton);

    add(panel, BorderLayout.SOUTH);
}

From source file:be.ac.ua.comp.scarletnebula.gui.DecoratedCommunicationPanel.java

final public void clearAndFill(final Collection<Server> selectedServers) {

    // Remove all components on there
    invalidate();//from   w  w w. j av  a  2s. co  m
    removeAll();

    setLayout(new BorderLayout());

    // If there are no servers, or none of the servers are running, do not
    // display the ssh console
    final Collection<Server> connectableServers = new ArrayList<Server>();
    for (final Server s : selectedServers) {
        if (s.getStatus() == VmState.RUNNING && s.getPublicDnsAddress() != null) {
            connectableServers.add(s);
        }
    }

    // If there are no servers to connect to, don't draw the ssh console
    if (connectableServers.size() == 0) {
        log.info("Connection tab clicked and no servers selected to connect to.");
        final BetterTextLabel txt = new BetterTextLabel(
                "Please select at least one running server to connect to.");
        txt.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 20));
        add(txt, BorderLayout.CENTER);
        validate();
        repaint();
    } else {
        final Server connectServer = selectedServers.iterator().next();

        final JPanel propertiesPanel = new JPanel();
        propertiesPanel.setLayout(new BoxLayout(propertiesPanel, BoxLayout.LINE_AXIS));
        propertiesPanel.add(Box.createHorizontalGlue());
        final JButton propertiesButton = new JButton("Properties", Utils.icon("modify16.png"));
        propertiesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                final ChangeServerSshLoginMethodWindow win = new ChangeServerSshLoginMethodWindow(parent,
                        connectServer);
                win.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        clearAndFill(selectedServers);
                    }
                });
                win.setVisible(true);
            }
        });

        final JButton restartButton = new JButton("Restart connection", Utils.icon("undo16.png"));
        restartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                clearAndFill(selectedServers);
            }
        });
        propertiesPanel.add(restartButton);
        propertiesPanel.add(Box.createHorizontalStrut(10));
        propertiesPanel.add(propertiesButton);
        propertiesPanel.add(Box.createHorizontalStrut(20));
        propertiesPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
        add(propertiesPanel, BorderLayout.NORTH);

        final SSHPanel sshPanel = new SSHPanel(connectServer);
        sshPanel.setBorder(BorderFactory.createEmptyBorder(5, 20, 10, 20));
        add(sshPanel, BorderLayout.CENTER);

        validate();
        repaint();
    }
}

From source file:com.github.pemapmodder.pocketminegui.gui.startup.installer.ServerSetupCard.java

@SuppressWarnings("unchecked")
public ServerSetupCard(InstallServerActivity activity) {
    JButton serverPropertiesButton = new JButton("General server properties");
    serverPropertiesButton.addActionListener(e -> new ServerOptionsActivity("Server properties editor",
            activity, new HashMap<>(SERVER_PROPERTIES_MAP), new HashMap<>(SERVER_PROPERTIES_DESC_MAP)).init());
    JButton pocketmineOpts = new JButton("PocketMine-specific settings");
    pocketmineOpts.addActionListener(e -> new ServerOptionsActivity("PocketMine settings editor", activity,
            new HashMap<>(POCKETMINE_YML_MAP), new HashMap<>(POCKETMINE_YML_DESC_MAP)).init());
    add(serverPropertiesButton);//from  w w w .  jav  a  2 s . c  o  m
    add(pocketmineOpts);
}

From source file:edu.uci.ics.jung.samples.DrawnIconVertexDemo.java

@SuppressWarnings("unchecked")
public DrawnIconVertexDemo() {

    // create a simple graph for the demo
    graph = new DirectedSparseGraph<Integer, Number>();
    Integer[] v = createVertices(10);
    createEdges(v);/*from w ww .ja v  a  2s . c  om*/

    vv = new VisualizationViewer<Integer, Number>(new FRLayout<Integer, Number>(graph));
    vv.getRenderContext().setVertexLabelTransformer(new Transformer<Integer, String>() {

        public String transform(Integer v) {
            return "Vertex " + v;
        }
    });
    vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.cyan));
    vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.cyan));

    vv.getRenderContext().setVertexIconTransformer(new Transformer<Integer, Icon>() {

        /*
         * Implements the Icon interface to draw an Icon with background color and
         * a text label
         */
        public Icon transform(final Integer v) {
            return new Icon() {

                public int getIconHeight() {
                    return 20;
                }

                public int getIconWidth() {
                    return 20;
                }

                public void paintIcon(Component c, Graphics g, int x, int y) {
                    if (vv.getPickedVertexState().isPicked(v)) {
                        g.setColor(Color.yellow);
                    } else {
                        g.setColor(Color.red);
                    }
                    g.fillOval(x, y, 20, 20);
                    if (vv.getPickedVertexState().isPicked(v)) {
                        g.setColor(Color.black);
                    } else {
                        g.setColor(Color.white);
                    }
                    g.drawString("" + v, x + 6, y + 15);

                }
            };
        }
    });

    vv.getRenderContext().setVertexFillPaintTransformer(
            new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.white, Color.yellow));
    vv.getRenderContext().setEdgeDrawPaintTransformer(
            new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.lightGray));

    vv.setBackground(Color.white);

    // add my listener for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller<Integer>());

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final ModalGraphMouse gm = new DefaultModalGraphMouse<Integer, Number>();
    vv.setGraphMouse(gm);

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(((DefaultModalGraphMouse<Integer, Number>) gm).getModeComboBox());
    content.add(controls, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
}