Example usage for javax.swing JButton setActionCommand

List of usage examples for javax.swing JButton setActionCommand

Introduction

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

Prototype

public void setActionCommand(String actionCommand) 

Source Link

Document

Sets the action command for this button.

Usage

From source file:ruc.edu.window.DynamicDataDemo2.java

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

    super(title);
    this.series1 = new TimeSeries("Random 1");
    this.series2 = new TimeSeries("Random 2");
    final TimeSeriesCollection dataset1 = new TimeSeriesCollection(this.series1);
    final TimeSeriesCollection dataset2 = new TimeSeriesCollection(this.series2);
    final JFreeChart chart = ChartFactory.createTimeSeriesChart("Dynamic Data Demo 2", "Time", "Value",
            dataset1, true, true, false);
    chart.setBackgroundPaint(Color.white);

    final XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(Color.gray);
    plot.setRangeGridlinePaint(Color.gray);
    plot.setDomainPannable(true);
    //      plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 4, 4, 4, 4));
    final ValueAxis axis = plot.getDomainAxis();
    axis.setAutoRange(true);
    //axis.setAutoRangeMinimumSize(600000.0);
    axis.setFixedAutoRange(5000.0); // 60 seconds

    plot.setDataset(1, dataset2);
    //final NumberAxis rangeAxis2 = new NumberAxis("Range Axis 2");
    /// rangeAxis2.setAutoRangeIncludesZero(false);
    plot.setRenderer(0, new DefaultXYItemRenderer());
    plot.setRenderer(1, new DefaultXYItemRenderer());

    //plot.setRangeAxis(1, rangeAxis2);
    plot.mapDatasetToRangeAxis(1, 0);

    plot.getRenderer().setSeriesPaint(0, new Color(91, 155, 213));
    plot.getRenderer(1).setSeriesPaint(0, Color.BLUE);

    XYLineAndShapeRenderer render2 = new XYLineAndShapeRenderer() {
        Stroke soild = new BasicStroke(2.0f);
        Stroke dashed = new BasicStroke(10.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f,
                new float[] { 10.0f }, 0.0f);

        @Override
        public Stroke getItemStroke(int row, int column) {
            return dashed;
        }
    };

    plot.setRenderer(1, render2);

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

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

    final JButton button1 = new JButton("Add To Series 1");
    button1.setActionCommand("ADD_DATA_1");
    button1.addActionListener(this);

    final JButton button2 = new JButton("Add To Series 2");
    button2.setActionCommand("ADD_DATA_2");
    button2.addActionListener(this);

    final JButton button3 = new JButton("Add To Both");
    button3.setActionCommand("ADD_BOTH");
    button3.addActionListener(this);

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

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

}

From source file:DynamicTreeDemo.java

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

    // Create the components.
    treePanel = new DynamicTree();
    populateTree(treePanel);/*from w ww . j  a va 2  s . c  o  m*/

    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:components.ListDialog.java

private ListDialog(Frame frame, Component locationComp, String labelText, String title, Object[] data,
        String initialValue, String longValue) {
    super(frame, title, true);

    //Create and initialize the buttons.
    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(this);
    ///*from  w  w  w. j a v a 2 s  . c  o m*/
    final JButton setButton = new JButton("Set");
    setButton.setActionCommand("Set");
    setButton.addActionListener(this);
    getRootPane().setDefaultButton(setButton);

    //main part of the dialog
    list = new JList(data) {
        //Subclass JList to workaround bug 4832765, which can cause the
        //scroll pane to not let the user easily scroll up to the beginning
        //of the list.  An alternative would be to set the unitIncrement
        //of the JScrollBar to a fixed value. You wouldn't get the nice
        //aligned scrolling, but it should work.
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            int row;
            if (orientation == SwingConstants.VERTICAL && direction < 0
                    && (row = getFirstVisibleIndex()) != -1) {
                Rectangle r = getCellBounds(row, row);
                if ((r.y == visibleRect.y) && (row != 0)) {
                    Point loc = r.getLocation();
                    loc.y--;
                    int prevIndex = locationToIndex(loc);
                    Rectangle prevR = getCellBounds(prevIndex, prevIndex);

                    if (prevR == null || prevR.y >= r.y) {
                        return 0;
                    }
                    return prevR.height;
                }
            }
            return super.getScrollableUnitIncrement(visibleRect, orientation, direction);
        }
    };

    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    if (longValue != null) {
        list.setPrototypeCellValue(longValue); //get extra space
    }
    list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    list.setVisibleRowCount(-1);
    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                setButton.doClick(); //emulate button click
            }
        }
    });
    JScrollPane listScroller = new JScrollPane(list);
    listScroller.setPreferredSize(new Dimension(250, 80));
    listScroller.setAlignmentX(LEFT_ALIGNMENT);

    //Create a container so that we can add a title around
    //the scroll pane.  Can't add a title directly to the
    //scroll pane because its background would be white.
    //Lay out the label and scroll pane from top to bottom.
    JPanel listPane = new JPanel();
    listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
    JLabel label = new JLabel(labelText);
    label.setLabelFor(list);
    listPane.add(label);
    listPane.add(Box.createRigidArea(new Dimension(0, 5)));
    listPane.add(listScroller);
    listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    //Lay out the buttons from left to right.
    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);

    //Put everything together, using the content pane's BorderLayout.
    Container contentPane = getContentPane();
    contentPane.add(listPane, BorderLayout.CENTER);
    contentPane.add(buttonPane, BorderLayout.PAGE_END);

    //Initialize values.
    setValue(initialValue);
    pack();
    setLocationRelativeTo(locationComp);
}

