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

The following examples show how to use javax.swing.JTextArea#requestFocus() . 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
Source File: Welcome.java    From btdex with GNU General Public License v3.0 4 votes vote down vote up
public Welcome(JFrame owner, boolean resetPin) {
	super(owner, ModalityType.APPLICATION_MODAL);
	setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	
	this.resetPin = resetPin;

	setTitle(tr(resetPin ? "welc_reset_pin" : "welc_welcome"));
	setResizable(false);

	JPanel topPanel = new JPanel(new BorderLayout());

	JPanel panel = new JPanel(new GridLayout(0, 2, 4, 4));

	if(!resetPin) {
		JPanel introPanel = new JPanel(new BorderLayout());
		String title = "<html><h2>" + tr("welc_intro_header") + "</h2>";
		introPanel.add(new JLabel(title), BorderLayout.PAGE_START);
		String intro = "<html>" + tr("welc_intro_text") + "<br><br>" + tr("welc_intro_pin");
		introText = new JLabel(intro);
		introText.setPreferredSize(new Dimension(60, 120));
		introPanel.add(introText, BorderLayout.PAGE_END);
		
		useLedgerButton = new JButton(tr("welc_use_ledger"));
		useLedgerButton.addActionListener(this);
		introPanel.add(useLedgerButton, BorderLayout.CENTER);
		topPanel.add(introPanel, BorderLayout.CENTER);
	}

	passphrase = new JTextArea(2, 40);
	passphrase.setWrapStyleWord(true);
	passphrase.setLineWrap(true);
	passphrase.setEditable(false);

	pin = new JPasswordField(12);
	pinCheck = new JPasswordField(12);

	acceptBox = new JCheckBox(tr("welc_wrote"));
	recoverBox = new JCheckBox(tr("welc_reuse"));

	recoverBox.addActionListener(this);

	JPanel recoveryPanel = new JPanel(new BorderLayout());
	recoveryPanel.setBorder(BorderFactory.createTitledBorder(tr(resetPin ?
			"welc_prhase_to_redefine" :
			"welc_recovery_phrase")));
	recoveryPanel.add(recoverBox, BorderLayout.PAGE_START);
	recoveryPanel.add(passphrase, BorderLayout.CENTER);
	recoveryPanel.add(acceptBox, BorderLayout.PAGE_END);

	topPanel.add(recoveryPanel, BorderLayout.PAGE_END);

	// Create a button
	JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));

	calcelButton = new JButton(tr("dlg_cancel"));
	okButton = new JButton(tr("dlg_ok"));
	getRootPane().setDefaultButton(okButton);

	calcelButton.addActionListener(this);
	okButton.addActionListener(this);

	panel.add(new Desc(tr("dlg_pin"), pin));
	panel.add(new Desc(tr("welc_reenter_pin"), pinCheck));

	//		buttonPane.add(acceptBox);
	buttonPane.add(calcelButton);
	buttonPane.add(okButton);

	// set action listener on the button

	JPanel content = (JPanel)getContentPane();
	content.setBorder(new EmptyBorder(4, 4, 4, 4));

	content.add(topPanel, BorderLayout.PAGE_START);
	content.add(panel, BorderLayout.CENTER);
	content.add(buttonPane, BorderLayout.PAGE_END);

	if(resetPin) {
		passphrase.setText("");
		passphrase.setEditable(true);
		passphrase.requestFocus();
		
		recoverBox.setVisible(false);
		acceptBox.setSelected(true);
		acceptBox.setVisible(false);
	}
	else
		newPass();
	pack();

	addWindowListener(new WindowAdapter() {
		@Override
		public void windowActivated(WindowEvent event) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					acceptBox.requestFocusInWindow();
				}
			});
		}
	});
}
 
Example 2
Source File: MainView.java    From HiJson with Apache License 2.0 4 votes vote down vote up
/**
 * 文本内容查找定位
 * @param key 要查找的字符串
 * @param ignoreCase 是否区分大小写
 * @param down  查找方向(向上false,向下true)
 * @param isFirst 是否从开头开始查找
 * @return
 */
