0

I am creating a java chat application and am having issues with a TextArea and a list in the client GUI. I am using a cardlayout to have the login on one panel and the chat on another. For some reason though, the TextArea and List are not filling up the center and east of the panel respectively leaving a lot of extra space below them all the way to the button panel I have.

I've tried to mess with the borderlayout for both the TextArea and the list but no success.

Id appreciate any help.

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;

public class ChatFrame extends Frame{
    public ChatFrame(){
        //setTitle("Chat Frame");
        setSize(700,700);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we){
                System.exit(0);
            }
        });
        add(new ChatPanel(), BorderLayout.CENTER);
        setVisible(true);
    }
    public static void main(String[] args){
        ChatFrame ch = new ChatFrame();
    }
}
class ChatPanel extends Panel implements ActionListener, Runnable{

    Button connect;
    Button disconnect;
    int currentCard = 1;
    CardLayout cl;
    Panel cardPanel;
    TextField tf2;
    String name;
    List l1 = new List();
    TextField tf;
    TextArea ta;
    Socket s;
    BufferedReader in;
    PrintWriter out;
    Thread t;

    public ChatPanel(){
        setLayout(new BorderLayout());
        cardPanel = new Panel();
        cl = new CardLayout();
        cardPanel.setLayout(cl);

        tf = new TextField("                                                     ");
        tf.addActionListener(this);
        add(tf, BorderLayout.NORTH);
        ta = new TextArea();
        add(ta, BorderLayout.CENTER);

        connect = new Button("Connect");
        connect.addActionListener(this);
        disconnect = new Button ("Disconnect");
        disconnect.addActionListener(this);
        disconnect.setEnabled(false);
        Panel buttonPanel = new Panel();
        buttonPanel.add(connect);
        buttonPanel.add(disconnect);
        add(buttonPanel, BorderLayout.SOUTH);

        l1.addActionListener(this);
        //l1.setSize(new Dimension(150, 400));
        add(l1,BorderLayout.EAST);

        Panel p1 = new Panel();
        Panel p2 = new Panel();
        tf2 = new TextField("          ");
        tf2.addActionListener(this);
        add(tf2, BorderLayout.NORTH);
        p1.add(tf2);
        p2.add(tf);
        p2.add(ta);
        p2.add(l1);
        cardPanel.add(p1, BorderLayout.CENTER);
        cardPanel.add(p2, BorderLayout.CENTER);
        add(cardPanel, BorderLayout.CENTER);

    }
    public void actionPerformed(ActionEvent ae){
        if((ae.getSource()==connect)){
            try{
            s = new Socket("localhost", 3000);
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new PrintWriter(s.getOutputStream(), true);
            t = new Thread(this,"Whatever");
            t.start();
        }catch(UnknownHostException uhe){
            System.out.println(uhe.getMessage());
        }catch(IOException ioe){
            System.out.println(ioe.getMessage());
        }

        connect.setEnabled(false);
        disconnect.setEnabled(true);
        cl.last(cardPanel);
        name = tf2.getText();
        out.println(name + " has entered the chat");
        ChatFrame ch = (ChatFrame) SwingUtilities.getWindowAncestor(this);
        ch.setTitle(name);
        //l1.add(name);
        }

        else if(ae.getSource()== disconnect){
            out.println(name + " has left the chat");
            try{
            s.close();
            }catch(IOException ioe){
                System.out.println(ioe.getMessage());
            }
            disconnect.setEnabled(false);
            connect.setEnabled(true);
            cl.first(cardPanel);
        }
        else if (ae.getSource()== tf){
            String temp = tf.getText();
            tf.setText("");
            out.println(name + ": " + temp);

        }

    }
    public void run(){
        try{
            for(;;){
                ta.append(in.readLine() + "\n");
            }
        }catch(IOException ioe){
            System.out.println(ioe.getMessage());
        }
    }
}
1
  • Why use AWT components? See this answer for many good reasons to abandon AWT components in favor of Swing. Commented Aug 5, 2019 at 12:54

1 Answer 1

1

This section of code is incorrect.

    add(l1,BorderLayout.EAST);

    Panel p1 = new Panel();
    Panel p2 = new Panel();
    tf2 = new TextField("          ");
    tf2.addActionListener(this);
    add(tf2, BorderLayout.NORTH);
    p1.add(tf2);
    p2.add(tf);
    p2.add(ta);
    p2.add(l1);

A component (l1) can only have a single parent container. Try adding a background color to each panel to more easily check that components are added to a container with the expected layout.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.