Example usage for javax.swing JButton doClick

List of usage examples for javax.swing JButton doClick

Introduction

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

Prototype

public void doClick() 

Source Link

Document

Programmatically perform a "click".

Usage

From source file:Main.java

public static void main(final String args[]) {
    JButton button = new JButton("Test");

    button.addActionListener(new ClickListener());
    JOptionPane.showMessageDialog(null, button);

    button.doClick();
}

From source file:Main.java

public static final void main(String[] args) {
    JFrame frame = new JFrame();
    JTabbedPane tabbedPane = new JTabbedPane();

    frame.add(tabbedPane);//from   w ww. j  av  a2s. c  o  m

    JButton addButton = new JButton("Add tab");
    addButton.addActionListener(e -> {
        JPanel newTabComponent = new JPanel();
        int tabCount = tabbedPane.getTabCount();
        newTabComponent.add(new JLabel("I'm tab " + tabCount));
        tabbedPane.addTab("Tab " + tabCount, newTabComponent);
    });
    frame.add(addButton, BorderLayout.SOUTH);
    addButton.doClick();

    frame.setSize(800, 300);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Attaches a key event listener to given component, simulating a button click upon
 * pressing enter within the context./* w ww .  j  a v  a 2  s. c o m*/
 * 
 * @param context
 * @param button
 */
public static void simulateClickOnEnter(Component context, JButton button) {
    context.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER)
                button.doClick();
        }
    });
}

From source file:Main.java

public static void setCancelButton(final JRootPane rp, final JButton b) {
    rp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            "cancel");
    rp.getActionMap().put("cancel", new AbstractAction() {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent ev) {
            b.doClick();
        }/*from w  w w . j av a  2s. com*/
    });
}

From source file:Main.java

public static void setDontSaveButton(final JRootPane rp, final JButton b) {
    rp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "dontSave");
    rp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_D, rp.getToolkit().getMenuShortcutKeyMask()), "dontSave");
    rp.getActionMap().put("dontSave", new AbstractAction() {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent ev) {
            b.doClick();
        }//  www.  ja va  2 s.  c om
    });
}

From source file:king.flow.action.DefaultMsgSendAction.java

protected void showDoneMsg(final TLSResult result) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override/* w  w  w  .ja va2  s. co  m*/
        public void run() {
            if (doneDisplayList.size() == 1) {
                showOnComponent(getBlockMeta(doneDisplayList.get(0)), result);
            } else {
                try {
                    JSONParser jsonParser = new JSONParser();
                    Object element = jsonParser.parse(result.getOkMsg());
                    if (element instanceof JSONArray) {
                        JSONArray jsonArray = (JSONArray) element;
                        int len = Integer.min(doneDisplayList.size(), jsonArray.size());
                        for (int i = 0; i < len; i++) {
                            TLSResult freshResult = new TLSResult(result.getRetCode(),
                                    jsonArray.get(i).toString(), result.getErrMsg(), result.getPrtMsg());
                            showOnComponent(getBlockMeta(doneDisplayList.get(i)), freshResult);
                        }
                    } else {
                        showOnComponent(getBlockMeta(doneDisplayList.get(0)), result);
                    }
                } catch (Exception e) {
                    getLogger(DefaultMsgSendAction.class.getName()).log(Level.WARNING,
                            "Exception encounters during successful json value parsing : \n{0}", e);
                }
            }

            CommonUtil.cachePrintMsg(result.getPrtMsg());
            if (result.getRedirection() != null) {
                String redirection = result.getRedirection();
                getLogger(DefaultMsgSendAction.class.getName()).log(Level.INFO,
                        "Be forced to jump to page[{0}], ignore designated page[{1}]",
                        new Object[] { redirection, String.valueOf(next.getNextPanel()) });
                int forwardPage = 0;
                try {
                    forwardPage = Integer.parseInt(redirection);
                } catch (Exception e) {
                    panelJump(next.getNextPanel());
                    return;
                }
                Object blockMeta = getBlockMeta(forwardPage);
                if (blockMeta == null || !(blockMeta instanceof Panel)) {
                    getLogger(DefaultMsgSendAction.class.getName()).log(Level.INFO,
                            "Be forced to jump to page[{0}], but page[{0}] is invalid Panel type. It is type[{1}]",
                            new Object[] { redirection,
                                    (blockMeta == null ? "NULL" : blockMeta.getClass().getSimpleName()) });
                    panelJump(next.getNextPanel());
                    return;
                }
                panelJump(forwardPage);
            } else {
                panelJump(next.getNextPanel());

                //trigger the action denoted in sendMsgAction
                Integer trigger = next.getTrigger();
                if (trigger != null && getBlockMeta(trigger) != null) {
                    final Component blockMeta = (Component) getBlockMeta(trigger);
                    switch (blockMeta.getType()) {
                    case COMBO_BOX:
                        JComboBox comboBlock = getBlock(trigger, JComboBox.class);
                        ItemListener[] itemListeners = comboBlock.getItemListeners();
                        ItemEvent e = new ItemEvent(comboBlock, ItemEvent.ITEM_STATE_CHANGED,
                                comboBlock.getItemAt(comboBlock.getItemCount() - 1).toString(),
                                ItemEvent.SELECTED);
                        for (ItemListener itemListener : itemListeners) {
                            itemListener.itemStateChanged(e);//wait and hang on util progress dialog gets to dispose
                        }
                        if (comboBlock.isEditable()) {
                            String value = comboBlock.getEditor().getItem().toString();
                            if (value.length() == 0) {
                                return;
                            }
                        }
                        break;
                    case BUTTON:
                        JButton btnBlock = getBlock(trigger, JButton.class);
                        btnBlock.doClick();
                        break;
                    default:
                        getLogger(DefaultMsgSendAction.class.getName()).log(Level.WARNING,
                                "Invalid trigger component[{0}] as unsupported type[{1}]",
                                new Object[] { trigger, blockMeta.getType() });
                        break;
                    }
                } //trigger dealing 
                  //keep next cursor on correct component
                Integer nextCursor = next.getNextCursor();
                if (nextCursor != null && getBlockMeta(nextCursor) != null) {
                    JComponent block = getBlock(nextCursor, JComponent.class);
                    block.requestFocusInWindow();
                }
            } // no redirection branch
        }
    });
}

