Java,在JEditorPane中选择正确的文本

时间:2013-06-12 15:19:19

标签: java swing user-interface netbeans jeditorpane

我正在使用jEditorPane作为某个编辑器,有人要求我这样做,也需要找到替换搜索...所以我想让他选择查找然后更换在需要。

所以我提供了这个简单的代码:

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int start = (jEditorPane1.getSelectionStart()>jEditorPane1.getSelectionEnd())?jEditorPane1.getSelectionEnd():jEditorPane1.getSelectionStart();

    int max = (start>jEditorPane1.getSelectionStart())?start:jEditorPane1.getSelectionStart();

    String searchWord = jTextField3.getText();
    int searchIndex = jEditorPane1.getText().indexOf(searchWord, max);
    if(searchIndex != -1){
        jEditorPane1.select(searchIndex, searchIndex+searchWord.length());
    }
    else{
        jEditorPane1.setSelectionStart(-1);
        jEditorPane1.setSelectionEnd(-1);
    }
}       

幸运的是,应用程序返回了良好的索引,但不幸的是在视窗上我看不到任何选择。

我也是java新手,请帮忙


PS。按钮动作由netbeans本身提供


您要求的样本

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * test.java
 *
 * Created on Jun 12, 2013, 8:23:19 PM
 */
package wordchecker;

/**
 *
 * @author Hassan
 */
public class test extends javax.swing.JFrame {

    /** Creates new form test */
    public test() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(jTextPane1);

        jTextField1.setText("jTextField1");

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        int startFrom = jTextPane1.getSelectionStart();
        if(jTextPane1.getSelectionStart() == jTextPane1.getSelectionEnd()){
            startFrom = -1;
        }

        String searchWord = jTextField1.getText();
        int searchIndex = jTextPane1.getText().indexOf(searchWord, startFrom);
        if(searchIndex != -1){
            jTextPane1.select(searchIndex, searchIndex+searchWord.length());
        }
        else{
            jTextPane1.setSelectionStart(0);
            jTextPane1.setSelectionEnd(0);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new test().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration
}

1 个答案:

答案 0 :(得分:2)

仅在文本组件具有焦点时才显示文本组件的选择。当您单击按钮时,它会获得焦点,因此您看不到文本选择。您可以使按钮不可聚焦,或者您可以将以下内容添加到actionPerformed()方法的底部:

jTextPane1.requestFocusInWindow();

另外,不要使用getText()方法。这会导致偏移问题。

int searchIndex = jTextPane1.getText().indexOf(searchWord, startFrom);

有关更多信息和解决方案,请参阅Text and New Lines。此链接的基础是使用:

int length = textPane.getDocument().getLength();
String text = textPane.getDocument().getText(0, length);

以上只会返回“\ n”作为EOL字符串,因此当您进行搜索然后选择文本时,偏移量将匹配。

编辑:

我通常使用如下代码:

int startFrom = jTextPane1.getCaretPosition();