From source file:StandardDialog.java

/**
 * Builds and returns the user interface for the dialog.  This method is
 * shared among the constructors.//  www  .ja  va  2 s.  co  m
 *
 * @return the button panel.
 */
protected JPanel createButtonPanel() {

    final L1R2ButtonPanel buttons = new L1R2ButtonPanel("Help", "OK", "Cancel");

    final JButton helpButton = buttons.getLeftButton();
    helpButton.setActionCommand("helpButton");
    helpButton.addActionListener(this);

    final JButton okButton = buttons.getRightButton1();
    okButton.setActionCommand("okButton");
    okButton.addActionListener(this);

    final JButton cancelButton = buttons.getRightButton2();
    cancelButton.setActionCommand("cancelButton");
    cancelButton.addActionListener(this);

    buttons.setBorder(BorderFactory.createEmptyBorder(4, 0, 0, 0));
    return buttons;
}

From source file:components.FrameDemo2.java

protected JComponent createButtonPane() {
    JButton button = new JButton("New window");
    button.setActionCommand(CREATE_WINDOW);
    button.addActionListener(this);
    defaultButton = button; //Used later to make this the frame's default button.

    //Center the button in a panel with some space around it.
    JPanel pane = new JPanel(); //use default FlowLayout
    pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    pane.add(button);/*  w ww .ja v  a 2 s .co m*/

    return pane;
}

From source file:components.ListDemo.java

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

    listModel = new DefaultListModel();
    listModel.addElement("Jane Doe");
    listModel.addElement("John Smith");
    listModel.addElement("Kathy Green");

    //Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);/*from   w w w  .j  av a 2  s  .c om*/
    list.addListSelectionListener(this);
    list.setVisibleRowCount(5);
    JScrollPane listScrollPane = new JScrollPane(list);

    JButton hireButton = new JButton(hireString);
    HireListener hireListener = new HireListener(hireButton);
    hireButton.setActionCommand(hireString);
    hireButton.addActionListener(hireListener);
    hireButton.setEnabled(false);

    fireButton = new JButton(fireString);
    fireButton.setActionCommand(fireString);
    fireButton.addActionListener(new FireListener());

    employeeName = new JTextField(10);
    employeeName.addActionListener(hireListener);
    employeeName.getDocument().addDocumentListener(hireListener);
    String name = listModel.getElementAt(list.getSelectedIndex()).toString();

    //Create a panel that uses BoxLayout.
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.add(fireButton);
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(employeeName);
    buttonPane.add(hireButton);
    buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    add(listScrollPane, BorderLayout.CENTER);
    add(buttonPane, BorderLayout.PAGE_END);
}

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

/**
 * Creates the content for tab 1.// w  w w.  j a v a  2  s.c o m
 *
 * @return The content panel.
 */
private JPanel createTab1Content() {
    final JPanel content = new JPanel(new BorderLayout());
    this.combo1 = new JComboBox();
    this.combo1.setActionCommand("combo1.changed");
    populateTextAnchorCombo(this.combo1);
    this.combo1.addActionListener(this);

    final JPanel controls = new JPanel();
    controls.add(this.combo1);

    final JButton fontButton = new JButton("Select Font...");
    fontButton.setActionCommand("fontButton.clicked");
    fontButton.addActionListener(this);
    controls.add(fontButton);
    content.add(controls, BorderLayout.NORTH);
    this.drawStringPanel1 = new DrawStringPanel("0123456789", false);
    content.add(this.drawStringPanel1);
    return content;
}

