Java Reference
In-Depth Information
32.
33. public void createGui(){
34. mainFrame = new JFrame("Demonstration for the Router pattern - GUI #" +
instanceCount++);
35. Container content = mainFrame.getContentPane();
36. content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
37.
38. JPanel displayPanel = new JPanel();
39. display = new JTextArea(10, 40);
40. JScrollPane displayArea = new JScrollPane(display);
41. display.setEditable(false);
42. displayPanel.add(displayArea);
43. content.add(displayPanel);
44.
45. JPanel dataPanel = new JPanel();
46. dataPanel.add(new JLabel("Message:"));
47. inputTextField = new JTextField(30);
48. dataPanel.add(inputTextField);
49. content.add(dataPanel);
50.
51. JPanel controlPanel = new JPanel();
52. sendMessage = new JButton("Send Message");
53. clearDisplay = new JButton("Clear");
54. exit = new JButton("Exit");
55. controlPanel.add(sendMessage);
56. controlPanel.add(clearDisplay);
57. controlPanel.add(exit);
58. content.add(controlPanel);
59.
60. sendMessage.addActionListener(this);
61. clearDisplay.addActionListener(this);
62. exit.addActionListener(this);
63. inputTextField.addActionListener(this);
64.
65. mainFrame.addWindowListener(new WindowCloseManager());
66. mainFrame.pack();
67. mainFrame.setVisible(true);
68. }
69.
70. public void actionPerformed(ActionEvent evt){
71. Object source = evt.getSource();
72. if (source == sendMessage){ sendMessage(); }
73. else if (source == inputTextField){ sendMessage(); }
74. else if (source == clearDisplay){ clearDisplay(); }
75. else if (source == exit){ exitApplication(); }
76. }
77.
78. private class WindowCloseManager extends WindowAdapter{
79. public void windowClosing(WindowEvent evt){
80. exitApplication();
81. }
82. }
83.
84. private void exitApplication(){
85. System.exit(0);
86. }
87.
88. private void clearDisplay(){
89. inputTextField.setText("");
90. display.setText("");
91. }
92.
93. private void sendMessage(){
94. String data = inputTextField.getText();
95. routerClient.sendMessageToRouter(new Message(inputChannel, data));
96. inputTextField.setText("");
97. }
98.
99. public void receiveMessage(Message message){
100. display.append(message.getMessage() + "\n");
101. }
102. }
RunPattern coordinates a demonstration of the pattern by creating a series of RouterGui objects. In the example,
each RouterGui is connected up to some of the others through the Router . This means that a message sent by
RouterGui # 4 will be delivered to all of the GUIs, while one sent from RouterGui # 1 will be sent to GUIs # 2
and 3.
Search WWH ::




Custom Search