Java Code Examples for javax.swing.JTextField#requestFocus()

The following examples show how to use javax.swing.JTextField#requestFocus() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Creates a dialog to input a string field.
 *
 * @param freeColClient The {@code FreeColClient} for the game.
 * @param frame The owner frame.
 * @param modal True if this dialog should be modal.
 * @param tmpl A {@code StringTemplate} to explain the action to the user.
 * @param defaultValue The default value appearing in the text field.
 * @param okKey A key displayed on the "ok"-button.
 * @param cancelKey A key displayed on the optional "cancel"-button.
 */
public FreeColStringInputDialog(FreeColClient freeColClient, JFrame frame,
                                boolean modal, StringTemplate tmpl,
                                String defaultValue,
                                String okKey, String cancelKey) {
    super(freeColClient, frame);

    textField = new JTextField(defaultValue);
    textField.setOpaque(false);
    JPanel panel = new JPanel(new BorderLayout()) {
            @Override
            public void requestFocus() {
                textField.requestFocus();
            }
        };
    panel.setOpaque(false);

    panel.add(Utility.localizedTextArea(tmpl));
    panel.add(textField, BorderLayout.PAGE_END);

    initializeInputDialog(frame, modal, panel, null, okKey, cancelKey);
}
 
Example 2
/**
 * Method to check whether value is provided or not; and display message box in case it is not provided
 *
 * @param textField Text field to be checked for value
 * @param fieldName Name of the field
 * @return Flag whether value exists or not
 */
private boolean checkMandatory(JTextField textField, String fieldName) {
    if (!textField.isEnabled()) {
        return true;
    }

    String value = textField.getText();
    if (value == null || value.trim().equals("")) {
        JOptionPane.showMessageDialog(this, labelUtil.getString("gui.msg.err.mandatoryCheck", fieldName),
            labelUtil.getString("gui.msg.title.err"), JOptionPane.ERROR_MESSAGE);

        textField.requestFocus();
        return false;
    }

    return true;
}
 
Example 3
@Override
public void actionPerformed(ActionEvent e) {
    associationModel.addAlias("...");
    removeButton.setEnabled(true);
    aliasNameScrollPane.repaint();
    int rowIndex = 0;
    for (String aliasName : associationModel.getAliasNames()) {
        if (aliasName.equals("...")) {
            break;
        }
        rowIndex++;
    }
    DefaultCellEditor editor = (DefaultCellEditor) aliasNames.getCellEditor(rowIndex, 0);
    aliasNames.editCellAt(rowIndex, 0);
    final JTextField textField = (JTextField) editor.getComponent();
    textField.requestFocus();
    textField.selectAll();
}
 
Example 4
/**
 * Method to handle change event for 'randomImage'
 */
private void useRandomImgChanged() {
    JTextField coverFileTextField = this.stegoUI.getEmbedPanel().getCoverFileTextField();
    JButton coverFileButton = this.stegoUI.getEmbedPanel().getCoverFileButton();

    if (this.randomImgCheckBox.isSelected()) {
        CommonUtil.setEnabled(coverFileTextField, false);
        coverFileTextField.setText("");
        coverFileButton.setEnabled(false);
    } else {
        CommonUtil.setEnabled(coverFileTextField, true);
        coverFileButton.setEnabled(true);
        coverFileTextField.requestFocus();
    }
}
 
Example 5
Source Project: opensim-gui   File: MeasurementSetPanel.java    License: Apache License 2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent evt) { 
   measurementSetModel.addMeasurement(DEFAULT_MEASUREMENT_NAME); 

   // See comments in EditMeasurementNameActionAndFocusListener below...
   int lastIndex = measurementSetModel.getMeasurementSet().getSize()-1;
   JTextField textField = getMeasurementNameTextField(lastIndex);
   textField.requestFocus();
   textField.selectAll();
}
 
