001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package examples;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022:
023: import javax.swing.*;
024:
025: public class MultiListener extends JFrame {
026: public MultiListener() {
027: setContentPane(new MultiListenerPanel());
028: }
029:
030: public static void main(String[] args) {
031: MultiListener theMultiListener = new MultiListener();
032:
033: theMultiListener.addWindowListener(new WindowAdapter() {
034: public void windowClosing(WindowEvent e) {
035: System.exit(0);
036: }
037: });
038:
039: theMultiListener.pack();
040: theMultiListener.setVisible(true);
041: }
042:
043: public static class MultiListenerPanel extends JPanel implements
044: ActionListener {
045: JTextArea topTextArea;
046: JTextArea bottomTextArea;
047: JButton button1, button2;
048: final static String newline = "\n";
049:
050: public MultiListenerPanel() {
051: init();
052: }
053:
054: public void init() {
055: JLabel l = null;
056:
057: GridBagLayout gridbag = new GridBagLayout();
058: GridBagConstraints c = new GridBagConstraints();
059: JPanel contentPane = this ;
060: contentPane.setLayout(gridbag);
061: contentPane.setBorder(BorderFactory.createCompoundBorder(
062: BorderFactory.createMatteBorder(1, 1, 2, 2,
063: Color.black), BorderFactory
064: .createEmptyBorder(5, 5, 5, 5)));
065:
066: c.fill = GridBagConstraints.BOTH;
067: c.gridwidth = GridBagConstraints.REMAINDER;
068: l = new JLabel("What MultiListener hears:");
069: gridbag.setConstraints(l, c);
070: contentPane.add(l);
071:
072: c.weighty = 1.0;
073: topTextArea = new JTextArea();
074: topTextArea.setEditable(false);
075: JScrollPane topScrollPane = new JScrollPane(topTextArea);
076:
077: Dimension preferredSize = new Dimension(200, 75);
078: topScrollPane.setPreferredSize(preferredSize);
079: gridbag.setConstraints(topScrollPane, c);
080: contentPane.add(topScrollPane);
081:
082: c.weightx = 0.0;
083: c.weighty = 0.0;
084: l = new JLabel("What Eavesdropper hears:");
085: gridbag.setConstraints(l, c);
086: contentPane.add(l);
087:
088: c.weighty = 1.0;
089: bottomTextArea = new JTextArea();
090: bottomTextArea.setEditable(false);
091: JScrollPane bottomScrollPane = new JScrollPane(
092: bottomTextArea);
093: bottomScrollPane.setPreferredSize(preferredSize);
094: gridbag.setConstraints(bottomScrollPane, c);
095: contentPane.add(bottomScrollPane);
096:
097: c.weightx = 1.0;
098: c.weighty = 0.0;
099: c.gridwidth = 1;
100: c.insets = new Insets(10, 10, 0, 10);
101: button1 = new JButton("Blah blah blah");
102: gridbag.setConstraints(button1, c);
103: contentPane.add(button1);
104:
105: c.gridwidth = GridBagConstraints.REMAINDER;
106: button2 = new JButton("You dont say!");
107: gridbag.setConstraints(button2, c);
108: contentPane.add(button2);
109:
110: button1.addActionListener(this );
111: button2.addActionListener(this );
112:
113: button2.addActionListener(new Eavesdropper(bottomTextArea));
114: }
115:
116: public void actionPerformed(ActionEvent e) {
117: topTextArea.append(e.getActionCommand() + newline);
118: }
119:
120: class Eavesdropper implements ActionListener {
121: JTextArea myTextArea;
122:
123: public Eavesdropper(JTextArea ta) {
124: myTextArea = ta;
125: }
126:
127: public void actionPerformed(ActionEvent e) {
128: myTextArea.append(e.getActionCommand()
129: + MultiListenerPanel.newline);
130: }
131: }
132: }
133: }
|