public void actionPerformed(ActionEvent e) { JTextField tf = getTextField(); final String text = tf.getText(); final int caretIndex = tf.getCaretPosition(); String pre = text.substring(0, caretIndex); java.util.List<TreeNode> nodes = null; // m_root.search(pre); // Collections.sort(nodes); if (nodes.isEmpty()) { return; } JPopupMenu jp = new JPopupMenu("options"); JMenuItem lastItem = null; for (TreeNode node : nodes) { String nodeName = node.toString(); String insertion = nodeName.substring(leaf(pre, ".").length()); lastItem = createInsertAction(nodeName, caretIndex, insertion); jp.add(lastItem); } if (nodes.size() == 0) { return; } if (nodes.size() == 1) { lastItem.getAction().actionPerformed(null); } else { Point pos = tf.getCaret().getMagicCaretPosition(); pos = pos != null ? pos : new Point(2, 2); jp.show(tf, pos.x, pos.y); } }
public MainFunction() { fc = new JFileChooser(); openButton = new JButton("Open PDF"); openButton.setPreferredSize(new Dimension(490, 90)); openButton.addActionListener(this); openButton.setBackground(Color.ORANGE); // Area for logging of what is going on textField = new JTextField(44); textField.getCaret().setVisible(false); this.add(openButton); this.add(textField); }
@Override public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { JTextField txt1 = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column); if (value == null) value = ""; txt1.setText(value.toString().trim()); Rectangle rect = table.getCellRect(row, column, true); tblY = table.getLocationOnScreen().y + rect.getLocation().y; tblX = table.getLocationOnScreen().x + rect.getLocation().x; iRow = row; txt1.getCaret().setVisible(true); // txt1.setCaretPosition(0); txt1.requestFocus(); return txt1; }