Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Socket not allowing swing frame to show up

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java Socket not allowing swing frame to show up

    Hey all I am at a loss as to why its doing this. If I just run the server and standalone client it works just fine. However, once I use my code for the client it seems to get stuck...
    static JTextField textField         = null;
    static JTextArea messageArea        = null;
    static String serverAddress         = "localhost";
    static UFTtrack window              = null;
     
    public static JFrame frame;
    public ImageIcon[] images;
    static JTable table;
    Date lastUpdate;
    static Timer timer;
    static Scanner in;
    static PrintWriter out;
     
    public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @SuppressWarnings({ "static-access" })
                public void run() {
                    try {
                        UIManager.setLookAndFeel(new MaterialLookAndFeel());
                    } catch (UnsupportedLookAndFeelException e1) {
                        e1.printStackTrace();
                    }
     
                    try {
                        window = new UFTtrack();
     
                        placeChatOnScreen();
                        createTable();
                        SystemTrayz.createTray();
     
                        centreWindow(frame);
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
     
    public UFTtrack() {
       initialize();
    }
     
    private void initialize() {
            frame = new JFrame("UFTtrack");
            frame.setTitle("UFT Tracker");
            frame.setResizable(false);
            frame.setLocationByPlatform(true);
            frame.setSize(1308, 900);
            frame.setBounds(100, 100, 450, 300);
            frame.setMinimumSize(new Dimension(1308, 900));
            frame.setPreferredSize(new Dimension(1308, 900));
            frame.setLayout(null);
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        }
     
    @SuppressWarnings({ "resource", "unused" })
    private static void placeChatOnScreen() {
        try {                       
            textField = new JTextField();
            textField.setFont(new Font("Segoe UI", Font.PLAIN, 13));
            textField.setDragEnabled(true);
            textField.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
            textField.setBounds(338, 838, 954, 22);
            frame.getContentPane().add(textField);
     
            messageArea = new JTextArea();
            messageArea.setEditable(false);
            messageArea.setFont(new Font("Segoe UI", Font.PLAIN, 13));
            messageArea.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
            messageArea.setDragEnabled(true);
            messageArea.setName("chatArea");
            messageArea.setWrapStyleWord(true);
            messageArea.setBounds(338, 648, 954, 181);
            frame.getContentPane().add(messageArea);
     
            textField.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    out.println(textField.getText());
                    textField.setText("");
                }
            });
     
            Socket socket = new Socket("localhost", 8877);
     
            in = new Scanner(socket.getInputStream());
            out = new PrintWriter(socket.getOutputStream(), true);
     
            while (in.hasNextLine()) {
                String line = in.nextLine();
     
                if (line.startsWith("SUBMITNAME")) {
                    out.println(getName());
                } else if (line.startsWith("NAMEACCEPTED")) {
                    textField.setEditable(true);
                } else if (line.startsWith("MESSAGE")) {
                    messageArea.append(line.substring(8) + "\n");
                }
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    It does fine until it gets to the "NAMEACCEPTED" else if condition and it just steps out of the while loop and then nothing happens. No error of any kind. It just doesn't how the swing frame!
    If I comment out:
    /*while (in.hasNextLine()) {
        String line = in.nextLine();
     
        if (line.startsWith("SUBMITNAME")) {
            out.println(getName());
        } else if (line.startsWith("NAMEACCEPTED")) {
            textField.setEditable(true);
        } else if (line.startsWith("MESSAGE")) {
            messageArea.append(line.substring(8) + "\n");
        }
    }*/
    and run it the swing frame loads up just fine. But my Socket (which I am wanting in my swing app) keeps it for some reason passing.
    window = new UFTtrack();
     
    placeChatOnScreen();
    createTable();
    SystemTrayz.createTray();
     
    centreWindow(frame);
    window.frame.setVisible(true);
    To sum all this up - the above code hits the window = new UFTtrack(); and placeChatOnScreen() but after it exits the While loop in the placeChatOnScreen() it never continues to createTable(); What's the deal???

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Socket not allowing swing frame to show up

    See my comment here: https://www.dreamincode.net/forums/t...me-to-show-up/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JLabel Will Not Show Up In Frame
    By rmorris7 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2014, 04:15 PM
  2. New frame should only show GUI once!
    By gerre in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 30th, 2012, 10:34 PM
  3. Control web page from java swing frame
    By gafaec in forum AWT / Java Swing
    Replies: 2
    Last Post: August 16th, 2011, 08:57 AM
  4. IE browser in Java swing/AWT Frame
    By gafaec in forum AWT / Java Swing
    Replies: 0
    Last Post: April 10th, 2011, 05:16 AM
  5. IE browser in Java swing/AWT Frame
    By gafaec in forum Java Native Interface
    Replies: 0
    Last Post: April 9th, 2011, 05:16 AM