JFrame Hanging When Called From Another Class
hi I am having trouble with JFrame. I have 3 classes:
1. ServerGui - This acts as a login gui, when details are entered it then initialises the class TheServer.
2. TheServer - This class the initiallises the class ServerGuiReal amongst other things.
3. ServerGuiReal acts as the gui for any information created in TheServer class.
The problem is that when I initialise the class called ServerGuiReal from the class TheServer, the JFrame i built in ServerGuiReal hangs i.e It just shows the the frame but doesn't display any of its contents and the x button in the top right does not work either even though i have setDefaultCloseOperation(JFrame.Exit_On_Close);
My wild guess is that alll the resources have not been released from the class ServerGui or something like that. Or maybe it has something to do with the inner class called Listener inside the ServerGui class. But ive really no idea at all to be honest.
Was wondering if anybody can see any problems in my code. Thanks in advance
TheServerGui class:
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
public class ServerGui {
private JFrame servFrame;
private ImagePanel servPanel;
private JPanel pan1;
private JPanel pan2;
private JButton myButton;
private JButton exit;
private JLabel myLabel;
private JTextField userName;
private JPasswordField password;
private JTextField port;
private File img;
private GridBagConstraints c;
public ServerGui()
{
c=new GridBagConstraints();
userName=new JTextField(20);
password=new JPasswordField(20);
port=new JTextField(20);
img=new File("images\\backg.gif");
servFrame=new JFrame("Server Login");
servPanel=new ImagePanel(img);
pan1=new JPanel();
pan1.setLayout(new GridLayout(3,2,5,5));
pan1.setBackground(new Color(0,0,0,0));
pan2=new JPanel();
pan2.setLayout(new GridLayout(1,3,5,5));
pan2.setBackground(new Color(0,0,0,0));
servPanel.setLayout(new GridBagLayout());
servPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
c.insets=new Insets(10,10,10,10);
servFrame.getContentPane().add(servPanel,BorderLayout.CENTER);
myButton=new JButton("Login");
exit=new JButton("Exit");
pan1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.GRAY,Color.BLACK));
pan1.add(myLabel=new JLabel("Enter Username",JLabel.TRAILING));
myLabel.setForeground(Color.WHITE);
pan1.add(userName);
pan1.add(myLabel=new JLabel("Enter Password",JLabel.TRAILING));
myLabel.setForeground(Color.WHITE);
pan1.add(password);
pan1.add(myLabel=new JLabel("Enter Port",JLabel.TRAILING));
myLabel.setForeground(Color.WHITE);
pan1.add(port);
pan2.add(exit);
pan2.add(myButton);
c.gridx=0;
c.gridy=0;
servPanel.add(pan1,c);
c.gridy=1;
c.gridx=0;
c.anchor=GridBagConstraints.FIRST_LINE_END;
servPanel.add(pan2,c);
servFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
servFrame.setSize(500,300);
exit.addActionListener(new Listener());
myButton.addActionListener(new Listener());
servFrame.setVisible(true);
}
public void runServer(int port)
{
servFrame.dispose();
new TheServer(port);
}
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Exit"))
{
servFrame.dispose();
}
else if(ae.getActionCommand().equals("Login"))
{
//need to check if password is good before proceeding
int newPort=Integer.parseInt(port.getText());
runServer(newPort);
}
}
}
}
TheServer class
Code:
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class TheServer {
private ArrayList<Socket>list;
private ArrayList<String>userNames;
private Socket client;
private ServerSocket server;
private int port;
private ServerGuiReal gui;
private String info;
private String waitMsg;
public TheServer(int port)
{
runGui();
list=new ArrayList<Socket>();
userNames=new ArrayList<String>();
waitMsg="Waiting for Connection on Port: "+port;
this.port=port;
try{
server=new ServerSocket(port);
while(true)
{
client=server.accept();
list.add(client);
info="Connection made on Port: "+port+"\n"+
"Connection made from: "+client.getInetAddress().getHostAddress()+"\n";
}
}catch(IOException e)
{
e.printStackTrace();
}
}
public void runGui()
{
gui=new ServerGuiReal(waitMsg);
}
}
ServerGuiReal class
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.io.File;
import javax.swing.*;
public class ServerGuiReal {
private String info;
private JFrame frame;
private JTextArea area;
private JScrollPane scroll;
private ImagePanel pan;
private File img;
public ServerGuiReal(String info)
{
this.info=info;
frame=new JFrame("Server");
img=new File("images\\backg.gif");
pan=new ImagePanel(img);
pan.setLayout(new GridBagLayout());
area=new JTextArea();
area.setLineWrap(true);
area.setEditable(false);
scroll=new JScrollPane(area);
scroll.setPreferredSize(new Dimension(300,400));
pan.add(scroll);
frame.getContentPane().add(pan);
frame.setSize(500,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//append the first server message to area i.e waiting for connection
area.append(info+"\n");
}
public void setDetails(String details)
{
area.append(details+"\n");
}
}