Marauroa Chat Tutorial/Swing Client: Difference between revisions
imported>Hendrik Brummermann splitted Marauroa Tutorial |
imported>Hendrik Brummermann m header and footer |
||
| Line 1: | Line 1: | ||
{{Navigation for Marauroa Top|Using}} |
|||
== Swing client == |
|||
{{Navigation for Marauroa Users}} |
|||
| ⚫ | |||
{{Marauroa Tutorial}} |
|||
| ⚫ | |||
The design of our Client class allows us to easily use it in something more complex then Test class above. Here is the example of a simple Swing application that uses Client as a communication tool |
The design of our Client class allows us to easily use it in something more complex then Test class above. Here is the example of a simple Swing application that uses Client as a communication tool |
||
<source lang="java"> |
<source lang="java"> |
||
| Line 135: | Line 138: | ||
} |
} |
||
</source> |
</source> |
||
== Deployment == |
|||
You still use the same command for compilation |
You still use the same command for compilation |
||
<pre> |
<pre> |
||
| Line 145: | Line 149: | ||
java -cp marauroa.jar;log4j.jar;. Chat |
java -cp marauroa.jar;log4j.jar;. Chat |
||
</pre> |
</pre> |
||
== Next Steps == |
|||
Congratulations, you completed this tutorial. The final sections will list some ideas for [[Marauroa Tutorial/Next Steps|further steps to improve]]. |
|||
[[Category:Marauroa]] |
|||
{{#breadcrumbs: [[Marauroa]] | [[Navigation for Marauroa Users|Using]] | [[Marauroa Tutorial|Tutorial]] | [[Marauroa Tutorial/Swing Client|Swing Client]]}} |
|||
Revision as of 23:49, 17 March 2011
Code
The design of our Client class allows us to easily use it in something more complex then Test class above. Here is the example of a simple Swing application that uses Client as a communication tool <source lang="java"> import javax.swing.*; import java.awt.event.*; import java.io.IOException; import java.net.URL; import java.util.*; import java.awt.*;
public class View extends JFrame implements ActionListener {
private javax.swing.JButton jSayButton; private javax.swing.JButton jConnectButton; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jChatArea; private javax.swing.JTextField jInputField; private javax.swing.Timer timer;
private Client client = null;
public View() {
initComponents();
jSayButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = jInputField.getText();
jInputField.setText("");
client.SendMessage(message);
}
});
jConnectButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
client = Client.get();
client.connect("127.0.0.1", 5555);
client.login("test1", "test1");
client.chooseCharacter("test1");
jSayButton.setEnabled(true);
jInputField.setEnabled(true);
} catch (Exception exception) {
JOptionPane.showMessageDialog(
View.this, "Error", exception.toString(), JOptionPane.WARNING_MESSAGE);
client = null;
}
}
});
timer = new javax.swing.Timer(300, this);
timer.setInitialDelay(500);
timer.start();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (client != null) {
client.loop(0);
String s = client.popQuote();
while (s != null) {
jChatArea.append(s + "\n");
s = client.popQuote();
}
}
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jChatArea = new javax.swing.JTextArea();
jSayButton = new javax.swing.JButton();
jConnectButton = new javax.swing.JButton();
jInputField = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Chat 0.5");
setName("main");
jChatArea.setColumns(20);
jChatArea.setEditable(false);
jChatArea.setRows(5);
jScrollPane1.setViewportView(jChatArea);
jSayButton.setText("Say");
jSayButton.setEnabled(false);
jConnectButton.setText("Connect");
jInputField.setEnabled(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jInputField, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSayButton, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jConnectButton)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jConnectButton)
.addComponent(jSayButton)
.addComponent(jInputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}
} </source>
To create the layout of the elements NetBeans was used, but we concentrate just on the source code here.
The client establishes a connection to a hard-coded location upon Connect button is pressed. There is no "Create new account" functionality, so account should be created beforehand.
We use timer to regularly check for the new messages in the Client queue. If there are some - they are put to the large text box. Sending the new message also relies on the Client facilities.
The only thing left for the Swing client now is a main method. Here is the source code <source lang="java"> final class Chat {
private Chat() {}
public static void main( String[] args ) {
new View();
}
} </source>
Deployment
You still use the same command for compilation
javac -cp marauroa.jar;log4j.jar;. *.java
Make sure that source files with code for Swing client are in the same directory with jars mentioned and Client.java file. You can run the client with the following command
java -cp marauroa.jar;log4j.jar;. Chat
Next Steps
Congratulations, you completed this tutorial. The final sections will list some ideas for further steps to improve. {{#breadcrumbs: Marauroa | Using | Tutorial | Swing Client}}