Java Code Examples for javax.swing.JTextArea.replaceRange()

The following are Jave code examples for showing how to use replaceRange() of the javax.swing.JTextArea class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: EditCalculateAndChart   File: Delete_Action.java   Source Code and License Vote up 5 votes
@Override
public void actionPerformed(ActionEvent e) {
    
    
     JTextArea area = TEdit.getTextArea();
     Highlighter hilite = area.getHighlighter();
     Highlighter.Highlight[] hilites = hilite.getHighlights();
     int Shift=0;
     
     if(hilites != null){ 
         if(hilites.length == 0 && 
                 TEdit.getSwingPool().containsKey("area.hilites")){
             hilites = (Highlighter.Highlight[])TEdit.getSwingPool().get("area.hilites");
             TEdit.getSwingPool().remove("area.hilites");
         }
             
         for (Highlighter.Highlight hilite1 : hilites) {
               area.replaceRange("",hilite1.getStartOffset()-Shift, hilite1.getEndOffset()-Shift);
               Shift = Shift -(hilite1.getEndOffset()-hilite1.getStartOffset());
         }
        
         if(hilites.length>0){
         area.setCaretPosition(hilites[0].getStartOffset());
         area.getCaret().setVisible(true);
         }
                    TEdit.setEnabled("Calc",false);
                    TEdit.setEnabled("Delete",false);
                    TEdit.setEnabled("Save",true); 
                    TEdit.setEnabled("SaveAs",true);
     }
}
 
Example 2
Project: FinalSpeed   File: LogFrame.java   Source Code and License Vote up 5 votes
public void trunkTextArea(JTextArea txtWin){
    int numLinesToTrunk = txtWin.getLineCount() - SCROLL_BUFFER_SIZE;
    if(numLinesToTrunk > 0)
    {
        try
        {
            int posOfLastLineToTrunk = txtWin.getLineEndOffset(numLinesToTrunk - 1);
            txtWin.replaceRange("",0,posOfLastLineToTrunk);
        }
        catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }
}
 
Example 3
Project: EditCalculateAndChart   File: Replace_Action.java   Source Code and License Vote up 4 votes
@Override
public void actionPerformed(ActionEvent e) {
    JTextArea area = TEdit.getTextArea();
    if(area.getText().length()==0){
        JOptionPane.showMessageDialog(TEdit.getFrame(),"There is nothing to replace!");
        return;
    }
    JTextField source = new JTextField();
    JTextField target = new JTextField();
    Object[] message = {
                          "Source:", source,
                          "Target:", target
                        };
    int option = JOptionPane.showConfirmDialog(TEdit.getFrame(), message, "Replace", JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE,myIcon);
    if (option != JOptionPane.OK_OPTION)
        return;
    
    String src = source.getText();
    String trgt= target.getText();
    
    if(src.contentEquals(""))
            return;
    
             Highlighter hilite = area.getHighlighter();
     Highlighter.Highlight[] hilites = hilite.getHighlights();
     int Shift=0;
     String txt=area.getText();
     if(hilites != null && hilites.length >0){ 
         for (Highlighter.Highlight hilite1 : hilites) {
                //txt.substring(hilite1.getStartOffset(), hilite1.getEndOffset()))
               area.replaceRange(txt.substring(hilite1.getStartOffset(), hilite1.getEndOffset()).replace(src,trgt),
                              hilite1.getStartOffset()-Shift, hilite1.getEndOffset()-Shift);
               Shift = Shift -(src.length()-trgt.length());
         }
        
     }
     else{
         txt=txt.replace(src,trgt);
         area.setText(txt);
     }
            
            
    
     
}