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

The following examples show how to use javax.swing.JTextField#setCaretPosition() . 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
private static void populateTextField(Attribute[] attrs, JTextField textField, ASN1ObjectIdentifier pkcs9Attr) {
	if (attrs != null) {
		for (Attribute attribute : attrs) {

			ASN1ObjectIdentifier attributeOid = attribute.getAttrType();

			if (attributeOid.equals(pkcs9Attr)) {
				ASN1Encodable challenge = attribute.getAttributeValues()[0];

				// data type can be one of IA5String or UTF8String
				if (challenge instanceof DERPrintableString) {
					textField.setText(((DERPrintableString) challenge).getString());
				} else if (challenge instanceof DERUTF8String) {
					textField.setText(((DERUTF8String) challenge).getString());
				}
				textField.setCaretPosition(0);
			}
		}
	}
}
 
Example 2
private
void
processKey(int key)
{
  if(isValidKey(key) == true)
  {
    findFirstMatchAndComplete();
  }
  else if(key == VK_ENTER)
  {
    JTextField field = getTextField();

    // Remove any highlighting.
    field.setCaretPosition(field.getText().length());
  }
}
 
Example 3
Source Project: netbeans   File: ValueResultItemTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testValueReplacement() throws Exception {
    JTextField textField = new JTextField();
    BaseDocument doc = getDocument("/org/netbeans/modules/xml/text/completion/res/ValueReplacement.xml");
    BaseDocument referenceDoc = getDocument("/org/netbeans/modules/xml/text/completion/res/ValueReplacement.golden.xml");
    Finder ip1Finder = new FinderFactory.StringFwdFinder("##IP1##", true);
    int insertPos1 = doc.find(ip1Finder, doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset());
    doc.replace(insertPos1, 7, "", null);
    textField.setDocument(doc);
    MockGrammarResult mgr = new MockGrammarResult("Middle", "Mid");
    ValueResultItem vri1 = new ValueResultItem(insertPos1, mgr, 0, null);
    textField.setCaretPosition(insertPos1);
    vri1.defaultAction(textField);

    Finder ip2Finder = new FinderFactory.StringFwdFinder("##IP2##", true);
    int insertPos2 = doc.find(ip2Finder, doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset());
    doc.replace(insertPos2, 7, "", null);
    ValueResultItem vri2 = new ValueResultItem(insertPos2, mgr, 7, null);
    textField.setCaretPosition(insertPos2);
    vri2.defaultAction(textField);

    MockGrammarResult mgr2 = new MockGrammarResult("Middle", "");
    Finder ip3Finder = new FinderFactory.StringFwdFinder("##IP3##", true);
    int insertPos3 = doc.find(ip3Finder, doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset());
    doc.replace(insertPos3, 7, "", null);
    ValueResultItem vri3 = new ValueResultItem(insertPos3, mgr2, 7, null);
    textField.setCaretPosition(insertPos3);
    vri3.defaultAction(textField);

    assertTrue("Result did not match reference", compare(referenceDoc, doc));
}
 
Example 4
Source Project: portecle   File: DGetAlias.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize the dialog's GUI components.
 *
 * @param sOldAlias The alias to display initially
 * @param select Whether to pre-select the initially displayed alias
 */
private void initComponents(String sOldAlias, boolean select)
{
	getContentPane().setLayout(new BorderLayout());

	JLabel jlAlias = new JLabel(RB.getString("DGetAlias.jlAlias.text"));
	m_jtfAlias = new JTextField(sOldAlias, 15);
	m_jtfAlias.setCaretPosition(sOldAlias.length());
	jlAlias.setLabelFor(m_jtfAlias);

	JButton jbOK = getOkButton(false);
	JButton jbCancel = getCancelButton();

	JPanel jpAlias = new JPanel(new FlowLayout(FlowLayout.CENTER));
	jpAlias.add(jlAlias);
	jpAlias.add(m_jtfAlias);
	jpAlias.setBorder(new EmptyBorder(5, 5, 5, 5));

	JPanel jpButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));
	jpButtons.add(jbOK);
	jpButtons.add(jbCancel);

	getContentPane().add(jpAlias, BorderLayout.CENTER);
	getContentPane().add(jpButtons, BorderLayout.SOUTH);

	getRootPane().setDefaultButton(jbOK);

	initDialog();

	if (select)
	{
		SwingHelper.selectAndFocus(m_jtfAlias);
	}
}
 