From source file:userinterface.graph.Histogram.java

/**
 * Generates the property dialog for a Histogram. Allows the user to select either a new or an exisitng Histogram
 * to plot data on/*from   w ww.j  a  v a  2  s . c om*/
 * 
 * @param defaultSeriesName
 * @param handler instance of {@link GUIGraphHandler}
 * @param minVal the min value in data cache
 * @param maxVal the max value in data cache
 * @return Either a new instance of a Histogram or an old one depending on what the user selects
 */

public static Pair<Histogram, SeriesKey> showPropertiesDialog(String defaultSeriesName, GUIGraphHandler handler,
        double minVal, double maxVal) {

    // make sure that the probabilities are valid
    if (maxVal > 1.0)
        maxVal = 1.0;
    if (minVal < 0.0)
        minVal = 0.0;

    // set properties for the dialog
    JDialog dialog = new JDialog(GUIPrism.getGUI(), "Histogram properties", true);
    dialog.setLayout(new BorderLayout());

    JPanel p1 = new JPanel(new FlowLayout());
    p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
            "Number of buckets"));

    JPanel p2 = new JPanel(new FlowLayout());
    p2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
            "Series name"));

    JSpinner buckets = new JSpinner(new SpinnerNumberModel(10, 5, Integer.MAX_VALUE, 1));
    buckets.setToolTipText("Select the number of buckets for this Histogram");

    // provides the ability to select a new or an old histogram to plot the series on
    JTextField seriesName = new JTextField(defaultSeriesName);
    JRadioButton newSeries = new JRadioButton("New Histogram");
    JRadioButton existing = new JRadioButton("Existing Histogram");
    newSeries.setSelected(true);
    JPanel seriesSelectPanel = new JPanel();
    seriesSelectPanel.setLayout(new BoxLayout(seriesSelectPanel, BoxLayout.Y_AXIS));
    JPanel seriesTypeSelect = new JPanel(new FlowLayout());
    JPanel seriesOptionsPanel = new JPanel(new FlowLayout());
    seriesTypeSelect.add(newSeries);
    seriesTypeSelect.add(existing);
    JComboBox<String> seriesOptions = new JComboBox<>();
    seriesOptionsPanel.add(seriesOptions);
    seriesSelectPanel.add(seriesTypeSelect);
    seriesSelectPanel.add(seriesOptionsPanel);
    seriesSelectPanel.setBorder(BorderFactory
            .createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Add series to"));

    // provides ability to select the min/max range of the plot
    JLabel minValsLabel = new JLabel("Min range:");
    JSpinner minVals = new JSpinner(new SpinnerNumberModel(0.0, 0.0, minVal, 0.01));
    minVals.setToolTipText("Does not allow value more than the min value in the probabilities");
    JLabel maxValsLabel = new JLabel("Max range:");
    JSpinner maxVals = new JSpinner(new SpinnerNumberModel(1.0, maxVal, 1.0, 0.01));
    maxVals.setToolTipText("Does not allow value less than the max value in the probabilities");
    JPanel minMaxPanel = new JPanel();
    minMaxPanel.setLayout(new BoxLayout(minMaxPanel, BoxLayout.X_AXIS));
    JPanel leftValsPanel = new JPanel(new BorderLayout());
    JPanel rightValsPanel = new JPanel(new BorderLayout());
    minMaxPanel.setBorder(
            BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Range"));
    leftValsPanel.add(minValsLabel, BorderLayout.WEST);
    leftValsPanel.add(minVals, BorderLayout.CENTER);
    rightValsPanel.add(maxValsLabel, BorderLayout.WEST);
    rightValsPanel.add(maxVals, BorderLayout.CENTER);
    minMaxPanel.add(leftValsPanel);
    minMaxPanel.add(rightValsPanel);

    // fill the old histograms in the property dialog

    boolean found = false;
    for (int i = 0; i < handler.getNumModels(); i++) {

        if (handler.getModel(i) instanceof Histogram) {

            seriesOptions.addItem(handler.getGraphName(i));
            found = true;
        }

    }

    existing.setEnabled(found);
    seriesOptions.setEnabled(false);

    // the bottom panel
    JPanel options = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JButton ok = new JButton("Plot");
    JButton cancel = new JButton("Cancel");

    // bind keyboard keys to plot and cancel buttons to improve usability

    ok.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "ok");
    ok.getActionMap().put("ok", new AbstractAction() {

        private static final long serialVersionUID = -7324877661936685228L;

        @Override
        public void actionPerformed(ActionEvent e) {

            ok.doClick();

        }
    });

    cancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            "ok");
    cancel.getActionMap().put("ok", new AbstractAction() {

        private static final long serialVersionUID = 2642213543774356676L;

        @Override
        public void actionPerformed(ActionEvent e) {

            cancel.doClick();

        }
    });

    //Action listener for the new series radio button
    newSeries.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (newSeries.isSelected()) {

                existing.setSelected(false);
                seriesOptions.setEnabled(false);
                buckets.setEnabled(true);
                buckets.setToolTipText("Select the number of buckets for this Histogram");
                minVals.setEnabled(true);
                maxVals.setEnabled(true);
            }
        }
    });

    //Action listener for the existing series radio button
    existing.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            if (existing.isSelected()) {

                newSeries.setSelected(false);
                seriesOptions.setEnabled(true);
                buckets.setEnabled(false);
                minVals.setEnabled(false);
                maxVals.setEnabled(false);
                buckets.setToolTipText("Number of buckets can't be changed on an existing Histogram");
            }

        }
    });

    //Action listener for the plot button
    ok.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            dialog.dispose();

            if (newSeries.isSelected()) {

                hist = new Histogram();
                hist.setNumOfBuckets((int) buckets.getValue());
                hist.setIsNew(true);

            } else if (existing.isSelected()) {

                String HistName = (String) seriesOptions.getSelectedItem();
                hist = (Histogram) handler.getModel(HistName);
                hist.setIsNew(false);

            }

            key = hist.addSeries(seriesName.getText());

            if (minVals.isEnabled() && maxVals.isEnabled()) {

                hist.setMinProb((double) minVals.getValue());
                hist.setMaxProb((double) maxVals.getValue());

            }
        }
    });

    //Action listener for the cancel button
    cancel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
            hist = null;
        }
    });

    dialog.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosed(WindowEvent e) {

            hist = null;
        }
    });

    p1.add(buckets, BorderLayout.CENTER);

    p2.add(seriesName, BorderLayout.CENTER);

    options.add(ok);
    options.add(cancel);

    // add everything to the main panel of the dialog
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    mainPanel.add(seriesSelectPanel);
    mainPanel.add(p1);
    mainPanel.add(p2);
    mainPanel.add(minMaxPanel);

    // add main panel to the dialog
    dialog.add(mainPanel, BorderLayout.CENTER);
    dialog.add(options, BorderLayout.SOUTH);

    // set dialog properties
    dialog.setSize(320, 290);
    dialog.setLocationRelativeTo(GUIPrism.getGUI());
    dialog.setVisible(true);

    // return the user selected Histogram with the properties set
    return new Pair<Histogram, SeriesKey>(hist, key);
}

From source file:techtonic.Onview.java

private void loadwellBtnPanel(String op) {
    // get the particular well using its name
    for (int x = 0; x < wells.size(); x++) {
        if (op.equals(wells.get(x).getName())) {
            currentWell = wells.get(x);//w  w w  .  j  a  va2s  .  co m
            break;
        }
    }
    // load the well panel with well on a button
    List<WitsmlWellbore> wellbores = xmlreader.getWellbores(currentWell);
    btnWellPanel.removeAll();
    for (int i = 0; i < wellbores.size(); i++) {
        WitsmlWellbore getWellBore = wellbores.get(i);

        JButton btn = new JButton("<html><b>Name:  " + getWellBore.getName() + "</b><br/> Status:  "
                + getWellBore.getStatus() + "<br> Type:  " + getWellBore.getType() + "</br></html>");
        btn.setBounds(5, 5, btnWellPanel.getWidth() - 10, 80);
        btn.setBounds(5, ((100 * i + 5)), btnWellPanel.getWidth() - 20, 100);
        btnWellPanel.add(btn);
        // add Listener to the button            
        btn.addActionListener(new WellBoreListenerOnView(i, getWellBore, displayAreaPanel1, jcbX_Axis,
                jcbY_Axis, trajectoryPanel, logsPanel));
        repaint();
    }
    JButton b = (JButton) btnWellPanel.getComponent(0);
    b.doClick();
    wellCombo.setEnabled(true);
    exportBtn.setEnabled(true);
    saveBtn.setEnabled(true);
    maxBtn.setEnabled(true);
}

