Você está aqui: Java ::: Classes e Componentes ::: JTextArea |
Como adicionar capacidades de refazer e desfazer a um JTextAreaQuantidade de visualizações: 4 vezes |
// Este exemplo mostra como adicionar as capacidades
// de "desfazer" e "refazer" a um JTextField
import javax.swing.*;
import javax.swing.undo.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Estudos extends JFrame{
JTextArea textArea;
JButton desfazer, refazer;
public Estudos() {
super("Desfazer e Refazer em um JTextField");
Container c = getContentPane();
FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
c.setLayout(layout);
textArea = new JTextArea(10, 20);
textArea.setLineWrap(true);
final UndoManager undo = new UndoManager();
Document doc = textArea.getDocument();
// Ouve eventos de desfazer e refazer
doc.addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent evt) {
undo.addEdit(evt.getEdit());
}
});
desfazer = new JButton("Desfazer");
desfazer.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
if(undo.canUndo()){
undo.undo();
}
}
catch(CannotUndoException cue){
// possiveis erros são tratados aqui
}
}
});
refazer = new JButton("Refazer");
refazer.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
if(undo.canRedo()){
undo.redo();
}
}
catch(CannotUndoException cue){
// possiveis erros são tratados aqui
}
}
});
c.add(textArea);
c.add(desfazer);
c.add(refazer);
setSize(350, 250);
setVisible(true);
}
public static void main(String args[]){
Estudos app = new Estudos();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|
|
|
Desafios, Exercícios e Algoritmos Resolvidos de Java |
Veja mais Dicas e truques de Java |
Dicas e truques de outras linguagens |
E-Books em PDF |
||||
|
||||
|
||||
Linguagens Mais Populares |
||||
|
1º lugar: Java |