From source file:edu.harvard.mcz.imagecapture.SpecimenPartAttribEditDialog.java

private void init() {
    setTitle("Edit Part Attribute");
    setBounds(100, 100, 420, 200);//from www  .ja v  a 2  s .  c  o  m
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            okButton = new JButton("OK");
            okButton.setActionCommand("OK");
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    okButton.grabFocus();
                    if (comboBoxType.getSelectedIndex() > -1) {
                        targetAttribute.setAttributeType(comboBoxType.getSelectedItem().toString());
                    }
                    targetAttribute.setAttributeValue(comboBoxValue.getSelectedItem().toString());

                    targetAttribute.setAttributeUnits(textFieldUnits.getText());
                    targetAttribute.setAttributeRemark(textFieldRemarks.getText());

                    thisDialog.setVisible(false);
                }
            });
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);
        }
        {
            JButton cancelButton = new JButton("Cancel");
            cancelButton.setActionCommand("Cancel");
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    thisDialog.setVisible(false);
                }
            });
            buttonPane.add(cancelButton);
        }
    }
    {
        JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(new FormLayout(
                new ColumnSpec[] { FormSpecs.RELATED_GAP_COLSPEC, FormSpecs.DEFAULT_COLSPEC,
                        FormSpecs.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"), },
                new RowSpec[] { FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC,
                        FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC,
                        FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC,
                        FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, }));
        {
            JLabel lblAttributeType = new JLabel("Attribute Type");
            panel.add(lblAttributeType, "2, 2, right, default");
        }
        {
            comboBoxType = new JComboBox();
            comboBoxType.setModel(
                    new DefaultComboBoxModel(new String[] { "caste", "scientific name", "sex", "life stage" }));
            comboBoxType.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    String item = comboBoxType.getSelectedItem().toString();
                    if (item != null) {
                        configureComboBoxValue(item);
                    }
                }

            });
            panel.add(comboBoxType, "4, 2, fill, default");
        }
        {
            JLabel lblValue = new JLabel("Value");
            panel.add(lblValue, "2, 4, right, default");
        }
        {
            comboBoxValue = new JComboBox();
            comboBoxValue.setModel(new DefaultComboBoxModel(Caste.getCasteValues()));

            panel.add(comboBoxValue, "4, 4, fill, default");
        }
        {
            JLabel lblUnits = new JLabel("Units");
            panel.add(lblUnits, "2, 6, right, default");
        }
        {
            textFieldUnits = new JTextField();
            panel.add(textFieldUnits, "4, 6, fill, default");
            textFieldUnits.setColumns(10);
        }
        {
            JLabel lblRemarks = new JLabel("Remarks");
            panel.add(lblRemarks, "2, 8, right, default");
        }
        {
            textFieldRemarks = new JTextField();
            panel.add(textFieldRemarks, "4, 8, fill, default");
            textFieldRemarks.setColumns(10);
        }
    }
}

From source file:FrameDemo2.java

protected JComponent createButtonPane() {
    JButton button = new JButton("New window");
    button.setActionCommand(CREATE_WINDOW);
    button.addActionListener(this);
    defaultButton = button; // Used later to make this the frame's default
                            // button.

    // Center the button in a panel with some space around it.
    JPanel pane = new JPanel(); // use default FlowLayout
    pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    pane.add(button);/* w ww.j ava2s  . c o m*/

    return pane;
}

From source file:Main.java

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

    listModel = new DefaultListModel();
    listModel.addElement("Debbie Scott");
    listModel.addElement("Scott Hommel");
    listModel.addElement("Sharon Zakhour");

    // Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);/*from   w w w  .ja  v a 2  s . c o  m*/
    list.addListSelectionListener(this);
    list.setVisibleRowCount(5);
    JScrollPane listScrollPane = new JScrollPane(list);

    JButton hireButton = new JButton(hireString);
    HireListener hireListener = new HireListener(hireButton);
    hireButton.setActionCommand(hireString);
    hireButton.addActionListener(hireListener);
    hireButton.setEnabled(false);

    fireButton = new JButton(fireString);
    fireButton.setActionCommand(fireString);
    fireButton.addActionListener(new FireListener());

    employeeName = new JTextField(10);
    employeeName.addActionListener(hireListener);
    employeeName.getDocument().addDocumentListener(hireListener);
    String name = listModel.getElementAt(list.getSelectedIndex()).toString();

    // Create a panel that uses BoxLayout.
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.add(fireButton);
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(employeeName);
    buttonPane.add(hireButton);
    buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    add(listScrollPane, BorderLayout.CENTER);
    add(buttonPane, BorderLayout.PAGE_END);
}