I'm trying to make family tree webapplet, to test it out outside of the site I just used JFrame.
I've used GroupLayout to set up things, but I'd like to have a fixed size, now it just takes up the size of the JFrame.
NewClass.Java

/*
 * NewJPanel.java
 *
 * Created on 13-dec-2010, 10:30:17
 */

package Familytree;


public class NewClass extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public NewClass() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();

        jMenuItem1.setText("Add new person");
        jPopupMenu1.add(jMenuItem1);

        setMaximumSize(new java.awt.Dimension(400, 120));
        setPreferredSize(new java.awt.Dimension(380, 80));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        jLabel1.setText("Name:");

        jLabel2.setText("Date of Birth:");

        jLabel3.setText("Gender:");

        jLabel4.setText("Date of Deceased:");

        jTextField1.setToolTipText("Type your name here.");
        jTextField1.setMinimumSize(new java.awt.Dimension(50, 20));

        jTextField2.setToolTipText("Type in the date of birth.");
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jTextField3.setToolTipText("Type in the date of deceased.");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox1, 0, 107, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTextField2)
                    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTextField2.getAccessibleContext().setAccessibleName("dd-mm-yyyy");
        jTextField3.getAccessibleContext().setAccessibleName("dd-mm-yyyy");
    }// </editor-fold>

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void formMouseReleased(java.awt.event.MouseEvent evt) {
        if(evt.isPopupTrigger()){

        }
}


    // Variables declaration - do not modify
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration

}

MainForm.Java

package Familytree;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class MainForm extends JPanel {

    public void makeGUI(){
        JFrame testFrame = new JFrame("Test Frame");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testFrame.setSize(800, 800);
        testFrame.setLocationRelativeTo(null);
        testFrame.setVisible(true);
        testFrame.getContentPane().add(this);
        testFrame.getContentPane().add(new NewClass());
    }

    public static void main(String [] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MainForm mainForm = new MainForm();

                    mainForm.makeGUI();
            }
        });
    }

}

Recommended Answers

All 6 Replies

NewClass: set same Dimension for minimum/maximum/preferred size of JPanel
MainForm: no need extends JPanel, no need setSize
for testFrame use GridLayout and pack() method

NewClass: set same Dimension for minimum/maximum/preferred size of JPanel

Didn't I do that correct at line 40/41? or do I also need to set the minimum size?

for testFrame use GridLayout and pack() method

Can you please explain what GridLayout will do and how to use it?

I mean, I know what GridLayout is/what it does and how to implement it.
But what will/should it do in this occasion?

Sorry for the "stupid?" questions hehe, I'm trying to understand what I'm doing.

Avoid GUI builders. They generate human beings hardly understandable code.
We can fix error(s) in the code YOU wrote but we cannot be behind you when you correctly/uncorrectly use the features of a GUI generator.
Swing is really easy to use and when you will pass a Java interview and they will ask you to write a very simple GUI using Notepad what will you do ?

Avoid GUI builders. They generate human beings hardly understandable code.
We can fix error(s) in the code YOU wrote but we cannot be behind you when you correctly/uncorrectly use the features of a GUI generator.
Swing is really easy to use and when you will pass a Java interview and they will ask you to write a very simple GUI using Notepad what will you do ?

Normally I write my own codes, but this time I wanted to see what code the GUI builder would generate, so that I might be able to get some idea's from it.

"Normally I write my own codes, but this time I wanted to see what code the GUI builder would generate, so that I might be able to get some idea's from it"

Good luck. You will see that human generated code should take about the 1/10 the numnber of lines of code that the generator will use.

The generator is not as lazy as you are. It can generate 10 lines to generate what you can do in 1 line. It is like a Sudoku solver that will always use the "brute force" algorithm. It works but haqrdly understandable by a human being.

Ok so I managed to edit the JPanel size(somewhat...)
But the thing is that the JFrame size is is changing to the size of the JPanel, but I want the JPanel size for example (20, 20), and the JFrame size (80, 90).

Here is what it looks like now:
http://img715.imageshack.us/img715/5585/10518855.png

And here is what I want it to look like eventually:
(note: the black squares are all JPanels;))
http://img156.imageshack.us/img156/2794/68114223.png

or

http://img24.imageshack.us/img24/8083/89h4ubihw.png

Here is the edited code:
Person.Java

package Familytree;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public final class Person extends JPanel {

    public Person() {
        initComponents();
    }

    public void initComponents(){

    JTextField nameTextField;
    JTextField dateOfBirthTextField;
    JComboBox genderComboBox;
    JTextField dateOfDeceasedTextField;

    JLabel nameLabel;
    JLabel dateOfBirthLabel;
    JLabel genderLabel;
    JLabel dateOfDeceasedLabel;

    setPreferredSize(new Dimension(380, 80));
    setMaximumSize(new Dimension(380, 80));
    setMinimumSize(new Dimension(380, 80));
    setBorder(BorderFactory.createLineBorder(Color.black));
    
    /*
     * Initializing and declaring the labels.
     */
    nameLabel = new JLabel();
    nameLabel.setText("Name:");

    dateOfBirthLabel = new JLabel();
    dateOfBirthLabel.setText("Date of Birth:");

    genderLabel = new JLabel();
    genderLabel.setText("Gender:");

    dateOfDeceasedLabel = new JLabel();
    dateOfDeceasedLabel.setText("Date of Deceased:");

    /*
     * Declaring the name textfield, adding a preffered size and tool tip text.
     */
    nameTextField = new JTextField();
    nameTextField.setSize(20, 50);
    nameTextField.setToolTipText("Type in the full name");

    /*
     * Declaring the date of birth textfield, adding a preffered size and tool tip text.
     * Date of deceased should be entered as follows: dd-mm-yyyy.
     */
    dateOfBirthTextField = new JTextField();
    dateOfBirthTextField.setSize(20, 50);
    dateOfBirthTextField.setToolTipText("Type in the date of birth. (dd-mm-yyyy)");

    /*
     * Declaring the gender combo box, possibilities to choose from are: Male/Female.
     * Added preferred size of the combo box.
     */
    String[] gender = { "Male", "Female" };
    genderComboBox = new JComboBox(gender);
    genderComboBox.setSize(20, 50);

    /*
     * Declaring the date of deceased textfield, adding a preffered size and tool tip text.
     * Date of deceased should be entered as follows: dd-mm-yyyy.
     */
    dateOfDeceasedTextField = new JTextField();
    dateOfDeceasedTextField.setSize(20, 50);
    dateOfDeceasedTextField.setToolTipText("Type in the date of deceased. (dd-mm-yyyy)");


    GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(
                layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(nameLabel)
                .addComponent(genderLabel))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(nameTextField, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                    .addComponent(genderComboBox, 0, 107, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(dateOfBirthLabel)
                    .addComponent(dateOfDeceasedLabel))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(dateOfBirthTextField, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                    .addComponent(dateOfDeceasedTextField, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
        );
        
        layout.setVerticalGroup(
                layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE )
                    .addComponent(dateOfBirthTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE )
                    .addComponent(dateOfBirthLabel))
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(genderLabel)
                    .addComponent(genderComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(dateOfDeceasedTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(dateOfDeceasedLabel))
        );
    }
}

MainForm.Java

package Familytree;

import java.awt.GridLayout;
import javax.swing.JFrame;

public class MainForm {

    public void makeGUI(){
        JFrame testFrame = new JFrame("Test Frame");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testFrame.setSize(800, 800);
        testFrame.setLocationRelativeTo(null);
        testFrame.setVisible(true);
        testFrame.getContentPane().add(new Person());
        testFrame.getContentPane().add(new Person2());
        testFrame.setLayout(new GridLayout());
        testFrame.pack();
    }

    public static void main(String [] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MainForm mainForm = new MainForm();

                    mainForm.makeGUI();
            }
        });
    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.