pertama silahkan buka aplikasi netbean, dan sebagai contoh kita akan menggunakan project chat client yang telah kita buat sebelumnya.
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
/**
*
* @author andik
*/
public class ChatClient extends javax.swing.JFrame {
/**
* Creates new form ChatClient
*/
private ObjectInputStream input;
private ObjectOutputStream output;
private Socket socket;
private String server, username;
private int port;
private List clients;
private List dialogs;
public ChatClient() {
clients = new ArrayList<>();
dialogs = new ArrayList<>();
initComponents();
}
public boolean start() {
try {
socket = new Socket(server, port);
} catch (Exception ec) {
System.out.println("Error connectiong to server:" + ec);
return false;
}
String msg = "Connection accepted " + socket.getInetAddress() + ":" + socket.getPort();
System.out.println(msg);
try {
input = new ObjectInputStream(socket.getInputStream());
output = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException eIO) {
System.out.println("Exception creating new Input/output Streams: " + eIO);
return false;
}
new ChatClient.ListenFromServer().start();
try {
output.writeObject("login~" + username + "~" + username + " sedang login...~server~\n");
output.writeObject("list~" + username + "~" + username + " sedang login...~server~\n");
} catch (IOException eIO) {
System.out.println("Exception doing login : " + eIO);
disconnect();
return false;
}
return true;
}
private void disconnect() {
try {
// TODO add your handling code here:
output.writeObject("logout~" + username + "~" + username + " sudah logout...~Server~\n");
} catch (IOException ex) {
//Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
}
try {
if (input != null) {
input.close();
}
} catch (Exception e) {
}
try {
if (output != null) {
output.close();
}
} catch (Exception e) {
}
try {
if (socket != null) {
socket.close();
}
} catch (Exception e) {
}
}
class ListenFromServer extends Thread {
@Override
public void run() {
while (true) {
try {
String msg = (String) input.readObject();
String res;
String type = msg.split("~")[0];
String pengirim = msg.split("~")[1];
String text = msg.split("~")[2];
String kepada = msg.split("~")[3];
switch (type) {
case "recieveText":
res = pengirim + ": " + text;
viewTextArea.setText(viewTextArea.getText() + res + "\n");
break;
case "recievePrivateText":
res = pengirim + ": " + text;
if (kepada.equals(username)) {
for (PMDialog pMDialog : dialogs) {
if (pMDialog.getName().equals(pengirim)) {
pMDialog.display(res);
pMDialog.setVisible(true);
break;
}
}
}
break;
case "login":
viewTextArea.setText(viewTextArea.getText() + pengirim + " sedah login..." + "\n");
clients.add(pengirim);
break;
case "logout":
viewTextArea.setText(viewTextArea.getText() + pengirim + " telah logout..." + "\n");
clients.remove(pengirim);
for (PMDialog pMDialog : dialogs) {
if (pMDialog.getName().equals(pengirim)) {
dialogs.remove(pMDialog);
break;
}
}
break;
case "list":
setTable(text);
break;
}
} catch (IOException e) {
System.out.println("Server has close the connection: " + e);
break;
} catch (ClassNotFoundException e2) {
}
}
}
private void setTable(String text) {
int rows = text.split(":").length - 1;
Object[][] data = new Object[rows][1];
for (int i = 0; i < rows; i++) { String t = text.split(":")[i]; data[i][0] = t; boolean ada = false; for (PMDialog pMDialog : dialogs) { if (pMDialog.getName().equals(t)) { ada = true; } } if (!ada) { PMDialog pmd = new PMDialog(ChatClient.this, socket, input, output, username, t); pmd.setName(t); pmd.setTitle(username + "/" + t); dialogs.add(pmd); } } String[] header = {"Clients"}; clientTable.setModel(new DefaultTableModel(data, header) { @Override public boolean isCellEditable(int row, int column) { return false; } }); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") //
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
popupMenu1 = new java.awt.PopupMenu();
popupMenu2 = new java.awt.PopupMenu();
jPanel3 = new javax.swing.JPanel();
usernameTextField = new javax.swing.JTextField();
masukButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
viewTextArea = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
clientTable = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
postTextField = new javax.swing.JTextField();
serverTextField = new javax.swing.JTextField();
kirimButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
portTextField = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
popupMenu1.setLabel("popupMenu1");
popupMenu2.setLabel("popupMenu2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel3.setBackground(new java.awt.Color(0, 102, 255));
usernameTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
usernameTextFieldActionPerformed(evt);
}
});
masukButton.setText("Masuk");
masukButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
masukButtonActionPerformed(evt);
}
});
viewTextArea.setColumns(20);
viewTextArea.setRows(5);
jScrollPane1.setViewportView(viewTextArea);
clientTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
clientTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
clientTableMouseClicked(evt);
}
});
jScrollPane2.setViewportView(clientTable);
jLabel1.setText("Server");
postTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
postTextFieldActionPerformed(evt);
}
});
kirimButton.setText("Kirim");
kirimButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kirimButtonActionPerformed(evt);
}
});
jLabel2.setText("Port");
jLabel3.setText("Username");
jLabel4.setFont(new java.awt.Font("Liberty Island Laser", 1, 24)); // NOI18N
jLabel4.setText("Aplikasi MultiChatClient");
jButton1.setFont(new java.awt.Font("Alien Encounters", 1, 12)); // NOI18N
jButton1.setText("Call");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(postTextField)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(kirimButton))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(portTextField, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(serverTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(10, 10, 10)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(usernameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
.addComponent(masukButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(220, 220, 220)
.addComponent(jLabel4)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(3, 3, 3)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(serverTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(portTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(masukButton)))
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(postTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
.addComponent(kirimButton)))
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}//
private void masukButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.server = serverTextField.getText();
this.port = new Integer(portTextField.getText());
this.username = usernameTextField.getText();
setTitle(server + ":" + port + "/" + username);
start();
}
private void kirimButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
String message = "postText~" + username + "~" + postTextField.getText() + "~all~\n";
output.writeObject(message);
postTextField.setText("");
} catch (IOException ex) {
Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void postTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
kirimButtonActionPerformed(evt);
}
private void clientTableMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if (evt.getClickCount() == 2 && clientTable.getSelectedRow() >= 0) {
String kepada = (String) clientTable.getValueAt(clientTable.getSelectedRow(), 0);
for (PMDialog pMDialog : dialogs) {
if (pMDialog.getName().equals(kepada) && !kepada.equals(username)) {
pMDialog.setTitle(username + "/" + kepada);
pMDialog.display("Silakan tulis pesan kepada " + kepada);
pMDialog.setVisible(true);
return;
}
}
}
}
private void usernameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String path = new String("C:\\Program Files (x86)\\Skype\\\\Phone\\Skype.exe");
System.out.println("Menjalankan MS Word");
try {
Process theProcess = Runtime.getRuntime().exec(path);
} catch(IOException e)
{
System.err.println("Error on exec() method");
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChatClient().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTable clientTable;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JButton kirimButton;
private javax.swing.JButton masukButton;
private java.awt.PopupMenu popupMenu1;
private java.awt.PopupMenu popupMenu2;
private javax.swing.JTextField portTextField;
private javax.swing.JTextField postTextField;
private javax.swing.JTextField serverTextField;
private javax.swing.JTextField usernameTextField;
private javax.swing.JTextArea viewTextArea;
// End of variables declaration
}
lalu, tambahkan jbutton dan rubah captionnya menjadi video.
lalu klik kanan pilih event >> action >> actionperformed
lalu masukan kode berikut
try {disini kita menggunakan aplikasi skype sebagai contoh.
proc = rt.exec(“C:\\Program Files\\Skype\\Phone\\Skype.exe”);
}
catch(Exception e) {
System.out.println(“Cannot execute application”);
}
kalian bisa menggunakan aplikasi lain seperti camstudio, steam, origin dll.
lalu tambahkan deklarasi berikut agar java dapat memanggil aplikasinya.
Process proc;lalu silahkan di jalankan di netbean.
Runtime rt = Runtime.getRuntime();
untuk lebih lengkapnya kalian bisa mengunduh kode lengkapnya disini.