public boolean startSegmentFindOrReplaceOperation(JTextArea textArea, String key, boolean ignoreCase, boolean down,boolean isFirst) {
    int length = key.length();
    Document doc = textArea.getDocument();
    int offset = textArea.getCaretPosition();
    int charsLeft = doc.getLength() - offset;
    if(charsLeft <=0 ){
        offset = 0;
        charsLeft = doc.getLength() - offset;
    }
    if (!down) {
        offset -= length;
        offset--;
        charsLeft = offset;
    }
    if(isFirst){
        offset = 0;
        charsLeft = doc.getLength() - offset;
    }
    Segment text = new Segment();
    text.setPartialReturn(true);
    try {
        while (charsLeft > 0) {
            doc.getText(offset, length, text);
            if ((ignoreCase == true && text.toString().equalsIgnoreCase(key))
                    || (ignoreCase == false && text.toString().equals(key))) {
                textArea.requestFocus();////焦点,才能能看到效果
                textArea.setSelectionStart(offset);
                textArea.setSelectionEnd(offset + length);
                return true;
            }
            charsLeft--;
            if (down) {
                offset++;
            } else {
                offset--;
            }

        }
    } catch (Exception e) {

    }
    return false;
}
 
Example 3
Source File: Main.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
public static void consolePrint(JTextArea console, String text) {
    console.append(text);
    console.setCaretPosition(console.getDocument().getLength());
    console.requestFocus();
}
 
Example 4
Source File: BandMathsDialog.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
private void makeUI() {
    JButton loadExpressionButton = new JButton("Load...");
    loadExpressionButton.setName("loadExpressionButton");
    loadExpressionButton.addActionListener(createLoadExpressionButtonListener());

    JButton saveExpressionButton = new JButton("Save...");
    saveExpressionButton.setName("saveExpressionButton");
    saveExpressionButton.addActionListener(createSaveExpressionButtonListener());

    JButton editExpressionButton = new JButton("Edit Expression...");
    editExpressionButton.setName("editExpressionButton");
    editExpressionButton.addActionListener(createEditExpressionButtonListener());

    final JPanel panel = GridBagUtils.createPanel();
    int line = 0;
    GridBagConstraints gbc = new GridBagConstraints();

    JComponent[] components = createComponents(PROPERTY_NAME_PRODUCT, SingleSelectionEditor.class);
    gbc.gridy = ++line;
    GridBagUtils.addToPanel(panel, components[1], gbc, "gridwidth=3, fill=BOTH, weightx=1");
    gbc.gridy = ++line;
    GridBagUtils.addToPanel(panel, components[0], gbc, "insets.top=3, gridwidth=3, fill=BOTH, anchor=WEST");

    gbc.gridy = ++line;
    components = createComponents(PROPERTY_NAME_BAND_NAME, TextFieldEditor.class);
    GridBagUtils.addToPanel(panel, components[1], gbc,
                            "weightx=0, insets.top=3, gridwidth=1, fill=HORIZONTAL, anchor=WEST");
    GridBagUtils.addToPanel(panel, components[0], gbc,
                            "weightx=1, insets.top=3, gridwidth=2, fill=HORIZONTAL, anchor=WEST");

    gbc.gridy = ++line;
    components = createComponents(PROPERTY_NAME_BAND_DESC, TextFieldEditor.class);
    GridBagUtils.addToPanel(panel, components[1], gbc,
                            "weightx=0, insets.top=3, gridwidth=1, fill=HORIZONTAL, anchor=WEST");
    GridBagUtils.addToPanel(panel, components[0], gbc,
                            "weightx=1, insets.top=3, gridwidth=2, fill=HORIZONTAL, anchor=WEST");

    gbc.gridy = ++line;
    components = createComponents(PROPERTY_NAME_BAND_UNIT, TextFieldEditor.class);
    GridBagUtils.addToPanel(panel, components[1], gbc,
                            "weightx=0, insets.top=3, gridwidth=1, fill=HORIZONTAL, anchor=WEST");
    GridBagUtils.addToPanel(panel, components[0], gbc,
                            "weightx=1, insets.top=3, gridwidth=2, fill=HORIZONTAL, anchor=WEST");

    gbc.gridy = ++line;
    components = createComponents(PROPERTY_NAME_BAND_WAVELENGTH, TextFieldEditor.class);
    GridBagUtils.addToPanel(panel, components[1], gbc,
                            "weightx=0, insets.top=3, gridwidth=1, fill=HORIZONTAL, anchor=WEST");
    GridBagUtils.addToPanel(panel, components[0], gbc,
                            "weightx=1, insets.top=3, gridwidth=2, fill=HORIZONTAL, anchor=WEST");

    gbc.gridy = ++line;
    components = createComponents(PROPERTY_NAME_SAVE_EXPRESSION_ONLY, CheckBoxEditor.class);
    GridBagUtils.addToPanel(panel, components[0], gbc, "insets.top=3, gridwidth=3, fill=HORIZONTAL, anchor=EAST");

    gbc.gridy = ++line;
    JPanel nodataPanel = new JPanel(new BorderLayout());
    components = createComponents(PROPERTY_NAME_NO_DATA_VALUE_USED, CheckBoxEditor.class);
    nodataPanel.add(components[0], BorderLayout.WEST);
    components = createComponents(PROPERTY_NAME_NO_DATA_VALUE, NumericEditor.class);
    nodataPanel.add(components[0]);
    GridBagUtils.addToPanel(panel, nodataPanel, gbc,
                            "weightx=1, insets.top=3, gridwidth=3, fill=HORIZONTAL, anchor=WEST");

    gbc.gridy = ++line;
    components = createComponents(PROPERTY_NAME_GENERATE_UNCERTAINTY_BAND, CheckBoxEditor.class);
    GridBagUtils.addToPanel(panel, components[0], gbc, "insets.top=3, gridwidth=3, fill=HORIZONTAL, anchor=EAST");

    gbc.gridy = ++line;
    JLabel expressionLabel = new JLabel(Bundle.CTL_BandMathsDialog_LblExpression());
    JTextArea expressionArea = new JTextArea();
    expressionArea.setRows(3);
    TextComponentAdapter textComponentAdapter = new TextComponentAdapter(expressionArea);
    bindingContext.bind(PROPERTY_NAME_EXPRESSION, textComponentAdapter);

    GridBagUtils.addToPanel(panel, expressionLabel, gbc, "insets.top=3, gridwidth=3, anchor=WEST");
    gbc.gridy = ++line;
    GridBagUtils.addToPanel(panel, expressionArea, gbc,
                            "weighty=1, insets.top=3, gridwidth=3, fill=BOTH, anchor=WEST");
    gbc.gridy = ++line;
    final JPanel loadSavePanel = new JPanel();
    loadSavePanel.add(loadExpressionButton);
    loadSavePanel.add(saveExpressionButton);
    GridBagUtils.addToPanel(panel, loadSavePanel, gbc,
                            "weighty=0, insets.top=3, gridwidth=2, fill=NONE, anchor=WEST");
    GridBagUtils.addToPanel(panel, editExpressionButton, gbc,
                            "weighty=1, insets.top=3, gridwidth=1, fill=HORIZONTAL, anchor=EAST");

    gbc.gridy = ++line;
    GridBagUtils.addToPanel(panel, new JLabel(""), gbc,
                            "insets.top=10, weightx=1, weighty=1, gridwidth=3, fill=BOTH, anchor=WEST");

    setContent(panel);

    expressionArea.selectAll();
    expressionArea.requestFocus();
}
 
