Monday, August 31, 2009

How to use GridLayout

Java Swing Tutorial Explaining the GridLayout. GridLayout is a layout manager that lays out a container’s components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle.

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

public class GridLayoutDemo {
public final static boolean RIGHT_TO_LEFT = false;

public static void addComponentsToPane(Container contentPane) {
if (RIGHT_TO_LEFT) {
contentPane.setComponentOrientation(
ComponentOrientation.RIGHT_TO_LEFT);
}
// Any number of rows and 2 columns
contentPane.setLayout(new GridLayout(0,2));

contentPane.add(new JLabel("JLabel 1"));
contentPane.add(new JButton("JButton 2"));
contentPane.add(new JCheckBox("JCheckBox 3"));
contentPane.add(new JTextField("Long-Named JTextField 4"));
contentPane.add(new JButton("JButton 5"));
}

private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("GridLayout Source Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the content pane and components in GridLayout
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}

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

Swing Validation Package with InputVerifier

Although there are multiple frameworks out there for validating user input in Swing, there is also an API in Swing called InputVerifier which is very easy to use, and extremely easy to customize.


import javax.swing.BorderFactory;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

public class InputVerifierExample extends JPanel {
private JLabel validationLabel;

public InputVerifierExample() {
DefaultFormBuilder formBuilder = new DefaultFormBuilder(new FormLayout("right:pref, 3dlu, p:g"));
formBuilder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JTextField javaField = new JTextField();
JTextField swingField = new JTextField();
this.validationLabel = new JLabel();

javaField.setInputVerifier(new StrictInputVerifier("Java"));
swingField.setInputVerifier(new StrictInputVerifier("Swing"));

formBuilder.append("Java Field:", javaField);
formBuilder.append("Swing Field:", swingField);
formBuilder.appendParagraphGapRow();
formBuilder.append(validationLabel, 3);

add(formBuilder.getPanel());
}

private class StrictInputVerifier extends InputVerifier {
private String validString;

public StrictInputVerifier(String validString) {
this.validString = validString;
}

public boolean verify(JComponent input) {
JTextField textField = (JTextField) input;
if (validString.equals(textField.getText())) {
validationLabel.setText("");
return true;
} else {
validationLabel.setText("Field must only contain " + this.validString);
return false;
}
}
}

public static void main(String[] a){
JFrame f = new JFrame("Input Verifier Example");
f.setDefaultCloseOperation(2);
f.add(new InputVerifierExample());
f.pack();
f.setVisible(true);
}
}
 

About

Site Info

Information Source

Academy of JAVA by Rajesh Rolen Copyright © 2009 Community is Developed by Rajesh Kumar Rolen WebSite

/* tracking code by yahoo login */