We help IT Professionals succeed at work.
Get Started

How to use Grouplayout in Java

P-R-W
P-R-W asked
on
1,457 Views
Last Modified: 2012-05-11
Hello,
On the website from Oracle, there is an example how to use the GroupLayout manager: http://download.oracle.com/javase/tutorial/uiswing/examples/layout/FindProject/src/layout/Find.java
When I run this one in Netbeans it runs fine, but when I want to use it within my program I get a NullPointerException. What I do is, from a menu from another frame I start a new frame with:

Settings settingsFrame = new Settings("Settings");

The Settings class is attached.
What do I forget or what do I do wrong?
Thanks
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.awt.event.*;
import java.awt.Component;
import javax.swing.*;
import javax.swing.GroupLayout.*;

public class Settings extends JFrame {
  
  private Alignment LEADING;
  private Alignment BASELINE;

  public Settings(String titel) {
    super(titel);
    setVisible(true);
    addWindowListener(new VensterHandler());

      JLabel label = new JLabel("Find What:");
      JTextField textField = new JTextField();
      JCheckBox caseCheckBox = new JCheckBox("Match Case");
      JCheckBox wrapCheckBox = new JCheckBox("Wrap Around");
      JCheckBox wholeCheckBox = new JCheckBox("Whole Words");
      JCheckBox backCheckBox = new JCheckBox("Search Backwards");
      JButton findButton = new JButton("Find");
      JButton cancelButton = new JButton("Cancel");

      // remove redundant default border of check boxes - they would hinder
      // correct spacing and aligning (maybe not needed on some look and feels)
      caseCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      wrapCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      wholeCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      backCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

        try {
                    UIManager.setLookAndFeel(
                                  "javax.swing.plaf.metal.MetalLookAndFeel");
                                //  "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                                //UIManager.getCrossPlatformLookAndFeelClassName());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }


        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(layout.createSequentialGroup()
            .addComponent(label)
            .addGroup(layout.createParallelGroup(LEADING)
                .addComponent(textField)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(caseCheckBox)
                        .addComponent(wholeCheckBox))
                    .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(wrapCheckBox)
                        .addComponent(backCheckBox))))
            .addGroup(layout.createParallelGroup(LEADING)
                .addComponent(findButton)
                .addComponent(cancelButton))
        );

        layout.linkSize(SwingConstants.HORIZONTAL, findButton, cancelButton);

        layout.setVerticalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(BASELINE)
                .addComponent(label)
                .addComponent(textField)
                .addComponent(findButton))
            .addGroup(layout.createParallelGroup(LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(BASELINE)
                        .addComponent(caseCheckBox)
                        .addComponent(wrapCheckBox))
                    .addGroup(layout.createParallelGroup(BASELINE)
                        .addComponent(wholeCheckBox)
                        .addComponent(backCheckBox)))
                .addComponent(cancelButton))
        );

        setTitle("Find");

    pack();
   
    
  }

  private class VensterHandler extends WindowAdapter {

    public void windowClosing(WindowEvent e) {
      dispose();
    }
  }
}

Open in new window

Comment
Watch Question
Awarded 2011
Awarded 2011
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE