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:qic.ui.ManualPanel.java

@SuppressWarnings("serial")
public ManualPanel(Main main) {
    super(new BorderLayout(5, 5));

    table.setDoubleBuffered(true);/*from ww  w .ja v a 2  s .c o  m*/

    JTextField searchTf = new JTextField(100);
    JButton runBtn = new JButton("Run");
    runBtn.setPreferredSize(new Dimension(200, 10));
    JLabel invalidTermsLblLbl = new JLabel();
    invalidTermsLblLbl.setFont(invalidTermsLblLbl.getFont().deriveFont(Font.BOLD));
    JLabel invalidTermsLbl = new JLabel();
    invalidTermsLbl.setForeground(Color.RED);
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
    JLabel searchLbl = new JLabel(" Search: ");
    searchLbl.setFont(searchLbl.getFont().deriveFont(Font.BOLD));
    northPanel.add(searchLbl);
    northPanel.add(searchTf);
    northPanel.add(invalidTermsLblLbl);
    northPanel.add(invalidTermsLbl);
    northPanel.add(runBtn);
    this.add(northPanel, BorderLayout.NORTH);

    List<String> searchList = Util.loadSearchList(MANUAL_TXT_FILENAME);
    searchList.stream().forEach(searchJListModel::addElement);
    searchJList.setModel(searchJListModel);

    searchJList.addListSelectionListener(e -> {
        if (e.getValueIsAdjusting()) {
            searchTf.setText(trimToEmpty(searchJList.getSelectedValue()));
        }
    });
    searchJList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            if (evt.getClickCount() == 2) {
                int index = searchJList.locationToIndex(evt.getPoint());
                if (index != -1) {
                    String search = trimToEmpty(searchJListModel.getElementAt(index));
                    searchTf.setText(search);
                    runBtn.doClick();
                }
            }
        }
    });

    searchJList.getInputMap().put(KeyStroke.getKeyStroke("DELETE"), "doSomething");
    searchJList.getActionMap().put("doSomething", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int selectedIndex = searchJList.getSelectedIndex();
            if (selectedIndex != -1) {
                searchJListModel.remove(selectedIndex);
            }
        }
    });

    ActionListener runCommand = e -> {
        String tfText = searchTf.getText().trim();
        if (!tfText.isEmpty()) {
            Worker<Command> worker = new Worker<Command>(() -> {
                runBtn.setEnabled(false);
                Command result = null;
                try {
                    result = runQuery(main, tfText);
                } catch (Exception ex) {
                    runBtn.setEnabled(true);
                    SwingUtil.showError(ex);
                }
                return result;
            }, command -> {
                if (command != null) {
                    if (command.invalidSearchTerms.isEmpty()) {
                        addDataToTable(command);
                        saveSearchToList(tfText);
                        invalidTermsLbl.setText("");
                        invalidTermsLblLbl.setText("");
                        if (getBooleanProperty(MANUAL_AUTO_VERIFY, false)) {
                            long sleep = Config.getLongProperty(Config.MANUAL_AUTO_VERIFY_SLEEP, 5000);
                            table.runAutoVerify(sleep);
                        }
                    } else {
                        String invalidTermsStr = command.invalidSearchTerms.stream().collect(joining(", "));
                        invalidTermsLbl.setText(invalidTermsStr + " ");
                        invalidTermsLblLbl.setText(" Invalid: ");
                    }
                }
                runBtn.setEnabled(true);
            });
            worker.execute();
        }
    };

    searchTf.addActionListener(runCommand);
    runBtn.addActionListener(runCommand);

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(table),
            new JScrollPane(searchJList));

    this.add(splitPane, BorderLayout.CENTER);
}