5

I'm trying to create a GUI using Java by hand-coding it (without using GUI tools).

I'm trying to create something like the picture below, but it's not coming out as I want it. (It was created by a mockup application called Pencil)

enter image description here

This is the code so far:

import java.awt.*; 
import javax.swing.*;
import javax.swing.JTable;


public class GUI extends JFrame {

    public void buildGui() {


        JFrame frame = new JFrame("Hotel TV Scheduler");    

                Container contentPane = frame.getContentPane();
                contentPane.setLayout(new FlowLayout());

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

                JPanel listPanel = new JPanel();
                listPanel.setLayout(new FlowLayout());

                JTable chOneTable = new JTable();
                JTable chTwoTable = new JTable();
                JTable listTable = new JTable();

                JButton rmvChOneButton = new JButton("Remove Channel");
                JButton rmvChTwoButton = new JButton("Remove Channel");

                listPanel.add(chOneTable);
                listPanel.add(chTwoTable);
                listPanel.add(listTable);
                listPanel.add(rmvChOneButton);
                listPanel.add(rmvChTwoButton);

                JPanel infoPanel = new JPanel();
                infoPanel.setLayout(new GridLayout());

                JLabel titleLabel = new JLabel("Title");
                JLabel genreLabel = new JLabel("Genre");
                JLabel durationLabel = new JLabel("Duration");
                JLabel actorLabel = new JLabel("Actor");
                JLabel directorLabel = new JLabel("Director");
                JLabel rentLabel = new JLabel("Rentable");
                JLabel synopsisLabel = new JLabel("Synopsis");

                JTextField txtTitle = new JTextField();
                JTextField txtGenre = new JTextField();
                JTextField txtDuration = new JTextField();
                JTextField txtActor = new JTextField();
                JTextField txtDirector = new JTextField();
                JTextField txtSynopsis = new JTextField();

                JCheckBox rentCB = new JCheckBox();

                infoPanel.add(titleLabel);
                infoPanel.add(txtTitle);
                infoPanel.add(genreLabel);
                infoPanel.add(txtGenre);
                infoPanel.add(durationLabel);
                infoPanel.add(txtDuration);
                infoPanel.add(actorLabel);
                infoPanel.add(txtActor);
                infoPanel.add(directorLabel);
                infoPanel.add(txtDirector);
                infoPanel.add(rentLabel);
                infoPanel.add(rentCB);
                infoPanel.add(synopsisLabel);
                infoPanel.add(txtSynopsis);


                contentPane.add(listPanel);
                contentPane.add(infoPanel);

                frame.setVisible(true);
    }
}

Any idea of what Layouts I could used to create the GUI setup or how to achieve it by coding it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Brian
  • 1,951
  • 16
  • 56
  • 101
  • 1
    What's the reason for not wanting to use a GUI tool? They make life a lot easier. – kevingreen Apr 03 '12 at 18:52
  • 2
    @kevingreen: One great reason is to help one better understand Swing before using the GUI tools. Then if you have trouble solving something using the GUI tool, you'll have a much easier time understanding how to fix it. I strongly encourage the original poster in this endeavor. 1+. And 1+ to kevingreen for his excellent answer below. – Hovercraft Full Of Eels Apr 03 '12 at 19:00
  • @HovercraftFullOfEels I feel the opposite. I think seeing a visual representation of the Swing layout as you build will help Brian see how things interact in a easier fashion than try/compile/error cycles. You can always look under the hood to see the code (Eclipse/Netbeans anyway). Netbeans will allow you to custom code things and instantly render them in the GUI tool as well. But either way, I do encourage the effort too. – kevingreen Apr 03 '12 at 19:03

1 Answers1

3

Check A Visual Guide to Layout Managers. That will help you with layout selection.

Also, think about using panels within panels. Each with the layout needed in order to achieve the look you need. Without a GUI building tool you will need to compile/run/compile/run… to see how things are laying out.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
kevingreen
  • 1,541
  • 2
  • 18
  • 40