@Override public void setSelectedIndex(int ind) { super.setSelectedIndex(ind); editor.setText(getItemAt(ind).toString()); editor.setSelectionEnd(caretPos + editor.getText().length()); editor.moveCaretPosition(caretPos); }
private static void setupSelectionOnPreferredComponent(final JComponent component) { if (component instanceof JTextField) { JTextField field = (JTextField) component; String text = field.getText(); if (text != null && field.getClientProperty(HAVE_INITIAL_SELECTION) == null) { field.setSelectionStart(0); field.setSelectionEnd(text.length()); } } else if (component instanceof JComboBox) { JComboBox combobox = (JComboBox) component; combobox.getEditor().selectAll(); } }
/** * Replace the path component under the caret with the file selected from the completion list. * * @param file the selected file. * @param caretPos * @param start the start offset of the path component under the caret. * @param end the end offset of the path component under the caret. * @throws BadLocationException */ private void replacePathComponent(LookupFile file, int caretPos, int start, int end) throws BadLocationException { final Document doc = myPathTextField.getDocument(); myPathTextField.setSelectionStart(0); myPathTextField.setSelectionEnd(0); final String name = file.getName(); boolean toRemoveExistingName; String prefix = ""; if (caretPos >= start) { prefix = doc.getText(start, caretPos - start); if (prefix.length() == 0) { prefix = doc.getText(start, end - start); } if (SystemInfo.isFileSystemCaseSensitive) { toRemoveExistingName = name.startsWith(prefix) && prefix.length() > 0; } else { toRemoveExistingName = name.toUpperCase().startsWith(prefix.toUpperCase()) && prefix.length() > 0; } } else { toRemoveExistingName = true; } int newPos; if (toRemoveExistingName) { doc.remove(start, end - start); doc.insertString(start, name, doc.getDefaultRootElement().getAttributes()); newPos = start + name.length(); } else { doc.insertString(caretPos, name, doc.getDefaultRootElement().getAttributes()); newPos = caretPos + name.length(); } if (file.isDirectory()) { if (!myFinder.getSeparator().equals(doc.getText(newPos, 1))) { doc.insertString( newPos, myFinder.getSeparator(), doc.getDefaultRootElement().getAttributes()); newPos++; } } if (newPos < doc.getLength()) { if (myFinder.getSeparator().equals(doc.getText(newPos, 1))) { newPos++; } } myPathTextField.setCaretPosition(newPos); }
@Override public Component getTreeCellEditorComponent( JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) { final JTextField field = (JTextField) super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row); final Object node = ((DefaultMutableTreeNode) value).getUserObject(); final PackagingElement<?> element = ((PackagingElementNode) node).getElementIfSingle(); LOG.assertTrue(element != null); final String name = ((RenameablePackagingElement) element).getName(); field.setText(name); int i = name.lastIndexOf('.'); field.setSelectionStart(0); field.setSelectionEnd(i != -1 ? i : name.length()); return field; }
public void saveAs() { Vector<RopeFileFilter> filters = new Vector<RopeFileFilter>(); if (fileExt.equals("m") || fileExt.equals("mac")) { filters.add(new RopeFileFilter(new String[] {".m", ".mac"}, "Macro files (*.m *.mac)")); filters.add( new RopeFileFilter( new String[] {".a", ".asm", ".aut", ".s"}, "Assembly files (*.a *.asm *.aut *.s)")); } else { filters.add( new RopeFileFilter( new String[] {".a", ".asm", ".aut", ".s"}, "Assembly files (*.a *.asm *.aut *.s)")); filters.add(new RopeFileFilter(new String[] {".m", ".mac"}, "Macro files (*.m *.mac)")); } filters.add(new RopeFileFilter(new String[] {".txt"}, "Text files (*.txt)")); RopeFileChooser chooser = new RopeFileChooser(selectedPath, null, filters); chooser.setDialogTitle("Save Source File"); String fileName = String.format("%s.%s", baseName, fileExt); chooser.setSelectedFile(new File(selectedPath, fileName)); JTextField field = chooser.getTextField(); field.setSelectionStart(0); field.setSelectionEnd(baseName.length()); File file = chooser.save(ROPE.mainFrame); if (file != null) { selectedPath = file.getParent(); BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter(file)); writer.write(sourceArea.getText()); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (writer != null) { writer.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } }
GamePanel() { MODEL = new Model(); // Set JLabel messages from MODEL & center align problem = new JLabel(MODEL.prb); score = new JLabel(MODEL.str); msg = new JLabel(MODEL.msg); timeLab = new JLabel(MODEL.time); problem.setHorizontalAlignment(JLabel.CENTER); score.setHorizontalAlignment(JLabel.CENTER); msg.setHorizontalAlignment(JLabel.CENTER); timeLab.setHorizontalAlignment(JLabel.CENTER); // create area where user types in answer answerPrompt = new JLabel("Answer: "); answerPrompt.setHorizontalAlignment(JLabel.RIGHT); entry = new JTextField("Type..."); entry.setSelectionStart(0); entry.setSelectionEnd(7); entry.setHorizontalAlignment(JTextField.CENTER); clear = new JButton("Clear"); // answer area contained in an HBox JPanel HBox = new JPanel(); HBox.setLayout(new GridLayout(1, 3)); HBox.add(answerPrompt); HBox.add(entry); HBox.add(clear); HBox.setDoubleBuffered(true); // Timer MODEL.timer = new Timer( 1000, new ActionListener() { public void actionPerformed(ActionEvent e) { Update("time"); } }); // Entire Game Area in VBox (messages & answer area) JPanel VBox = new JPanel(); VBox.setLayout(new GridLayout(5, 1)); VBox.setDoubleBuffered(true); VBox.add(problem); VBox.add(HBox); VBox.add(score); VBox.add(msg); VBox.add(timeLab); // Create KeyPad JPanel ButtonPanel = new JPanel(); ButtonPanel.setLayout(new GridLayout(4, 3)); ButtonPanel.setDoubleBuffered(true); buttons = new JButton[10]; for (int i = 1; i < buttons.length; i++) { buttons[i] = new JButton(Integer.toString(i)); ButtonPanel.add(buttons[i]); } JLabel filler = new JLabel(" "); ButtonPanel.add(filler); buttons[0] = new JButton("0"); ButtonPanel.add(buttons[0]); // put everything together add(VBox); add(ButtonPanel); } // end of GamePanel() constructor