Example 6
Source Project: netbeans   File: NotifyDescriptor.java    License: Apache License 2.0 4 votes vote down vote up
/** Make a component representing the input line.
* @param text a label for the input line
* @return the component
*/
protected Component createDesign(final String text) {
    JPanel panel = new JPanel();
    panel.setOpaque (false);

    JLabel textLabel = new JLabel();
    Mnemonics.setLocalizedText(textLabel, text);

    boolean longText = text.length () > 80;
    textField = new JTextField(25);
    textLabel.setLabelFor(textField);
    
    textField.requestFocus();
    
    GroupLayout layout = new GroupLayout(panel);
    panel.setLayout(layout);
    if (longText) {
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(32, 32, 32))
                    .addComponent(textField))
                .addContainerGap())
        );
    } else {
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textLabel)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textField, GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                .addContainerGap())
        );
    }
    if (longText) {
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textLabel)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    } else {
        layout.setVerticalGroup(
                    layout.createParallelGroup(Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(textLabel)
                            .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );
    }

    javax.swing.KeyStroke enter = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0);
    javax.swing.text.Keymap map = textField.getKeymap();

    map.removeKeyStrokeBinding(enter);

    /*

          textField.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
    System.out.println("action: " + evt);
                InputLine.this.setValue (OK_OPTION);
              }
            }
          );
    */
    panel.getAccessibleContext().setAccessibleDescription(
        NbBundle.getMessage(NotifyDescriptor.class, "ACSD_InputPanel")
    );
    textField.getAccessibleContext().setAccessibleDescription(
        NbBundle.getMessage(NotifyDescriptor.class, "ACSD_InputField")
    );
    
    return panel;
}
 
Example 7
Source Project: netbeans   File: AddJspWatchAction.java    License: Apache License 2.0 4 votes vote down vote up
public void performAction () {
        ResourceBundle bundle = NbBundle.getBundle (AddJspWatchAction.class);

        JPanel panel = new JPanel();
        panel.getAccessibleContext ().setAccessibleDescription (bundle.getString ("ACSD_WatchPanel")); // NOI18N
        JTextField textField;
        JLabel textLabel = new JLabel (bundle.getString ("CTL_Watch_Name")); // NOI18N
        textLabel.setBorder (new EmptyBorder (0, 0, 0, 10));
        panel.setLayout (new BorderLayout ());
        panel.setBorder (new EmptyBorder (11, 12, 1, 11));
        panel.add ("West", textLabel); // NOI18N
        panel.add ("Center", textField = new JTextField (25)); // NOI18N
        textField.getAccessibleContext ().setAccessibleDescription (bundle.getString ("ACSD_CTL_Watch_Name")); // NOI18N
        textField.setBorder (
            new CompoundBorder (textField.getBorder (), 
            new EmptyBorder (2, 0, 2, 0))
        );
        textLabel.setDisplayedMnemonic (
            bundle.getString ("CTL_Watch_Name_Mnemonic").charAt (0) // NOI18N
        );


        String t = null;//Utils.getELIdentifier();
//        Utils.log("Watch: ELIdentifier = " + t);
        
        boolean isScriptlet = Utils.isScriptlet();
        Utils.log("Watch: isScriptlet: " + isScriptlet);
        
        if ((t == null) && (isScriptlet)) {
            t = Utils.getJavaIdentifier();
            Utils.log("Watch: javaIdentifier = " + t);
        }
        
        if (t != null) {
            textField.setText(t);
        } else {
            textField.setText(watchHistory);
        }
        textField.selectAll ();        
        textLabel.setLabelFor (textField);
        textField.requestFocus ();

        org.openide.DialogDescriptor dd = new org.openide.DialogDescriptor (
            panel, 
            bundle.getString ("CTL_Watch_Title") // NOI18N
        );
        dd.setHelpCtx (new HelpCtx ("debug.add.watch"));
        Dialog dialog = DialogDisplayer.getDefault ().createDialog (dd);
        dialog.setVisible(true);
        dialog.dispose ();

        if (dd.getValue() != org.openide.DialogDescriptor.OK_OPTION) return;
        String watch = textField.getText();
        if ((watch == null) || (watch.trim ().length () == 0)) {
            return;
        }
        
        String s = watch;
        int i = s.indexOf (';');
        while (i > 0) {
            String ss = s.substring (0, i).trim ();
            if (ss.length () > 0)
                DebuggerManager.getDebuggerManager ().createWatch (ss);
            s = s.substring (i + 1);
            i = s.indexOf (';');
        }
        s = s.trim ();
        if (s.length () > 0)
            DebuggerManager.getDebuggerManager ().createWatch (s);
        
        watchHistory = watch;
        
        // open watches view
//        new WatchesAction ().actionPerformed (null); TODO
    }
 
