Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:04:03+00:00 2026-06-06T15:04:03+00:00

I created a chat panel and added to Jframe but the panel is not

  • 0

I created a chat panel and added to Jframe but the panel is not displaying. But my sop in the chat panel are displaying in the console. Any one please let me know what could be the problem

My Frame

public class MyFrame extends JFrame {

MyPanel chatClient;
  String input;

public MyFrame() {

    input = (String)JOptionPane.showInputDialog(null, "Name:", "Connect to chat                  
server", JOptionPane.QUESTION_MESSAGE, null,null, "Test");
input=input.trim();
chatClient = new MyPanel("localhost",input);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(chatClient);

 }
  public static void main(String...args){
    new MyFrame();
  }
}

MyPanel:

public class MyPanel extends JPanel{
  ChatClient chatClient;

  public MyPanel(String host, String uid) {
    chatClient= new ChatClient(host,uid);
    add(chatClient.getChatPanel());
    this.setVisible(true);
  }
}

chat panel:

public class ChatClient  {

  Client client;
  String name;
  ChatPanel chatPanel;
  String hostid;

  public ChatClient(String host,String uid){
    client = new Client();
    client.start();
    System.out.println("in constructor");
    Network.register(client);

    client.addListener(new Listener(){

      public void connected(Connection connection){
        System.out.println("in client connected method");
        Network.RegisterName registerName = new Network.RegisterName();
        registerName.name=name;
        client.sendTCP(registerName);
      }

      public void received(Connection connection,Object object){
        System.out.println("in client received method");

          if (object instanceof Network.UpdateNames) {
                  Network.UpdateNames updateNames = (Network.UpdateNames)object;
                  //chatFrame.setNames(updateNames.names);

                  System.out.println("got it message");
                  return;
          }

          if (object instanceof Network.ChatMessage) {
                  Network.ChatMessage chatMessage = (Network.ChatMessage)object;
                  //chatFrame.addMessage(chatMessage.text);
                    System.out.println("send it message");
                  return;
          }

      }



    }); // end of listner

    name=uid.trim();
    hostid=host.trim();
    chatPanel = new ChatPanel(hostid,name);

    chatPanel.setSendListener(new Runnable(){

      public void run(){
        Network.ChatMessage chatMessage = new Network.ChatMessage();
        chatMessage.chatMessage=chatPanel.getSendText();
        client.sendTCP(chatMessage);
      }
    });

    new Thread("connect"){
      public void run(){
        try{
          client.connect(5000, hostid,Network.port);
        }catch(IOException e){
          e.printStackTrace();
        }
      }
    }.start();

  }//end of constructor

  static public class ChatPanel extends JPanel{

    CardLayout cardLayout;
    JList messageList,nameList;
    JTextField sendText;
    JButton sendButton;
    JPanel topPanel,bottomPanel,panel;

    public ChatPanel(String host,String user){
      setSize(600, 200);
      this.setVisible(true);
      System.out.println("Chat panel "+host+"user: "+user);
      {
        panel = new JPanel(new BorderLayout());
        {
          topPanel = new JPanel(new GridLayout(1,2));
          panel.add(topPanel); 
          {
            topPanel.add(new JScrollPane(messageList=new JList()));
            messageList.setModel(new DefaultListModel());
          } 
          {
            topPanel.add(new JScrollPane(nameList=new JList()));
            nameList.setModel(new DefaultListModel());
          }
          DefaultListSelectionModel disableSelections = new DefaultListSelectionModel() {
                                                          public void setSelectionInterval (int index0, int index1) {
                                                          }
                                                  };

          messageList.setSelectionModel(disableSelections);
          nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        } 
        {
          bottomPanel = new JPanel(new GridBagLayout());
          panel.add(bottomPanel,BorderLayout.SOUTH);

          bottomPanel.add(sendText=new JTextField(),new GridBagConstraints(0,0,1,1,1,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
          bottomPanel.add(sendButton=new JButton(),new GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.CENTER,0,new Insets(0,0,0,0),0,0));
        }

      }

      sendText.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
          sendButton.doClick();
        }
      });

    }


    public void setSendListener (final Runnable listener) {
            sendButton.addActionListener(new ActionListener() {
                    public void actionPerformed (ActionEvent evt) {
                            if (getSendText().length() == 0) return;
                            listener.run();
                            sendText.setText("");
                            sendText.requestFocus();
                    }
            });
    }

    public String getSendText () {
            return sendText.getText().trim();
    }

    public void setNames (final String[] names) {
      EventQueue.invokeLater(new Runnable(){
        public void run(){
          DefaultListModel model = (DefaultListModel)nameList.getModel();
          model.removeAllElements();
          for(String name:names)
            model.addElement(name);
        }
      });

    }

    public void addMessage (final String message) {
            EventQueue.invokeLater(new Runnable() {
                    public void run () {
                            DefaultListModel model = (DefaultListModel)messageList.getModel();
                            model.addElement(message);
                            messageList.ensureIndexIsVisible(model.size() - 1);
                    }
            });
    }

  }

  public JPanel getChatPanel(){
    return chatPanel;
  }

}

EDIT 1

public class ChatPanel {
  ChatP caht;
  public ChatPanel1() {
    caht=new ChatP();
  }
  static class ChatP extends JPanel{
    JPanel panel;
    public ChatP(){
      panel = new JPanel();
      panel.add(new JLabel("hi from chat panel"));
    }
  }

  public JPanel getChatPanel(){
    return caht;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:04:05+00:00Added an answer on June 6, 2026 at 3:04 pm

    Call pack(). The cobbled together code still has many problems, and odd aspects, but shows..

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    
    public class MyFrame extends JFrame {
    
        MyPanel chatClient;
        String input;
    
        public MyFrame() {
            chatClient = new MyPanel("localhost","input");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            add(chatClient);
    
            pack();
            // Do last!
            setVisible(true);
        }
        public static void main(String...args){
            new MyFrame();
        }
    }
    
    class MyPanel extends JPanel{
    
        public MyPanel(String host, String uid) {
            ChatPanel chatPanel = new ChatPanel();
            add(chatPanel.getChatPanel());
        }
    }
    
    class ChatPanel {
        ChatP caht;
    
        public ChatPanel() {
            caht=new ChatP();
        }
    
        static class ChatP extends JPanel{
            public ChatP(){
                add(new JLabel("hi from chat panel"));
            }
        }
    
        public JPanel getChatPanel(){
            return caht;
        }
    }
    

    Notes

    Batteries not included. That source was the minimalist code needed to diagnose the problem and suggest a solution. For better help sooner, post an SSCCE.

    • Don’t extend frame or panel, simply keep a reference to each.
    • Start and update GUIs on the EDT. See Concurrency in Swing for more details.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a chat application. But my problem is that I don't know how
I am creating an asynchronous chat application in C. I created two threads, one
I have created a chat application using PHP, but it works only into my
I have created simple chat application using smack api.i can able to chat well.but
There i have created a chat app, all is working fine. but there is
I am trying to create a chat application in .net but I am confused
I created a chat application and seems to work just fine except that it
I am trying to build a bluetooth chat application using j2me. I created a
I’ve created my own plugin architecture based on the common practices but I’m stuck
this is my first time using any StackExchange website, so let's see how it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.