fubarable

re: How do I check the text in JTextField?

Oct 13th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Component;
  3. import java.awt.Dialog.ModalityType;
  4. import java.awt.Dimension;
  5. import java.awt.Point;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.KeyEvent;
  8. import java.beans.PropertyChangeEvent;
  9. import java.beans.PropertyChangeListener;
  10. import javax.swing.*;
  11.  
  12. /**
  13.  * In reference to: How do I check the text in JTextField?
  14.  * http://stackoverflow.com/questions/26044389/how-do-i-check-the-text-in-jtextfield
  15.  * @author Pete
  16.  *
  17.  */
  18. public class SubmitPanelTest {
  19.    private static void createAndShowGui() {
  20.       final SubmitPanel submitPanel = new SubmitPanel();
  21.       final SubmitExampleMainPanel mainPanel = new SubmitExampleMainPanel(
  22.             submitPanel);
  23.  
  24.       JFrame frame = new JFrame("Command");
  25.       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  26.       frame.getContentPane().add(mainPanel);
  27.       frame.pack();
  28.       frame.setLocationRelativeTo(null);
  29.       frame.setVisible(true);
  30.  
  31.       JDialog dialog = new JDialog(frame, "Submit Command",
  32.             ModalityType.MODELESS);
  33.       dialog.getContentPane().add(submitPanel);
  34.       dialog.pack();
  35.       dialog.setLocationRelativeTo(frame);
  36.       Point p = frame.getLocation();
  37.       int locX = p.x + 50;
  38.       int locY = p.y + 150;
  39.       dialog.setLocation(locX, locY);
  40.       dialog.setVisible(true);
  41.  
  42.       submitPanel.addPropertyChangeListener(new PropertyChangeListener() {
  43.  
  44.          @Override
  45.          public void propertyChange(PropertyChangeEvent evt) {
  46.             if (SubmitPanel.SUBMIT.equals(evt.getPropertyName())) {
  47.                Object submit = evt.getNewValue();
  48.                if (submit == null) {
  49.                   return;
  50.                }
  51.                System.out.println("Submit String = " + submit);
  52.             }
  53.          }
  54.       });
  55.    }
  56.  
  57.    public static void main(String[] args) {
  58.       SwingUtilities.invokeLater(new Runnable() {
  59.          public void run() {
  60.             createAndShowGui();
  61.          }
  62.       });
  63.    }
  64. }
  65.  
  66. @SuppressWarnings("serial")
  67. class SubmitExampleMainPanel extends JPanel {
  68.    private StatusBarPanel statusbar = new StatusBarPanel();
  69.  
  70.    public SubmitExampleMainPanel(SubmitPanel submitPanel) {
  71.       JPanel southPanel = new JPanel();
  72.       southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.LINE_AXIS));
  73.       southPanel.add(statusbar);
  74.       southPanel.add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
  75.  
  76.       setLayout(new BorderLayout());
  77.       add(southPanel, BorderLayout.PAGE_END);
  78.       add(Box.createRigidArea(new Dimension(400, 300)));
  79.  
  80.       submitPanel.addPropertyChangeListener(SubmitPanel.SUBMIT,
  81.             new SubmitListener());
  82.    }
  83.  
  84.    private class SubmitListener implements PropertyChangeListener {
  85.       @Override
  86.       public void propertyChange(PropertyChangeEvent pcEvt) {
  87.          Object newValue = pcEvt.getNewValue();
  88.          if (newValue != null) {
  89.             String command = newValue.toString();
  90.             statusbar.setCommandText(command);
  91.          }
  92.       }
  93.    }
  94. }
  95.  
  96. @SuppressWarnings("serial")
  97. class StatusBarPanel extends JPanel {
  98.    private static final int COLUMNS = 20;
  99.    private JTextField commandField = new JTextField(COLUMNS);
  100.  
  101.    public StatusBarPanel() {
  102.       commandField.setEditable(false);
  103.       commandField.setFocusable(false);
  104.       add(new JLabel("Current Command:"));
  105.       add(commandField);
  106.    }
  107.  
  108.    public void setCommandText(String commandText) {
  109.       commandField.setText(commandText);
  110.    }
  111. }
  112.  
  113. @SuppressWarnings("serial")
  114. class SubmitPanel extends JPanel {
  115.    public static final String SUBMIT = "submit";
  116.    private static final int COLUMNS = 40;
  117.    private String submit = "";
  118.    private JTextField submitField = new JTextField(COLUMNS);
  119.  
  120.    public SubmitPanel() {
  121.       SubmitAction submitAction = new SubmitAction("Submit", KeyEvent.VK_S);
  122.       submitField.addActionListener(submitAction);
  123.       add(submitField);
  124.       add(new JButton(submitAction));
  125.       add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
  126.    }
  127.  
  128.    private class SubmitAction extends AbstractAction {
  129.       public SubmitAction(String name, int mnemonic) {
  130.          super(name);
  131.          putValue(MNEMONIC_KEY, mnemonic);
  132.       }
  133.  
  134.       @Override
  135.       public void actionPerformed(ActionEvent e) {
  136.          setSubmit(submitField.getText());
  137.          submitField.selectAll();
  138.          submitField.requestFocusInWindow();
  139.       }
  140.    }
  141.  
  142.    public String getSubmit() {
  143.       return submit;
  144.    }
  145.  
  146.    public void setSubmit(String submit) {
  147.       String oldValue = this.submit;
  148.       String newValue = submit;
  149.       this.submit = newValue;
  150.       firePropertyChange(SUBMIT, oldValue, newValue);
  151.    }
  152. }
  153.  
  154. @SuppressWarnings("serial")
  155. class ExitAction extends AbstractAction {
  156.    public ExitAction(String name, int mnemonic) {
  157.       super(name);
  158.       putValue(MNEMONIC_KEY, mnemonic);
  159.    }
  160.  
  161.    @Override
  162.    public void actionPerformed(ActionEvent e) {
  163.       Component comp = (Component) e.getSource();
  164.       java.awt.Window win = SwingUtilities.getWindowAncestor(comp);
  165.       win.dispose();
  166.    }
  167. }
Add Comment
Please, Sign In to add comment