Example 8
/** Create the keyboard panel of the selected type.
    @param mode type of the keyboard to be employed.
*/
public OSKeybPanel(KEYBMODES mode)
{
    super();

    GridBagLayout bgl=new GridBagLayout();
    GridBagConstraints constraints=new GridBagConstraints();
    setLayout(bgl);
    constraints = DialogUtil.createConst(0,0,1,1,0,0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0,0,0,0));

    Font standardF = UIManager.getDefaults().getFont("TextPane.font");
    int size = standardF.getSize();
    Font f = new Font("Courier New",0,size+1);
    Font fbig = new Font("Courier New",0,size+2);

    ActionListener al = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            JDialog jd = (JDialog) txt;
            // We must find a target for the results of the keyboard
            // actions.
            if (!(jd.getMostRecentFocusOwner() instanceof JTextField))
                return;
            JTextField jfd = (JTextField)jd.getMostRecentFocusOwner();
            if (jfd.getSelectedText()!=null) {
                String ee = jfd.getText().replace(
                    jfd.getSelectedText(), "");
                jfd.setText(ee);
            }
            int p = jfd.getCaretPosition();
            if (p<0) {
                jfd.setText(jfd.getText()+e.getActionCommand());
            } else {
                String s = jfd.getText().substring(0,p);
                String t = jfd.getText().substring(p);
                jfd.setText(s+e.getActionCommand()+t);
                jfd.setCaretPosition(++p);
            }
            jfd.requestFocus();
        }
    };

    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 100;
    constraints.weighty = 100;

    // Create an array of buttons containing the array characters.
    // All is done automatically, so changing the array contents
    // automatically will change the buttons.
    for (int i=0;i<symbols.length();i++)
    {
        k[i] = new JButton(String.valueOf(symbols.charAt(i)));
        if (mode == KEYBMODES.GREEK && i>47)
            continue;
        if (mode == KEYBMODES.MISC && i<48)
            continue;

        k[i].setFont(i>71 ? fbig : f);
        k[i].setFocusable(false);
        k[i].addActionListener(al);
        k[i].putClientProperty("Quaqua.Button.style","toggleCenter");
        if (constraints.gridx>7) {
            k[i].putClientProperty("Quaqua.Button.style","toggleWest");
            constraints.gridy++;
            constraints.gridx=0;
            k[i-1].putClientProperty("Quaqua.Button.style","toggleEast");
        }

        add(k[i], constraints);
        constraints.gridx++;
    }

    // TODO: avoid using numbers in the code, but calculate automatically
    // the indices.
    k[0].putClientProperty("Quaqua.Button.style","toggleWest");
    k[symbols.length()-1].putClientProperty(
        "Quaqua.Button.style","toggleEast");
    k[47].putClientProperty("Quaqua.Button.style","toggleEast");
    k[48].putClientProperty(
        "Quaqua.Button.style","toggleWest");
}
 
Example 9
private void initGUI() {
	setLayout(new BorderLayout(0, 0));

	panelTags = new JPanel();
	FlowLayout flowLayout = (FlowLayout) panelTags.getLayout();
	flowLayout.setAlignment(FlowLayout.LEFT);
	add(panelTags, BorderLayout.CENTER);

	panelAdds = new JPanel();
	add(panelAdds, BorderLayout.EAST);
	panelAdds.setLayout(new BorderLayout(0, 0));

	btnAdd = new JButton();
	
	AbstractAction action = new AbstractAction("+") {
		private static final long serialVersionUID = 1L;
		@Override
		public void actionPerformed(ActionEvent ae) {
			JTextField field = new JTextField(10);
			panelTags.add(field);
			field.requestFocus();
			panelTags.revalidate();
			btnAdd.setEnabled(false);
			field.addActionListener(e -> {
				String s = field.getText();
				panelTags.remove(field);
				if (!s.equals(""))
					addTag(s);

				btnAdd.setEnabled(true);
				btnAdd.requestFocus();
			});

		}
	};

	btnAdd.setAction(action);

	btnAdd.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "ADD");
	btnAdd.getActionMap().put("ADD", action);
	panelAdds.add(btnAdd);
	componentFont = MTGControler.getInstance().getFont().deriveFont(Font.PLAIN, 15);

}
 
Example 10
Source Project: Spark   File: TelephoneTextField.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new IconTextField with Icon.
 */
public TelephoneTextField() {
    setLayout(new GridBagLayout());

    setBackground(new Color(212, 223, 237));

    pad = new PhonePad();

    textField = new JTextField();

    textField.setBorder(null);
    setBorder(new JTextField().getBorder());


    imageComponent = new JLabel(PhoneRes.getImageIcon("ICON_NUMBERPAD_IMAGE"));

    add(imageComponent, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 2, 0), 0, 0));
    add(textField, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 5, 2, 5), 0, 0));

    imageComponent.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            displayPad(e);
        }
    });

    textField.requestFocus();


    textField.setForeground((Color)UIManager.get("TextField.lightforeground"));
    textField.setText(textFieldText);

    textField.addFocusListener(this);
    textField.addMouseListener(this);
    textField.addKeyListener(this);
}