1 16 17 package org.apache.log4j.gui.examples; 18 19 import java.awt.BorderLayout ; 20 import java.awt.event.*; 21 import javax.swing.*; 22 import org.apache.log4j.*; 23 import org.apache.log4j.gui.*; 24 25 26 27 public class TextPaneAppenderExample implements ActionListener { 28 29 JFrame mainframe; 30 ButtonGroup priorities; 31 TextPaneAppender tpa; 32 Category gui; 33 Priority prio[]; 34 JTabbedPane logview; 35 36 37 public TextPaneAppenderExample () { 38 mainframe = new JFrame("Testing the TextPaneAppender..."); 39 mainframe.setSize(300,300); 40 logview = new JTabbedPane(); 41 createLogger(); 42 createMenuBar(); 43 mainframe.setVisible(true); 44 mainframe.getContentPane().add(logview); 45 } 46 47 public void createLogger() { 48 tpa = new TextPaneAppender(new PatternLayout("%-5p %d [%t]: %m%n"),"Debug"); 49 logview.addTab("Events ...",new JScrollPane(tpa.getTextPane())); 50 gui = Category.getInstance(this.getClass().getName()); 51 gui.addAppender(tpa); 52 } 53 54 public void createMenuBar() { 55 JMenu file = new JMenu("File"); 56 JMenuItem exit = new JMenuItem("Exit"); 57 exit.addActionListener(new ActionListener() { 58 public void actionPerformed(ActionEvent ae) { 59 System.exit(0); 60 } 61 }); 62 file.add(exit); 63 JMenuBar mb = new JMenuBar(); 64 mb.add(file); 65 66 JMenu logevent = new JMenu("LoggingEvents"); 67 JMenu selectprio = new JMenu("Priority"); 68 69 prio = Priority.getAllPossiblePriorities(); 70 JRadioButtonMenuItem priority[]= new JRadioButtonMenuItem[prio.length]; 71 priorities = new ButtonGroup(); 72 73 for (int i=0; i<prio.length;i++) { 74 if (i==0) 75 priority[i] = new JRadioButtonMenuItem(prio[i].toString(),true); 76 else 77 priority[i] = new JRadioButtonMenuItem(prio[i].toString()); 78 priority[i].setActionCommand(prio[i].toString()); 79 selectprio.add(priority[i]); 80 priorities.add(priority[i]); 81 82 } 83 84 logevent.add(selectprio); 85 86 JMenuItem lognow = new JMenuItem("LogIt!"); 87 lognow.addActionListener(this); 88 logevent.add(lognow); 89 90 mb.add(logevent); 91 92 mainframe.setJMenuBar(mb); 93 94 } 95 96 public void actionPerformed(ActionEvent ae){ 97 String logtext = JOptionPane.showInputDialog("Text to log"); 98 if (logtext == null) logtext="NO Input"; 99 int i=0; 100 String name = priorities.getSelection().getActionCommand(); 101 while (!prio[i].toString().equals(name)) 102 i=i+1; 103 gui.log(prio[i],logtext); 104 } 105 106 static public void main(String args[]) { 107 TextPaneAppenderExample tpex = new TextPaneAppenderExample(); 108 } 109 110 } 111 | Popular Tags |