Java programming part 82 GUI : DocumentListener
Channel: Redemptie
Date: 2012-06-07

Java Programming tutorial, GUI, Graphical User Interface. this tutorial is teaching. How to Add DocumentListener to an TextArea in java using the Document class.


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

public class UserFrame extends JFrame implements ActionListener, DocumentListener{
 public static String name = "charles";
 public static JMenuItem print;
 public static JButton jb11;

 public static ImageIcon icon1;
 public static ImageIcon icon2;
 public static ImageIcon icon3;
 public static ImageIcon icon4;
 public static ImageIcon icon5;
 public static ImageIcon icon6;
 public static ImageIcon icon7;
 public static JCheckBox tbox;
 public static JRadioButton rbut;
 
 public static JTextArea area;
 public static JTextArea lineNumbers;
 
 public static JComboBox combo;
 public static JList list;
 
 UserFrame(){
  setTitle("UsersFrame");
  setSize(600,550);
   
 }


public static void main(String[] args){
 File dir = new File("C:");
 String[] FoldersAndFiles = dir.list();
 
 UserFrame f = new UserFrame();
 JMenuBar bar = new JMenuBar();
 bar.setToolTipText("this is the manu bar please use this carefully");
 JToolBar tb = new JToolBar();
 tb.setToolTipText("This is a too bar");
 tb.setLayout(new FlowLayout());
 icon1 = new ImageIcon("src\\Paint-icon.png");
 icon2 = new ImageIcon("src\\stop-icon.png");
 icon3 = new ImageIcon("src\\shield-icon.png");
 icon4 = new ImageIcon("src\\repair-icon.png");
 icon5 = new ImageIcon("src\\sunset-icon.png");
 icon6 = new ImageIcon("src\\strawberry-icon.png");
 icon7 = new ImageIcon("src\\girl-icon.png");

 tbox = new JCheckBox("What");
 rbut = new JRadioButton("Why");

 area = new JTextArea();
 Document doc = area.getDocument();
 doc.addDocumentListener(f);
 
 lineNumbers = new JTextArea(20,1);
 lineNumbers.setBackground(Color.YELLOW);
 
 

 combo = new JComboBox();
 combo.addItem("Plain");
 combo.addItem("Fancy");
 combo.addItem("Official");
 combo.addActionListener(f);
 
 jb11 = new JButton(icon1);
 jb11.addActionListener(f);
 jb11.setToolTipText("this button has a tool tip, please read!");
 JButton jb12 = new JButton(icon2);
 JButton jb13 = new JButton(icon3);
 JButton jb14 = new JButton(icon4);
 JButton jb15 = new JButton(icon5);
 JButton jb16 = new JButton(icon6);
 JButton jb17 = new JButton(icon7);
 JMenu fileMenu = new JMenu("File");
 JMenu editMenu = new JMenu("Edit");
 print = new JMenuItem("print name");
 JMenuItem print2 = new JMenuItem("print nothen");
 print.addActionListener(f);
 fileMenu.add(print);
 fileMenu.add(print2);
 bar.add(fileMenu);
 bar.add(editMenu);
 f.setJMenuBar(bar);
 

 tb.add(jb11);
 tb.add(jb12);
 tb.add(jb13);
 tb.add(jb14);
 tb.add(jb15);
 tb.add(jb16);
 tb.add(jb17);
 //tb.add(tbox);
 tb.add(rbut);
 tb.add(combo);

 JPanel pan = new JPanel();
 JPanel pan2 = new JPanel();
 pan2.setLayout(new BorderLayout());
 pan.setLayout(new BorderLayout());
 pan.add(tb,BorderLayout.NORTH);
 pan.add(pan2,BorderLayout.CENTER);
 pan2.add(area,BorderLayout.CENTER);
 pan2.add(lineNumbers,BorderLayout.WEST);
 JScrollPane scroll = new JScrollPane(pan);
 f.add(scroll);
 f.setDefaultCloseOperation(EXIT_ON_CLOSE);
 f.setVisible(true);
}


@Override
public void actionPerformed(ActionEvent e) {
 // TODO Auto-generated method stub
 
 Object c = (Object) e.getSource();

 if(c==combo){
  JComboBox cb = (JComboBox) e.getSource();
  String passed = (String) cb.getSelectedItem();
  System.out.println(passed);
 }
 if(c==jb11){
  System.out.println("Charles");
 }
 if(c==print){
  System.out.println("The menu item");
 }


}


@Override
public void changedUpdate(DocumentEvent d) {
 // TODO Auto-generated method stub
 
}


@Override
public void insertUpdate(DocumentEvent d) {
 // TODO Auto-generated method stub
 System.out.println(d.getType());
}


@Override
public void removeUpdate(DocumentEvent d) {
 // TODO Auto-generated method stub
 System.out.println(d.getType());
}
}