Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>" Is it as simple as writing:</p> <pre><code>if (e.getSource()=='querybutton' || e.getSource=='enter')" </code></pre> <p>It's not simple to write this, but rather it is wrong to write it.</p> <p>For one you don't want to compare Strings with ==, for another, you don't declare Strings with single quotes, and for a third, the enter key is not obtained in this way, but rather by adding the appropriate ActionListener object to the JTextField itself, and finally there should be in a single ActionListener class that handles this action, so the if block is completely unnecessary. This can probably be best done with a small inner private ActionListener class. You'd then create one object of this class and add it as an ActionListener for the querybutton and for the JTextField.</p> <p>edit 1:</p> <p>A more complete example of what I mean is shown below, a demo class that has a private inner handler class:</p> <pre><code>import java.awt.event.*; import javax.swing.*; public class ActionListenerEg extends JPanel { private JButton queryButton = new JButton("Query"); private JTextField textField = new JTextField("hello", 20); public ActionListenerEg() { QueryListener qListener = new QueryListener(); queryButton.addActionListener(qListener); textField.addActionListener(qListener); add(queryButton); add(textField); } private class QueryListener implements ActionListener { public void actionPerformed(ActionEvent arg0) { String textInField = textField.getText(); System.out.println("Use text in field, \"" + textInField + "\" to call SQL query in a background SwingWorker thread."); } } private static void createAndShowUI() { JFrame frame = new JFrame("ActionListenerEg"); frame.getContentPane().add(new ActionListenerEg()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } </code></pre> <p>The ActionListener is fired either by pressing the button or by pressing enter from within the JTextField. I'd then have in my control class, code that is called inside of the actinoPerformed method.</p> <p>edit 2: Having most handler or "control" code in its own Handler or Control class can be a good idea, but it doesn't have to implement ActionListener interface itself, but rather just have the code that will be called from within the ActionListener codes. For example, here I try to put all the handler code in its own class. It will have different methods that are called for various situations. e.g.,</p> <pre><code>import java.awt.Component; import java.awt.event.*; import javax.swing.*; public class ActionListenerEg extends JPanel { private ActionListenerHandler handler; private JButton queryButton = new JButton("Query"); private JButton displayButton = new JButton("Display"); private JTextField textField = new JTextField("hello", 20); // pass in handler or handler public ActionListenerEg(final ActionListenerHandler handler) { this.handler = handler; QueryListener qListener = new QueryListener(); queryButton.addActionListener(qListener); textField.addActionListener(qListener); displayButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (handler != null) { handler.displayActionPerformed(e); } } }); add(queryButton); add(textField); add(displayButton); } private class QueryListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (handler != null) { String textInField = textField.getText(); handler.doQueryAction(e, textInField); } } } private static void createAndShowUI() { ActionListenerHandler handler = new ActionListenerHandler(); JFrame frame = new JFrame("ActionListenerEg"); frame.getContentPane().add(new ActionListenerEg(handler)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class ActionListenerHandler { public void displayActionPerformed(ActionEvent e) { JOptionPane.showMessageDialog((Component) e.getSource(), "Display things!"); } public void doQueryAction(ActionEvent e, String textInField) { String text = "We will use \"" + textInField + "\" to help create and run the SQL Query"; JOptionPane.showMessageDialog((Component) e.getSource(), text); } } </code></pre> <p>Please ask questions if it's clear as mudd, or if anything is wrong.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload