7 Replies - 284 Views - Last Post: 20 August 2019 - 09:36 AM Rate Topic: -----

#1 waj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-August 19

Java Chat Program, GUI problems

Posted 20 August 2019 - 06:01 AM

hello

I have this chat application (for purposes of learning and applying)
it has client and server

sometimes I open the client and the Jtextfield is lost from the window
sometimes it repeats the JOptionPane input dialog (for the IP)
and sometimes it repeats the JOptionPane input dialog for handle name

sometimes similar thing happens with the server (the Jtextfield lost, repetition of handle input dialog)


I understand my code is a mess, but I'm trying different styles (or patterns, or whatever) to develop the techique best for me when programming. Please comment on that if you find it useful!

I am thinking of having a mediator class that handles the code (instead of using the main method (and class) and static variables for that), but I don't wanna recode for that now, as I don't know what works and what not, ...

Chat Client: https://pastebin.com/2uQj2rH8

Chat Server: https://pastebin.com/AVe80dxU

Please note I've asked somewhere else, but got no answers.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Chat Program, GUI problems

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,899
  • Joined: 25-December 13

Re: Java Chat Program, GUI problems

Posted 20 August 2019 - 06:56 AM

Quote

I understand my code is a mess

If you know it is a mess, why not clean it up before asking other people to sort it out?
I personally won't download code from another site. It would be better if the source code was pasted here wrapped in code tags so it can be examined without having to download it.
Was This Post Helpful? 0
  • +
  • -

#3 waj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-August 19

Re: Java Chat Program, GUI problems

Posted 20 August 2019 - 07:32 AM

Well, I thought it was easy to understand for an expert?

the code, without the network module:

Chat Client - no network: https://pastebin.com/sRKQ99bQ

Chat Server - no network: https://pastebin.com/PteXwNKv

It's a simple GUI application this way.
Was This Post Helpful? 0
  • +
  • -

#4 waj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-August 19

Re: Java Chat Program, GUI problems

Posted 20 August 2019 - 07:49 AM

About my code (the GUI only code - without the Networking - which has the problem):

My server and client have same GUI

(a JTextField for chat chat input, a JTextArea for showing conversation, all in a JFrame)

about class 'JTextField_group': contains the JTextField and it's handling function (the listener)

at both apps start, I have showInputDialog, to get "handle" of person.

at Client start, I have showInputDialog, to get IP of server.

F
Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,899
  • Joined: 25-December 13

Re: Java Chat Program, GUI problems

Posted 20 August 2019 - 08:24 AM

Please post the code here on the forum wrapped in code tags. No links.
Was This Post Helpful? 0
  • +
  • -

#6 waj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-August 19

Re: Java Chat Program, GUI problems

Posted 20 August 2019 - 08:29 AM

Server code (only GUI)
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import java.awt.event.*;
import javax.swing.*;
 
import java.util.*;
import java.awt.*;
 
import java.io.*;
import java.net.*;
 
public class Server //change main class name
{
    static JFrame chatFrame = new JFrame("ChatNow Server"); // Create a frame
    static JTextArea JTextArea_group = new JTextArea("Here goes the conversation\n", 15, 80);
    static String serverHandle = JOptionPane.showInputDialog("Please choose your handle");
   
    public static void main(String[] args)
    {
        chatFrame.setSize(900, 400); // Set the frame size
        chatFrame.setLocationRelativeTo(null);
        chatFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        chatFrame.setVisible(true); // Display the frame
 
       
        FlowLayout layoutManager = new FlowLayout(0,10,5);
        chatFrame.setLayout(layoutManager);
 
 
//        JTextArea JTextArea_group = new JTextArea("Here goes the conversation", 15, 80);
        JTextArea_group.setLineWrap(true);
        JTextArea_group.setWrapStyleWord(true);
        JTextArea_group.setEditable(false);
 
        //chatFrame.add(JTextArea_group);
 
 
        /* here add scroll to JTextArea - actually JScrollpane takes JTextArea */        
        JScrollPane scrollPane = new JScrollPane(JTextArea_group);
        chatFrame.add(scrollPane);
 
             
 
        JTextField_group JTF = new JTextField_group();
 
   }
 }
 
