I am working on a Java project for college that involves us setting up a TCP Server and Client. I have that part working and now to add more of a feel to my project I want to add a GUI.

We have not begun learning about GUI's in Java yet. However I want to try as I think it would be a useful exercise. I have a very basic GUI set up and the appropriate ActionListener set for the button. My next problem is positioning my panels so they look neat and tidy on the Frame...

At the moment I have all the components in one panel as seen below:

public ClientGUI(){

    //Initialise Frame
    frame = new JFrame("TCP Client");

    //Initialise Panel 1 & Components
    p1 = new JPanel();

    //Set Layout
    p1.setLayout(new GridLayout(1,2));

    //Label 1 - For TextArea
    l1 = new JLabel("Chat Log");

    //TextArea - To display conversation
    t1 = new JTextArea(10,10);

    //Label 2 - For TextField
    l2 = new JLabel("Message");

    //Message Box - For user input
    t2 = new JTextField(10);

    //Button 1 - To send message
    b1 = new JButton("Send");

    //Add panels to frame

    //Frame properties...

    //Add Event listener to button
    b1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ev){
            //do something

I have a wireframe drawn up of what I'd like it to look like but unfortunately I cannot upload as my reputation is not high enough. Basically something like this...

Label on top of a TextArea. Label and TextField below. Button below.

I'd appreciate any feedback anyone might have! Thanks very much.

  • \$\begingroup\$ This question appears to be off-topic because it is primarily a design review. \$\endgroup\$ – Jamal Feb 21 '14 at 18:50

What you want is called the BoxLayout, which feeds UI elements in columns or rows. And then you can nest them one inside another, e.g. have one horizontal box layout panel as an element in another that is vertical (kind of like nested HTML tables). Here is a pretty decent tutorial about layout managers and it includes the BoxLayout.

| improve this answer | |

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