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

public OptionDialogFrame() {
    setTitle("OptionDialogTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    JPanel gridPanel = new JPanel();
    gridPanel.setLayout(new GridLayout(2, 3));

    typePanel = new ButtonPanel("Type", "Message", "Confirm", "Option", "Input");
    messageTypePanel = new ButtonPanel("Message Type", "ERROR_MESSAGE", "INFORMATION_MESSAGE",
            "WARNING_MESSAGE", "QUESTION_MESSAGE", "PLAIN_MESSAGE");
    messagePanel = new ButtonPanel("Message", "String", "Icon", "Component", "Other", "Object[]");
    optionTypePanel = new ButtonPanel("Confirm", "DEFAULT_OPTION", "YES_NO_OPTION", "YES_NO_CANCEL_OPTION",
            "OK_CANCEL_OPTION");
    optionsPanel = new ButtonPanel("Option", "String[]", "Icon[]", "Object[]");
    inputPanel = new ButtonPanel("Input", "Text field", "Combo box");

    gridPanel.add(typePanel);//  ww  w.  ja  va 2 s  .  c o m
    gridPanel.add(messageTypePanel);
    gridPanel.add(messagePanel);
    gridPanel.add(optionTypePanel);
    gridPanel.add(optionsPanel);
    gridPanel.add(inputPanel);

    // add a panel with a Show button

    JPanel showPanel = new JPanel();
    JButton showButton = new JButton("Show");
    showButton.addActionListener(new ShowAction());
    showPanel.add(showButton);

    add(gridPanel, BorderLayout.CENTER);
    add(showPanel, BorderLayout.SOUTH);
}

From source file:components.ToolBarDemo.java

protected JButton makeNavigationButton(String imageName, String actionCommand, String toolTipText,
        String altText) {/*from  w  ww  .  ja v  a 2s  .co m*/
    //Look for the image.
    String imgLocation = "images/" + imageName + ".gif";
    URL imageURL = ToolBarDemo.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:gda.util.userOptions.UserOptionsDialog.java

/**
 * @param frame// w  w  w . ja  v a2s . com
 * @param parent
 * @param options
 */
public UserOptionsDialog(JFrame frame, Component parent, UserOptions options) {
    super(frame, options.title, true);
    this.frame = frame;
    this.options = options;
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    label = new JLabel(options.title != null ? options.title : "");
    label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    label.setAlignmentX(Component.CENTER_ALIGNMENT);
    JPanel pane = makePane();
    // pane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    pane.setAlignmentX(Component.CENTER_ALIGNMENT);
    // pane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    panel.add(label);
    panel.add(pane);

    JPanel btnPanel = new JPanel();
    btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));

    JButton okButton = new JButton("OK");
    okButton.addActionListener(this);

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

    JButton defButton = new JButton("Default");
    defButton.addActionListener(this);

    JButton resetButton = new JButton("Reset");
    resetButton.addActionListener(this);

    btnPanel.add(Box.createHorizontalGlue());
    btnPanel.add(okButton);
    btnPanel.add(Box.createHorizontalGlue());
    btnPanel.add(cancelButton);
    btnPanel.add(Box.createHorizontalGlue());
    btnPanel.add(defButton);
    btnPanel.add(Box.createHorizontalGlue());
    btnPanel.add(resetButton);
    btnPanel.add(Box.createHorizontalGlue());

    btnPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    panel.add(btnPanel);

    getContentPane().add(panel);
    getRootPane().setDefaultButton(cancelButton);
    pack();
    setLocationRelativeTo(parent);
    setVisible(true);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}

From source file:amicity.graph.pc.gui.GraphEditor.java

public GraphEditor(JungGraph aGraph) {
    super(aGraph);
    Factory<Node> vertexFactory = new NodeFactory(this.graph);
    Factory<Edge> edgeFactory = new EdgeFactory();

    final GraphEditorEventHandler eventHandler = new GraphEditorEventHandler(vv.getRenderContext(),
            vertexFactory, edgeFactory);

    // the EditingGraphMouse will pass mouse event coordinates to the
    // vertexLocations function to set the locations of the vertices as
    // they are created
    vv.setGraphMouse(eventHandler);/*from ww w . j  a  va2s . c om*/
    vv.addKeyListener(eventHandler.getModeKeyListener());

    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());
        }
    });

    JButton layoutButton = new JButton("force-directed layout");
    layoutButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            FRLayout<Node, Edge> newLayout = new FRLayout<Node, Edge>(graph, vv.getSize());
            layout = newLayout;
            vv.getModel().setGraphLayout(layout);
        }
    });

    JButton circleButton = new JButton("circle layout");
    circleButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // layout = new CircleLayout<Node,Edge>(graph);
            CircleLayout<Node, Edge> newLayout = new CircleLayout<Node, Edge>(graph);
            layout = newLayout;
            vv.getModel().setGraphLayout(layout);
        }
    });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(layoutButton);
    controls.add(circleButton);
    add(controls, BorderLayout.SOUTH);
}