class JTextField_group  
{
    JTextField TextFieldChatInput = new JTextField(80);
 
    JTextField_group()
    {    
        Server.chatFrame.add(TextFieldChatInput);                  
        TextFieldChatInput.setText("Does some text show up?");    
 
        TextFieldChatInput.addActionListener(new messageTextInputListener());
    }
 
    class messageTextInputListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            /*
            JOptionPane.showMessageDialog(null, "The text you entered is: " + TextFieldChatInput.getText());
            ChatGUI.JTextArea_group.setText( TextFieldChatInput.getText() );
            */
 
            String IhaveTextOfChatInput = TextFieldChatInput.getText();
            Server.JTextArea_group.append(Server.serverHandle + ": " + "\n");
            Server.JTextArea_group.append(IhaveTextOfChatInput + "\n");
            TextFieldChatInput.setText("");
                       
        }
    }
}



Client code (only GUI)
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import java.awt.event.*;
import javax.swing.*;
 
import java.io.*;
import java.net.*;
 
public class Client //change main class name
{
    static JFrame chatFrame = new JFrame("ChatNow Client"); // Create a frame
    static JTextArea JTextArea_group = new JTextArea("Here goes the conversation\n", 15, 80);
    static String ip = JOptionPane.showInputDialog("Please enter IP Address");
    static String clientHandle = JOptionPane.showInputDialog("Please choose your handle");
   
    public static void main(String[] args)
    {
 
        chatFrame.setSize(900, 400); // Set the frame size
        chatFrame.setLocationRelativeTo(null);
        chatFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        chatFrame.setVisible(true); // Display the frame
 
       
        FlowLayout layoutManager = new FlowLayout(0,10,5);
        chatFrame.setLayout(layoutManager);
 
 
//        JTextArea JTextArea_group = new JTextArea("Here goes the conversation", 15, 80);
        JTextArea_group.setLineWrap(true);
        JTextArea_group.setWrapStyleWord(true);
        JTextArea_group.setEditable(false);
 
        //chatFrame.add(JTextArea_group);
 
 
        /* here add scroll to JTextArea - actually JScrollpane takes JTextArea */        
        JScrollPane scrollPane = new JScrollPane(JTextArea_group);
        chatFrame.add(scrollPane);
 
             
        JTextField_group JTF = new JTextField_group();
 
    }
 }
 
class JTextField_group  
{
    JTextField TextFieldChatInput = new JTextField(80);
 
    JTextField_group()
    {    
        Client.chatFrame.add(TextFieldChatInput);                  
        TextFieldChatInput.setText("Does some text show up?");    
 
        TextFieldChatInput.addActionListener(new messageTextInputListener());
    }
 
    class messageTextInputListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            /*
            JOptionPane.showMessageDialog(null, "The text you entered is: " + TextFieldChatInput.getText());
            ChatGUI.JTextArea_group.setText( TextFieldChatInput.getText() );
            */
 
            String IhaveTextOfChatInput = TextFieldChatInput.getText();
            Client.JTextArea_group.append(Client.clientHandle + ": "  + "\n");
            Client.JTextArea_group.append(IhaveTextOfChatInput + "\n");
            TextFieldChatInput.setText("");
 
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,899
  • Joined: 25-December 13

Re: Java Chat Program, GUI problems

Posted 20 August 2019 - 08:49 AM

The window is set visible before all the components have been added to it.
Move the call to setVisible after the GUI is built.
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,727
  • Joined: 20-September 08

Re: Java Chat Program, GUI problems

Posted 20 August 2019 - 09:36 AM

Your code is very difficult to read. e.g. the variable names look like class names

http://technojeeves....yle-conventions

The most important thing you should realise is that thead synchronization between the gui and network code will be necessary. See javax.swing.SwingWorker
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1