From source file:edu.ku.brc.specify.tasks.subpane.ESResultsTablePanel.java

/**
 * @param e/*w w  w  . j  a va  2 s .c  o  m*/
 */
protected void doDoubleClickOnRow(final MouseEvent e) {
    if (e.getClickCount() == 2 && e.getButton() == 1) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                if (propChangeListener != null) {
                    propChangeListener.propertyChange(new PropertyChangeEvent(this, "doubleClick", 2, 0));
                }

                if (serviceBtns != null) {
                    for (ServiceInfo si : serviceBtns.keySet()) {
                        if (si.isDefault()) {
                            final JButton defBtn = serviceBtns.get(si);
                            if (defBtn != null) {
                                SwingUtilities.invokeLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        defBtn.doClick();
                                    }
                                });
                                break;
                            }
                        }
                    }
                }
            }
        });
    }
}

From source file:net.minelord.gui.panes.IRCPane.java

public IRCPane() {
    super();//  w w w .  ja  v  a  2  s.co m
    this.setBorder(new EmptyBorder(5, 5, 5, 5));
    this.setLayout(null);
    nickSelectPane = new JPanel();
    nickSelectPane.setLayout(null);
    nickSelectPane.setBounds(325, 70, 200, 200);
    TitledBorder title = BorderFactory
            .createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Pick a nick");
    title.setTitleJustification(TitledBorder.RIGHT);
    nickSelectPane.setBorder(title);
    nickSelect = new JTextField();
    final JButton done = new JButton("Done");
    nickSelect.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent arg0) {
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
            String nick = nickSelect.getText();
            boolean success = true;
            if (nick.length() > 0) {
                if (nick.substring(0, 1).matches("[0-9]") || nick.charAt(0) == '-' || nick.contains(" "))
                    success = false;
            } else
                success = false;
            if (success)
                IRCPane.nick = nick;
            done.setEnabled(success);
        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            if (arg0.getKeyCode() == 10)
                done.doClick();
        }
    });
    nickSelect.setBounds(50, 65, 100, 30);
    done.setBounds(50, 125, 100, 30);
    done.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            remove(nickSelectPane);
            repaint();
            startClient(nickSelect.getText());
        }
    });

    done.setEnabled(false);
    nickSelectPane.add(nickSelect);
    nickSelectPane.add(done);
    add(nickSelectPane);
    addFocusListener(new FocusListener() {
        @Override
        public void focusLost(FocusEvent paramFocusEvent) {
        }

        @Override
        public void focusGained(FocusEvent paramFocusEvent) {
            if (nickSelectPane.getParent() != null)
                nickSelect.requestFocus();
            if (input != null && input.getParent() != null)
                input.requestFocus();
        }
    });
    instance = this;
}