From source file:appletComponentArch.DynamicTreePanel.java

public DynamicTreePanel() {
    super(new BorderLayout());

    //Create the components.
    treePanel = new DynamicTree();
    populateTree(treePanel);/*from ww  w.  j a  v a2 s.com*/

    JButton addButton = new JButton("Add");
    addButton.setActionCommand(ADD_COMMAND);
    addButton.addActionListener(this);

    JButton removeButton = new JButton("Remove");
    removeButton.setActionCommand(REMOVE_COMMAND);
    removeButton.addActionListener(this);

    JButton clearButton = new JButton("Clear");
    clearButton.setActionCommand(CLEAR_COMMAND);
    clearButton.addActionListener(this);

    //Lay everything out.
    treePanel.setPreferredSize(new Dimension(300, 150));
    add(treePanel, BorderLayout.CENTER);

    JPanel panel = new JPanel(new GridLayout(0, 3));
    panel.add(addButton);
    panel.add(removeButton);
    panel.add(clearButton);
    add(panel, BorderLayout.SOUTH);
}

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

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

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

    // SERIALIZE - DESERIALIZE for testing purposes
    JFreeChart deserializedChart = null;

    try {
        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        final ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(chart);
        out.close();
        chart = null;
        dataset = null;
        this.series = null;
        System.gc();

        final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        deserializedChart = (JFreeChart) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    final TimeSeriesCollection c = (TimeSeriesCollection) deserializedChart.getXYPlot().getDataset();
    this.series = c.getSeries(0);
    // FINISHED TEST

    final ChartPanel chartPanel = new ChartPanel(deserializedChart);
    final JButton button = new JButton("Add New Data Item");
    button.setActionCommand("ADD_DATA");
    button.addActionListener(this);

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

}

From source file:com.ln.gui.Notifylist.java

