I am not able to load JFrame for multiple clients whereas it works fine for a single client. I am able to perform all the operations on a single client. The frame becomes visible but the contents are not.
Do I need to add something extra? Am I making the correct connections?I am running it on my local machine.
Can I run multiple instances on the same machine?
Server :
public class ServerThread extends Thread{
public static void main(String[] args) throws IOException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(4518);
while(true){
Socket clientSocket = serverSocket.accept();
Thread window=new Frame1();
window.start();
}
} catch (Exception e) {
e.printStackTrace();
}
Client:
public class Client extends JFrame{
public static void main(String arg[]){
String hostName="localhost";
int portNumber=4518;
try{
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
}
JFrame:
public class Frame1 extends Thread{
static JFrame frame;
private static JTextField textField;
private static JPasswordField passwordField;
static Connection conn = null;
static PreparedStatement pst = null;
static ResultSet rs = null;
public Frame1() {
System.out.println("hello");
initialize();
}
private static void initialize() {
frame = new JFrame();
frame.setVisible(true);
frame.setTitle("Course Registration- Spring 2017");
frame.getContentPane().setBackground(new Color(204, 204, 255));
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Sign in");
btnNewButton.setBackground(new Color(204, 102, 153));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
conn = DBConnect.DBConnect();
String sql = "Select * from users where username =? and password=?";
try{
String userName=textField.getText();
String password=passwordField.getText();
pst = conn.prepareStatement(sql);
pst.setString(1, userName);
pst.setString(2, password);
rs = pst.executeQuery();
if(userName.equals("admin") && password.equals("admin")){
ServerSideViews s = new ServerSideViews();
s.setVisible(true);
s.setUserName(userName);
s.setFrameLogin(frame);
System.out.println(userName);
frame.setVisible(false);
}
else if(rs.next() && !(userName.equals("admin"))){
JOptionPane.showMessageDialog(null, "Welcome " + textField.getText() );
CourseRegistration c = new CourseRegistration();
c.setVisible(true);
c.setFrameLogin(frame);
frame.setVisible(false);
String sql1 = "Select major from users where username=?;";
System.out.println(sql1);
pst = conn.prepareStatement(sql1);
pst.setString(1, userName);
rs = pst.executeQuery();
String major="";
while(rs.next())
{
major=rs.getString("major");
}
c.setUserName(userName);
c.comboBox.setSelectedItem(major);
c.comboBox.disable();
}
else{
JOptionPane.showMessageDialog(null, "Invalid user name or password");
}
}catch(Exception e1){
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(157, 182, 117, 29);
frame.getContentPane().add(btnNewButton);
JLabel lblUserName = new JLabel("User Name");
lblUserName.setBounds(76, 96, 86, 16);
frame.getContentPane().add(lblUserName);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(86, 129, 61, 16);
frame.getContentPane().add(lblPassword);
textField = new JTextField();
textField.setBounds(157, 91, 130, 26);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton_1 = new JButton("Register");
btnNewButton_1.setBackground(new Color(51, 102, 204));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UserRegistration reg = new UserRegistration();
reg.setVisible(true);
reg.setFrameLogin(frame);
frame.setVisible(false);
}
});
btnNewButton_1.setBounds(157, 225, 117, 29);
frame.getContentPane().add(btnNewButton_1);
passwordField = new JPasswordField();
passwordField.setBounds(157, 124, 130, 26);
frame.getContentPane().add(passwordField);
JLabel lblNewLabel = new JLabel("Course Registration - Spring 2017");
lblNewLabel.setBounds(109, 24, 241, 16);
frame.getContentPane().add(lblNewLabel);
}
}