Example 5
Source Project: binnavi   File: CLogFilePanel.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new panel object.
 */
public CLogFilePanel() {
  super(new BorderLayout());

  final JTextField fileLabel = new JTextField(ConfigHelper.getConfigurationDirectory(
      Constants.COMPANY_NAME, Constants.PROJECT_NAME));
  fileLabel.setEditable(false);
  fileLabel.setCaretPosition(0);
  add(fileLabel);
  final JButton button = new JButton(new COpenAction());
  add(button, BorderLayout.EAST);
}
 
Example 6
@Override
public void focusLost(FocusEvent e) {
	JTextField jTextField = (JTextField) e.getSource();
	try {
		parse(jTextField.getText());
		jTextField.setBackground(Color.WHITE);
	} catch (ParseException ex) {
		ColorSettings.config(jTextField, ColorEntry.GLOBAL_ENTRY_INVALID);
	}
	jTextField.setCaretPosition(jTextField.getText().length());
}
 
Example 7
Source Project: ET_Redux   File: DialogEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param textF
 * @param editable
 */
public BigDecimalDocument(JTextField textF, boolean editable) {
    super(textF, editable);
    this.editable = editable;
    textF.setFont(myScienceFont);
    textF.setCaretPosition(0);
}
 
Example 8
Source Project: pumpernickel   File: LengthSpinnerEditor.java    License: MIT License 4 votes vote down vote up
public void actionPerformed(ActionEvent e) {
	JTextField f = (JTextField) e.getSource();
	f.setCaretPosition(0);
}
 
