Forums Register Login

How to activate threads in Swings.

+Pie Number of slices to send: Send
Hi frds, i'm using client and Server communication using Sockets by using Swings,how can i call a method using thread which hve to run till da prgm exit.
The method inludes reading of data stream continusly whn ever send button is clicked.i'm using NetBeans.
Pgm is....
package networking;
import java.io.*;
import java.net.*;
class Ser extends javax.swing.JFrame implements Runnable
{
ServerSocket server;
Socket client;
ObjectOutputStream output;
ObjectInputStream input;
public Ser()
{
super("SERVER DISPLAY");
initComponents();
setVisible(true);
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

PortNo = new javax.swing.JLabel();
PortNoTF = new javax.swing.JTextField();
Start = new javax.swing.JButton();
Message = new javax.swing.JLabel();
MessageTF = new javax.swing.JTextField();
Send = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
ServerDisplayTA = new javax.swing.JTextArea();
DisConnect = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

PortNo.setText("PortNo");

Start.setText("Start");
Start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StartActionPerformed(evt);
}
});

Message.setText("Message");

MessageTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MessageTFActionPerformed(evt);
}
});

Send.setText("Send");
Send.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SendActionPerformed(evt);
}
});

jLabel1.setText("SERVER DISPLAY");

ServerDisplayTA.setColumns(20);
ServerDisplayTA.setRows(5);
jScrollPane1.setViewportView(ServerDisplayTA);

DisConnect.setText("DisConnect");
DisConnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DisConnectActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(PortNo, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Message, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(PortNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Start))
.addGroup(layout.createSequentialGroup()
.addComponent(MessageTF, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Send))))
.addGroup(layout.createSequentialGroup()
.addGap(182, 182, 182)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(183, 183, 183)
.addComponent(DisConnect)))
.addContainerGap(140, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PortNo)
.addComponent(PortNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Start))
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Message)
.addComponent(MessageTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Send))
.addGap(40, 40, 40)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(DisConnect))
);

pack();
}// </editor-fold>

private void MessageTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void StartActionPerformed(java.awt.event.ActionEvent evt) {
ServerDisplayTA.append("Waiting for Server "+"\n");
try
{
server=new ServerSocket(1234,100);
client=server.accept();
ServerDisplayTA.append("Server Accepted the Connection"+"\n");
}
catch(IOException e)
{
e.printStackTrace();
ServerDisplayTA.append("Server Could not Connect"+"\n");
}

// TODO add your handling code here:
}

private void SendActionPerformed(java.awt.event.ActionEvent evt) {
String mesg=MessageTF.getText();
try
{
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
output.writeObject(mesg);
ServerDisplayTA.append("SERVER>>"+mesg+"\n");
}
catch(IOException e)
{
e.printStackTrace();
ServerDisplayTA.append("Server Could not Send"+"\n");
}
// TODO add your handling code here:
}

private void DisConnectActionPerformed(java.awt.event.ActionEvent evt) {
try
{
server.close();
client.close();
output.close();
input.close();
ServerDisplayTA.append("Server Terminated Application"+"\n");
}
catch(IOException e)
{
e.printStackTrace();
ServerDisplayTA.append("Server Cannot Terminate During Communication"+"\n");
}

}
public void run()
{
String msg;
try
{
input=new ObjectInputStream(client.getInputStream());
msg=(String)input.readObject();
ServerDisplayTA.append("CLIENT>>"+msg+"\n");
run();
}
catch(IOException e)
{
e.printStackTrace();
ServerDisplayTA.append("IO Exception During Termination"+"\n");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
ServerDisplayTA.append("IO Exception During Termination"+"\n");
}
catch(NullPointerException e)
{
e.printStackTrace();
ServerDisplayTA.append("Exception in run");
}
}


// Variables declaration - do not modify
private javax.swing.JButton DisConnect;
private javax.swing.JLabel Message;
private javax.swing.JTextField MessageTF;
private javax.swing.JLabel PortNo;
private javax.swing.JTextField PortNoTF;
private javax.swing.JButton Send;
private javax.swing.JTextArea ServerDisplayTA;
private javax.swing.JButton Start;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration

}
public class Server
{
public static void main(String args[])
{
Ser ob=new Ser();
Thread th=new Thread(ob);
th.start();
}
}
+Pie Number of slices to send: Send
plzzz rply frds....
+Pie Number of slices to send: Send
as this is your stated problem
> how can i call a method using thread which hve to run till da prgm exit.

forget your program for the moment, and just write a sample program,
jframe with a Jbutton and actionlistener to do your thread stuff (can be just a System.out.println)

have a go, and if you can't work it out, after visiting the thread tutorials and/or googling "new Thread",
post your sample program here, so we can help you work it out. Then you should be able to adapt the
same principles to your main program.

when posting code - use the code button (paste your code between the tags)
+Pie Number of slices to send: Send
And please Use Real Words.

"frds", "da prgm", "Pgm", "plzzz", "rply"... they are not quite easily read. I for one am not even sure what "frds" means. I'm guessing it's "friends".
What's a year in metric? Do you know this metric stuff tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1293 times.
Similar Threads
How to activate threads in Swings.
Pressing a button to open a new window
Networked Programs that work across countries
Problem adding JComboBox to JFrame
JTable Problem with netbeans
Need help populating a JList in NetBeans
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:58:42.