Hai,I am trying to create a chat program in core java using nio channels in which the server responds multiple clients at a time.I created 3 clases namely client_login,client_form and nioserver.But the problem is that if the the user login sucessfully the frame for chating apperes,but the send and exit button that i am created is not appeares in the frame as well as it is not possible to close the frame.here is my client_form file.
public class Client_form extends javax.swing.JFrame {
public Client_form(String uname,String hostname) throws IOException
{
super(uname);
this.uname=uname;
this.hostname=hostname;
System.out.println(uname+""+hostname);
socketAddress = new InetSocketAddress(hostname,7789);
charset = Charset.forName("ISO-8859-1");
decoder = charset.newDecoder();
encoder = charset.newEncoder();
buffer = ByteBuffer.allocateDirect(1024);
buffer1 = ByteBuffer.allocateDirect(1024);
charBuffer = CharBuffer.allocate(1024);
channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(socketAddress);
selector = Selector.open();
channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE|SelectionKey.OP_READ);
setVisible(true);
initComponents();
setVisible(true);
}
public void client_start(){
String line=null;
try {
while (selector.select(500) > 0)
{
Set readyKeys = selector.selectedKeys();
Iterator readyItor = readyKeys.iterator();
while (readyItor.hasNext())
{
SelectionKey key = (SelectionKey)readyItor.next();
readyItor.remove();
SocketChannel keyChannel = (SocketChannel)key.channel();
if (key.isConnectable())
{
System.out.println("server foundllllllllllllll");
System.out.println("connected");
if (keyChannel.isConnectionPending())
{
keyChannel.finishConnect();
}
buffer1.clear();
System.out.println("username"+uname);
buffer1=ByteBuffer.wrap(new String(uname).getBytes());
keyChannel.write(buffer1);
buffer1.clear();
try
{
Thread.sleep(1000);
buffer1.clear();
charBuffer.clear();
keyChannel.read(buffer1);
buffer1.flip();
decoder.decode(buffer1, charBuffer, false);
charBuffer.flip();
String data=charBuffer.toString();
System.out.println("inside readmmmmmmmmmmm"+data);
System.out.println("before passing appendtext"+data);
tamsg.append("hai "+data+ "\n");
buffer1.clear();
charBuffer.clear();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
} catch(Exception ex) {}
}
@SuppressWarnings("unchecked")
private void initComponents() {
tamsg = new java.awt.TextArea();
tinput = new javax.swing.JTextField();
btnsnd = new javax.swing.JButton();
btnexit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tamsg.setEditable(false);
btnsnd.setText("Send");
btnsnd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsndActionPerformed(evt);
}
});
btnexit.setText("Exit");
btnexit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnexitActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tamsg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(tinput, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnsnd)
.addGap(26, 26, 26)
.addComponent(btnexit)
.addGap(0, 98, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(tamsg, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tinput, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addComponent(btnsnd)
.addComponent(btnexit))
.addContainerGap())
);
pack();
}
private void btnsndActionPerformed(java.awt.event.ActionEvent evt) {
String line=null;
String data=null;
line=tinput.getText();
buffer1.clear();
buffer1=ByteBuffer.wrap(new String(tinput.getText()).getBytes());
try
{
channel.write(buffer1);
tamsg.append(uname+": "+line+ "\n");
buffer1.clear();
channel.read(buffer1);
buffer1.flip();
buffer1.rewind();
buffer1.clear();
decoder.decode(buffer1, charBuffer, false);
charBuffer.flip();
data=charBuffer.toString();
System.out.println("server"+data);
tamsg.append("server:"+data+ "\n");
charBuffer.clear();
buffer1.clear();
}
catch (IOException ex)
{
Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
}
tinput.setText("");
buffer1.clear();
}
private void btnexitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private javax.swing.JButton btnexit;
private javax.swing.JButton btnsnd;
private java.awt.TextArea tamsg;
private javax.swing.JTextField tinput;
}