문제

How can i remove lines from a JTextArea one by one instead of all together?

I have a JTextArea which gets appended with string results from a thread, now i would like to remove one line at a time while the thread is executing.

도움이 되었습니까?

해결책

  • You first need to decide what should trigger the line removal.
  • Should it be the addition of a new line, so that total line number is constant. If so then you should write your code to call the line removal code in the same location that where a new line is added.
  • Or should it be at a constant rate -- and if so, then you will want to use a Swing Timer for this.
  • Then you need to decide which line to remove. If not the first line, then you'll need to figure out how to calculate which line. The javax.swing.text.Utilities class can help you find out the start and finish location of every line of text in your JTextArea.

Edit
You ask:

the main concern is about how to remove it from the JTextArea, i have already calculated the start and end positions of a line that has to be deleted.But what function can assist in removing just that one line?

  • You would first get the JTextArea's Document by calling, getDocument()
  • Then you could call remove(int offs, int length) on the Document as per the Document API.

다른 팁

Try This :

  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;

class SwingControlDemo {
String [] m; 
int i=0;
String append="";
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
Timer   t;

public SwingControlDemo(){
  prepareGUI();
}

public static void main(String[] args){
  SwingControlDemo  swingControlDemo = new SwingControlDemo();      
  swingControlDemo.showTextAreaDemo();
}

private void prepareGUI(){
  mainFrame = new JFrame("Java Swing Examples");
  mainFrame.setSize(400,400);
  mainFrame.setLayout(new GridLayout(3, 1));
  mainFrame.addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent windowEvent){
        System.exit(0);
     }        
  });    
  headerLabel = new JLabel("", JLabel.CENTER);        
  statusLabel = new JLabel("",JLabel.CENTER);    

  statusLabel.setSize(350,100);

  controlPanel = new JPanel();
  controlPanel.setLayout(new FlowLayout());

  mainFrame.add(headerLabel);
  mainFrame.add(controlPanel);
  mainFrame.add(statusLabel);
  mainFrame.setVisible(true);  
 }

private void showTextAreaDemo(){
  headerLabel.setText("Control in action: JTextArea"); 

  JLabel  commentlabel= new JLabel("Comments: ", JLabel.RIGHT);

  final JTextArea commentTextArea = 
     new JTextArea("This is a Swing tutorial "
     +"\n to make GUI application in Java."+"\n to make GUI application in Java"+"\n to make GUI application in Java",5,20);

  JScrollPane scrollPane = new JScrollPane(commentTextArea);    

  JButton showButton = new JButton("Show");

  showButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {     
     String s=commentTextArea.getText(); 
         m=s.split("\n");
        t.start();

     }
  }); 

 t=new Timer(1000,new ActionListener(){
     public void actionPerformed(ActionEvent e)
    {
    i++;
     append="";
    if(i<=m.length)
    {
     for(int j=i;j<m.length;j++)
     {
      append=append+m[j];
     }  
     commentTextArea.setText(append);

    }

    else
    {
    t.stop();   
    }
      }});
  controlPanel.add(commentlabel);
  controlPanel.add(scrollPane);        
  controlPanel.add(showButton);
  mainFrame.setVisible(true);  
     }
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top