Example 5
Source File: JTextDrawingObjectDialog.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void init(Component owner) {
      mComboBoxTextSize = new JComboBox(TEXT_SIZE_LIST);
mComboBoxTextSize.setEditable(true);
mComboBoxTextSize.setSelectedItem(""+(int)mTextObject.getSize());

      mComboBoxStyle = new JComboBox(TEXT_STYLE_LIST);
int styleIndex = 0;
for (int i=0; i<TEXT_STYLE.length; i++) {
	if (mTextObject.getStyle() == TEXT_STYLE[i]) {
		styleIndex = i;
		break;
		}
	}
mComboBoxStyle.setSelectedIndex(styleIndex);

JComponent menuPanel = new JPanel();
double[][] size = { { 8, TableLayout.PREFERRED, 4, TableLayout.PREFERRED, 8 },
					{ 8, TableLayout.PREFERRED, 4, TableLayout.PREFERRED, 8 } };
menuPanel.setLayout(new TableLayout(size));

menuPanel.add(new JLabel("Text size:"), "1,1");
menuPanel.add(mComboBoxTextSize, "3,1");
menuPanel.add(new JLabel("Text style:"), "1,3");
menuPanel.add(mComboBoxStyle, "3,3");

mTextArea = new JTextArea(mTextObject.getText(), 3, 20);
      mTextArea.setBorder(BorderFactory.createEtchedBorder());
JPanel textPanel = new JPanel();
      textPanel.setBorder(BorderFactory.createEmptyBorder(0,4,0,4));
textPanel.add(mTextArea);

JButton buttonOK = new JButton("OK");
buttonOK.addActionListener(this);
JButton buttonCancel = new JButton("Cancel");
buttonCancel.addActionListener(this);
JPanel innerButtonPanel = new JPanel();
      innerButtonPanel.setLayout(new GridLayout(1,2,8,0));
      innerButtonPanel.add(buttonCancel);
      innerButtonPanel.add(buttonOK);
      innerButtonPanel.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
JPanel buttonPanel = new JPanel();
      buttonPanel.setLayout(new BorderLayout());
      buttonPanel.add(innerButtonPanel, BorderLayout.EAST);

getContentPane().setLayout(new BorderLayout());
getContentPane().add(menuPanel,  BorderLayout.NORTH);
getContentPane().add(textPanel,  BorderLayout.CENTER);
getContentPane().add(buttonPanel,  BorderLayout.SOUTH);

getRootPane().setDefaultButton(buttonOK);

pack();
setLocationRelativeTo(owner);

mTextArea.requestFocus();
setVisible(true);
}
 
