Basic Chatroom Skeleton Application

Basic Chatroom Skeleton Application
This Java code is not addressing the core of a chat application (sending and receiving messages) however it makes a good GUI skeleton for starting such an application.
1.  import java.awt.BorderLayout;
2.  import javax.swing.JPanel;
3.  import javax.swing.JFrame;
4.  import javax.swing.JTextArea;
5.  import javax.swing.JTextField;
6.  import javax.swing.JButton;
7.  import java.awt.Dimension;
8.  import java.awt.event.*;
9.  import javax.swing.BoxLayout;
10.  
11. public class Chatroom extends JFrame {
12.  
13.         private static final long serialVersionUID = 1L;
14.  
15.         private JPanel jContentPane = null;
16.  
17.         private JTextArea jTextArea = null;
18.  
19.         private JPanel jPanel = null;
20.  
21.         private JTextField jTextField = null;
22. 
23.         private JButton jButton = null;
24. 
25.         /**
26.          * This is the default constructor
27.          */
28.         public Chatroom() {
29.                 super();
30.                initialize();
31.         }
32. 
33.         /**
34.          * This method initializes this
35.          *
36.          * @return void
37.          */
38.         private void initialize() {
39.                 this.setSize(411, 331);
40.                 this.setContentPane(getJContentPane());
41.                 this.setTitle("Chatroom");
42.         }
43.  
44.         /**
45.          * This method initializes jContentPane
46.          *
47.          * @return javax.swing.JPanel
48.          */
49.         private JPanel getJContentPane() {
50.                 if (jContentPane == null) {
51.                         jContentPane = new JPanel();
52.                         jContentPane.setLayout(new BorderLayout());
53.                         jContentPane.add(getJTextArea(), BorderLayout.CENTER);
54.                         jContentPane.add(getJPanel(), BorderLayout.SOUTH);
55.                 }
56.                 return jContentPane;
57.         }
58.  
59.         /**
60.          * This method initializes jTextArea   
61.          *      
62.          * @return javax.swing.JTextArea       
63.          */
64.         private JTextArea getJTextArea() {
65.                 if (jTextArea == null) {
66.                         jTextArea = new JTextArea();
67.                         jTextArea.setEnabled(true);
68.                         jTextArea.setEditable(false);
69.                 }
70.                 return jTextArea;
71.         }
72.  
73.         /**
74.          * This method initializes jPanel       
75.          *      
76.          * @return javax.swing.JPanel   
77.          */
78.         private JPanel getJPanel() {
79.                 if (jPanel == null) {
80.                         jPanel = new JPanel();
81.                         jPanel.setLayout(new BoxLayout(getJPanel(), BoxLayout.X_AXIS));
82.                         jPanel.setPreferredSize(new Dimension(0, 30));
83.                         jPanel.add(getJTextField(), null);
84.                         jPanel.add(getJButton(), null);
85.                 }
86.                 return jPanel;
87.         }
88.  
89.         /**
90.          * This method initializes jTextField   
91.          *      
92.          * @return javax.swing.JTextField       
93.          */
94.         private JTextField getJTextField() {
95.                 if (jTextField == null) {
96.                         jTextField = new JTextField();
97.                         jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
98.                                 public void keyPressed(java.awt.event.KeyEvent e) {
99.                                         // If Enter was pressed
100.                                         if(e.getKeyCode() == 10)
101.                                         {
102.                                                 addMessage();
103.                                         }
104.                                         setTitle("Chatroom - Typing a message...");
105.                                         t.start();
106.                                 }
107.                         });
108.                 }
109.                 return jTextField;
110.         }
111.        
112.         private void addMessage()
113.         {
114.                 jTextArea.append(jTextField.getText() + "\r\n"); // TODO Auto-generated Event stub mouseClicked()
115.                 jTextField.setText("");
116.                 setTitle("Chatroom");
117.         }
118.        
119.         javax.swing.Timer t = new javax.swing.Timer(3000, new ActionListener() {
120.         public void actionPerformed(ActionEvent e) {
121.                 setTitle("Chatroom");  //  @jve:decl-index=0:
122.         }
123.      });
124.  
125.         /**
126.          * This method initializes jButton     
127.          *      
128.          * @return javax.swing.JButton 
129.          */
130.         private JButton getJButton() {
131.                 if (jButton == null) {
132.                         jButton = new JButton();
133.                         jButton.setPreferredSize(new Dimension(80, 50));
134.                         jButton.setText("SEND");
135.                         jButton.addMouseListener(new java.awt.event.MouseAdapter() {
136.                                 public void mouseClicked(java.awt.event.MouseEvent e) {
137.                                         addMessage();
138.                                 }
139.                         });
140.                 }
141.                 return jButton;
142.         }
143.  
144. }  //  @jve:decl-index=0:visual-constraint="10,10"
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top