@SuppressWarnings("unchecked")
public Notifylist() {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setResizable(false);// w w  w . j a va2s .c o  m
    setIconImage(Toolkit.getDefaultToolkit().getImage(Configuration.mydir + "\\resources\\icons\\ln6464.png"));
    setTitle("Event list");
    DateFormat dd = new SimpleDateFormat("dd");
    DateFormat dh = new SimpleDateFormat("HH");
    DateFormat dm = new SimpleDateFormat("mm");
    Date day = new Date();
    Date hour = new Date();
    Date minute = new Date();
    int dayd = Integer.parseInt(dd.format(day));
    int hourh = Integer.parseInt(dh.format(hour));
    int minutem = Integer.parseInt(dm.format(minute));

    int daydiff = dayd - Main.dayd;
    int hourdiff = hourh - Main.hourh;
    int mindiff = minutem - Main.minutem;

    model.clear();
    Events = new String[Main.events];
    Events2 = new String[Main.events];
    //      Events = Main.Eventlist;
    for (int i = 0; i != Main.events; i++) {
        Events[i] = Main.Eventlist[i];
    }
    for (int i = 0; i != Main.events; i++) {
        Events2[i] = Main.Eventlist[i];
    }
    for (int i = 0; i != Events2.length; i++) {
        if (Events2[i] != null) {
            Events2[i] = Main.Eventlist[i];
            Events2[i] = Events2[i].replace(StringUtils.substringBetween(Events2[i], "in: ", " Days"), Integer
                    .toString(Integer.parseInt(StringUtils.substringBetween(Events2[i], "in: ", " Days"))));
            Events2[i] = Events2[i].replace(StringUtils.substringBetween(Events2[i], "Days ", " Hours"), Integer
                    .toString(Integer.parseInt(StringUtils.substringBetween(Events2[i], "Days ", " Hours"))));
            Events2[i] = Events2[i].replace(StringUtils.substringBetween(Events2[i], "Hours ", " Minutes"),
                    Integer.toString(
                            Integer.parseInt(StringUtils.substringBetween(Events2[i], "Hours ", " Minutes"))));

        }
        if (Events[i] != null) {
            Events[i] = Main.Eventlist[i];
            Events[i] = Events[i].replace(StringUtils.substringBetween(Events[i], "in: ", " Days"),
                    Integer.toString(Integer.parseInt(StringUtils.substringBetween(Events[i], "in: ", " Days"))
                            - daydiff));
            Events[i] = Events[i].replace(StringUtils.substringBetween(Events[i], "Days ", " Hours"),
                    Integer.toString(
                            Integer.parseInt(StringUtils.substringBetween(Events[i], "Days ", " Hours"))
                                    - hourdiff));
            Events[i] = Events[i].replace(StringUtils.substringBetween(Events[i], "Hours ", " Minutes"),
                    Integer.toString(
                            Integer.parseInt(StringUtils.substringBetween(Events[i], "Hours ", " Minutes"))
                                    - mindiff));
            //Arrays.sort(Events);
            model.add(i, Events[i]);

        }
    }
    setBounds(100, 100, 671, 331);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    JButton Remove = new JButton("Remove selected");
    Remove.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (list.getSelectedIndices().length > 0) {
                int[] tmp = list.getSelectedIndices();
                Main.events = Main.events - tmp.length;
                int[] selectedIndices = list.getSelectedIndices();
                for (int i = tmp.length - 1; i >= 0; i--) {
                    selectedIndices = list.getSelectedIndices();
                    model.removeElementAt(selectedIndices[i]);
                    Events = ArrayUtils.remove(Events, selectedIndices[i]);
                    Events2 = ArrayUtils.remove(Events2, selectedIndices[i]);
                    Main.Eventlist = ArrayUtils.remove(Main.Eventlist, selectedIndices[i]);
                    //http://i.imgur.com/lN2Fe.jpg
                }
            }
        }
    });
    Remove.setBounds(382, 258, 130, 25);
    contentPane.add(Remove);
    JButton btnClose = new JButton("Close");
    btnClose.setBounds(522, 258, 130, 25);
    contentPane.add(btnClose);
    btnClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });
    try {
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 11, 642, 236);
        contentPane.add(scrollPane);
        list.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        list.setBounds(10, 11, 642, 46);
        scrollPane.setViewportView(list);

        scrollPane.getVerticalScrollBar().setValue(0);

    } catch (NullPointerException e) {
    }
}

From source file:com.att.aro.ui.view.menu.file.BPVideoWarnFailPanel.java

private JButton getDefaultButton(String text, ActionListener al) {
    JButton button = new JButton();
    button.setText(text);/*w ww. j  a  v a2  s .  c  om*/
    button.addActionListener(al);
    return button;
}

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

private JPanel createTab1Content() {
    JPanel jpanel = new JPanel(new BorderLayout());
    combo1 = new JComboBox();
    combo1.setActionCommand("combo1.changed");
    populateTextAnchorCombo(combo1);/*w w  w  .j av a  2  s .  c o  m*/
    combo1.addActionListener(this);
    JPanel jpanel1 = new JPanel();
    jpanel1.add(combo1);
    JButton jbutton = new JButton("Select Font...");
    jbutton.setActionCommand("fontButton.clicked");
    jbutton.addActionListener(this);
    jpanel1.add(jbutton);
    jpanel.add(jpanel1, "North");
    drawStringPanel1 = new DrawStringPanel("0123456789", false);
    jpanel.add(drawStringPanel1);
    return jpanel;
}

From source file:ColorPicker3.java

public ColorPicker3() {
    super("JColorChooser Test Frame");
    setSize(200, 100);/*from w  w  w  .j  a v  a2 s .  c  o m*/
    final JButton go = new JButton("Show JColorChoser");
    final Container contentPane = getContentPane();
    go.addActionListener(new ActionListener() {
        final JColorChooser chooser = new JColorChooser();

        boolean first = true;

        public void actionPerformed(ActionEvent e) {
            if (first) {
                first = false;
                GrayScalePanel gsp = new GrayScalePanel();
                chooser.addChooserPanel(gsp);
                chooser.setPreviewPanel(new CustomPane());
            }
            JDialog dialog = JColorChooser.createDialog(ColorPicker3.this, "Demo 3", true, chooser,
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            c = chooser.getColor();
                        }
                    }, null);
            dialog.setVisible(true);
            contentPane.setBackground(c);
        }
    });
    contentPane.add(go, BorderLayout.SOUTH);
    // addWindowListener(new BasicWindowMonitor()); // 1.1 & 1.2
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}