Example 6
Source File: InputDialog.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
  * Prompt and return input.
  *
  * @param title       the title of the dialog.
  * @param description the dialog description.
  * @param icon        the icon to use.
  * @param parent      the parent to use.
  * @return the user input.
  */
 public String getInput(String title, String description, Icon icon, Component parent) {
     textArea = new JTextArea();
     textArea.setLineWrap(true);

     TitlePanel titlePanel = new TitlePanel(title, description, icon, true);

     // Construct main panel w/ layout.
     final JPanel mainPanel = new JPanel();
     mainPanel.setLayout(new BorderLayout());
     mainPanel.add(titlePanel, BorderLayout.NORTH);

     // The user should only be able to close this dialog.
     final Object[] options = {Res.getString("ok"), Res.getString("cancel")};
     optionPane = new JOptionPane(new JScrollPane(textArea), JOptionPane.PLAIN_MESSAGE,
         JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);

     mainPanel.add(optionPane, BorderLayout.CENTER);

     // Lets make sure that the dialog is modal. Cannot risk people
     // losing this dialog.
     JOptionPane p = new JOptionPane();
     dialog = p.createDialog(parent, title);
     dialog.setModal(true);
     dialog.pack();
     dialog.setSize(width, height);
     dialog.setContentPane(mainPanel);
     dialog.setLocationRelativeTo(parent);
     optionPane.addPropertyChangeListener(this);

     // Add Key Listener to Send Field
     textArea.addKeyListener(new KeyAdapter() {
         @Override
public void keyPressed(KeyEvent e) {
             if (e.getKeyChar() == KeyEvent.VK_TAB) {
                 optionPane.requestFocus();
             }
             else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                 dialog.dispose();
             }
         }
     });

     textArea.requestFocus();
     textArea.setWrapStyleWord(true);


     dialog.setVisible(true);
     return stringValue;
 }
 
Example 7
Source File: InputTextAreaDialog.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
  * Prompt and return input.
  *
  * @param title       the title of the dialog.
  * @param description the dialog description.
  * @param icon        the icon to use.
  * @param parent      the parent to use.
  * @return the user input.
  */
 public String getInput(String title, String description, Icon icon, Component parent) {
     textArea = new JTextArea();
     textArea.setLineWrap(true);

     TitlePanel titlePanel = new TitlePanel(title, description, icon, true);

     // Construct main panel w/ layout.
     final JPanel mainPanel = new JPanel();
     mainPanel.setLayout(new BorderLayout());
     mainPanel.add(titlePanel, BorderLayout.NORTH);

     // The user should only be able to close this dialog.
     final Object[] options = {Res.getString("ok"), Res.getString("cancel")};
     optionPane = new JOptionPane(new JScrollPane(textArea), JOptionPane.PLAIN_MESSAGE,
             JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);

     mainPanel.add(optionPane, BorderLayout.CENTER);

     // Let's make sure that the dialog is modal. Cannot risk people
     // losing this dialog.
     JOptionPane p = new JOptionPane();
     dialog = p.createDialog(parent, title);
     dialog.setModal(true);
     dialog.pack();
     dialog.setSize(width, height);
     dialog.setContentPane(mainPanel);
     dialog.setLocationRelativeTo(parent);
     optionPane.addPropertyChangeListener(this);

     // Add Key Listener to Send Field
     textArea.addKeyListener(new KeyAdapter() {
         @Override
public void keyPressed(KeyEvent e) {
             if (e.getKeyChar() == KeyEvent.VK_TAB) {
                 optionPane.requestFocus();
             }
             else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                 dialog.dispose();
             }
         }
     });

     textArea.requestFocus();


     dialog.setVisible(true);
     return stringValue;
 }