Example 9
/** 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 10
private void initAuthorityCertificatesTab() {

		jlCaCertificatesFile = new JLabel(res.getString("DPreferences.jlCaCertificatesFile.text"));
		jtfCaCertificatesFile = new JTextField(caCertificatesFile.toString(), 25);
		jtfCaCertificatesFile.setToolTipText(res.getString("DPreferences.jtfCaCertificatesFile.tooltip"));
		jtfCaCertificatesFile.setCaretPosition(0);
		jtfCaCertificatesFile.setEditable(false);

		jbBrowseCaCertificatesFile = new JButton(res.getString("DPreferences.jbBrowseCaCertificatesFile.text"));
		PlatformUtil.setMnemonic(jbBrowseCaCertificatesFile,
				res.getString("DPreferences.jbBrowseCaCertificatesFile.mnemonic").charAt(0));
		jbBrowseCaCertificatesFile.setToolTipText(res.getString("DPreferences.jbBrowseCaCertificatesFile.tooltip"));

		jcbUseCaCertificates = new JCheckBox(res.getString("DPreferences.jcbUseCaCertificates.text"), useCaCertificates);
		jcbUseCaCertificates.setToolTipText(res.getString("DPreferences.jcbUseCaCertificates.tooltip"));
		PlatformUtil.setMnemonic(jcbUseCaCertificates, res.getString("DPreferences.jcbUseCaCertificates.mnemonic")
				.charAt(0));

		jcbUseWinTrustedRootCertificates = new JCheckBox(
				res.getString("DPreferences.jcbUseWinTrustRootCertificates.text"), useWinTrustRootCertificates);
		jcbUseWinTrustedRootCertificates.setToolTipText(res
				.getString("DPreferences.jcbUseWinTrustRootCertificates.tooltip"));
		PlatformUtil.setMnemonic(jcbUseWinTrustedRootCertificates,
				res.getString("DPreferences.jcbUseWinTrustRootCertificates.menmonic").charAt(0));


		jlTrustChecks = new JLabel(res.getString("DPreferences.jlTrustChecks.text"));

		jcbEnableImportTrustedCertTrustCheck = new JCheckBox(
				res.getString("DPreferences.jcbEnableImportTrustedCertTrustCheck.text"),
				enableImportTrustedCertTrustCheck);
		jcbEnableImportTrustedCertTrustCheck.setToolTipText(res
				.getString("DPreferences.jcbEnableImportTrustedCertTrustCheck.tooltip"));
		jcbEnableImportTrustedCertTrustCheck.setMnemonic(res.getString(
				"DPreferences.jcbEnableImportTrustedCertTrustCheck.mnemonic").charAt(0));

		jcbEnableImportCaReplyTrustCheck = new JCheckBox(
				res.getString("DPreferences.jcbEnableImportCaReplyTrustCheck.text"), enableImportCaReplyTrustCheck);
		jcbEnableImportCaReplyTrustCheck.setToolTipText(res
				.getString("DPreferences.jcbEnableImportCaReplyTrustCheck.tooltip"));
		jcbEnableImportCaReplyTrustCheck.setMnemonic(res.getString(
				"DPreferences.jcbEnableImportCaReplyTrustCheck.mnemonic").charAt(0));

		// layout
		jpAuthorityCertificates = new JPanel();
		jpAuthorityCertificates.setLayout(new MigLayout("insets dialog", "20lp[][]", "20lp[][]"));
		jpAuthorityCertificates.add(jlCaCertificatesFile, "split");
		jpAuthorityCertificates.add(jtfCaCertificatesFile, "");
		jpAuthorityCertificates.add(jbBrowseCaCertificatesFile, "wrap rel");
		if (Security.getProvider(SecurityProvider.MS_CAPI.jce()) != null) {
			jpAuthorityCertificates.add(jcbUseCaCertificates, "wrap rel");
			jpAuthorityCertificates.add(jcbUseWinTrustedRootCertificates, "wrap para");
		} else {
			jpAuthorityCertificates.add(jcbUseCaCertificates, "wrap para");
		}
		jpAuthorityCertificates.add(jlTrustChecks, "wrap unrel");
		jpAuthorityCertificates.add(jcbEnableImportTrustedCertTrustCheck, "wrap rel");
		jpAuthorityCertificates.add(jcbEnableImportCaReplyTrustCheck, "wrap unrel");


		jbBrowseCaCertificatesFile.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				try {
					CursorUtil.setCursorBusy(DPreferences.this);
					browsePressed();
				} finally {
					CursorUtil.setCursorFree(DPreferences.this);
				}
			}
		});
	}
 
Example 11
public void initializeUI() {
		uiDone = true;
		
		// Create general label panel.
		JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		topContentPanel.add(labelPanel);

		// Prepare general label
		JLabel titleLabel = new JLabel(Msg.getString("TabPanelSponsorship.label"), JLabel.CENTER); //$NON-NLS-1$
		titleLabel.setFont(new Font("Serif", Font.BOLD, 16));
		labelPanel.add(titleLabel);

		// Prepare spring layout info panel.
		JPanel infoPanel = new JPanel(new SpringLayout());//GridLayout(2, 2, 0, 0));
//		infoPanel.setBorder(new MarsPanelBorder());
		centerContentPanel.add(infoPanel, BorderLayout.NORTH);

		// Prepare sponsor name label
		JLabel sponsorNameLabel = new JLabel(Msg.getString("TabPanelSponsorship.sponsor"), JLabel.RIGHT); //$NON-NLS-1$
		//sponsorNameLabel.setSize(2, 2);
		infoPanel.add(sponsorNameLabel);

		// Prepare sponsor label
		JTextField sponsorTF = new JTextField();
		ReportingAuthorityType sponsor = null;
		if (settlement.getReportingAuthority() != null) {
		    sponsor = settlement.getReportingAuthority().getOrg();
		    sponsorTF.setText(sponsor+""); // Conversion.capitalize(sponsor)
		}
		sponsorTF.setEditable(false);
		sponsorTF.setColumns(8);
		sponsorTF.setCaretPosition(0);
		if (settlement.getReportingAuthority() != null) {
			TooltipManager.setTooltip(sponsorTF, settlement.getReportingAuthority().getToolTipStr(), TooltipWay.down);
		}
		//JLabel sponsorLabel = new JLabel(sponsor, JLabel.RIGHT);
		infoPanel.add(sponsorTF);


		// Prepare birth location name label
		JLabel objectiveNameLabel = new JLabel(Msg.getString("TabPanelSponsorship.objective"), JLabel.RIGHT); //$NON-NLS-1$
		//objectiveNameLabel.setSize(2, 2);
		infoPanel.add(objectiveNameLabel);

		// Prepare birth location label
		String objective = null;
		JTextField objectiveTF = new JTextField();
		if (settlement.getReportingAuthority() != null) {
			objective = settlement.getReportingAuthority().getMissionAgenda().getObjectiveName();
		}
		//JLabel objectiveLabel = new JLabel(objective, JLabel.RIGHT);
		objectiveTF.setText(Conversion.capitalize(objective));
		objectiveTF.setEditable(false);
		objectiveTF.setColumns(16);
		objectiveTF.setCaretPosition(0);
		infoPanel.add(objectiveTF);

		//Lay out the spring panel.
		SpringUtilities.makeCompactGrid(infoPanel,
		                                2, 2, //rows, cols
		                                20, 10,        //initX, initY
		                                10, 10);       //xPad, yPad
	}
 
Example 12
public void initializeUI() {
		uiDone = true;
		
		// Create general label panel.
		JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		topContentPanel.add(labelPanel);

		// Prepare general label
		JLabel titleLabel = new JLabel(Msg.getString("TabPanelSponsorship.label"), JLabel.CENTER); //$NON-NLS-1$
		titleLabel.setFont(new Font("Serif", Font.BOLD, 16));
		labelPanel.add(titleLabel);

		// Prepare spring layout info panel.
		JPanel infoPanel = new JPanel(new SpringLayout());//GridLayout(2, 2, 0, 0));
//		infoPanel.setBorder(new MarsPanelBorder());
		centerContentPanel.add(infoPanel, BorderLayout.NORTH);

		// Prepare sponsor name label
		JLabel sponsorNameLabel = new JLabel(Msg.getString("TabPanelSponsorship.sponsor"), JLabel.RIGHT); //$NON-NLS-1$
		//sponsorNameLabel.setSize(2, 2);
		infoPanel.add(sponsorNameLabel);

		// Prepare sponsor label
		JTextField sponsorTF = new JTextField();
		ReportingAuthorityType sponsor = null;
		if (person.getReportingAuthority() != null) {
		    sponsor = person.getReportingAuthority().getOrg();
		    sponsorTF.setText(sponsor+""); // Conversion.capitalize(sponsor)
		}
		sponsorTF.setEditable(false);
		sponsorTF.setColumns(8);
		sponsorTF.setCaretPosition(0);
		if (person.getReportingAuthority() != null) {
			TooltipManager.setTooltip (sponsorTF, person.getReportingAuthority().getToolTipStr(), TooltipWay.down);
		}
		infoPanel.add(sponsorTF);


		// Prepare birth location name label
		JLabel objectiveNameLabel = new JLabel(Msg.getString("TabPanelSponsorship.objective"), JLabel.RIGHT); //$NON-NLS-1$
		//objectiveNameLabel.setSize(2, 2);
		infoPanel.add(objectiveNameLabel);

		// Prepare birth location label
		String objective = null;
		JTextField objectiveTF = new JTextField();
		if (person.getReportingAuthority() != null) {
			objective = person.getReportingAuthority().getMissionAgenda().getObjectiveName();
		}
		//JLabel objectiveLabel = new JLabel(objective, JLabel.RIGHT);
		objectiveTF.setText(Conversion.capitalize(objective));
		objectiveTF.setEditable(false);
		objectiveTF.setColumns(16);
		objectiveTF.setCaretPosition(0);
		infoPanel.add(objectiveTF);

		//Lay out the spring panel.
		SpringUtilities.makeCompactGrid(infoPanel,
		                                2, 2, //rows, cols
		                                20, 10,        //initX, initY
		                                10, 10);       //xPad, yPad
	}
 
Example 13
private
void
findFirstMatchAndComplete()
{
  JTextField field = getTextField();
  String text = field.getText();

  // Only iterate if there is some text to compare with.
  if(text.length() != 0)
  {
    boolean match = false;

    for(int index = 0; index < getComboBox().getItemCount(); ++index)
    {
      String item = getComboBox().getItemAt(index).toString();

      if(isCaseSensitive() == true)
      {
        match = item.startsWith(text);
      }
      else
      {
        match = item.toLowerCase().startsWith(text.toLowerCase());
      }

      if(match == true)
      {
        // Display the item's actual text or as is typed?
        if(usesTypedText() == true)
        {
          field.setText(text + item.substring(text.length()));
        }
        else
        {
          field.setText(item);
        }

        // Highlight added text.
        field.setCaretPosition(item.length());
        field.moveCaretPosition(text.length());

        break;
      }
    }
  }
}