examples
Class Example9
java.lang.Object
examples.Example9
public class Example9
- extends java.lang.Object
This is an example for a GUI from a
tutorial for
an SWT layout manager. Apart from its relative complexity, its main features
are (a) the use of a panel cell for the 'Owner Info' part of
the GUI shown in the figure below, and (b) baseline alignment. Note that the panel cell is
needed only because the GUI requires a panel with a titled border.
// Utility function for creating a JTextField with border
public static JTextField createTextField(String text,int len)
{
JTextField ed=new JTextField(text,len);
ed.setBorder(BorderFactory.createLoweredBevelBorder());
return ed;
}
public static PanelCell ownerInfoPanelCell()
{
// Create the cell for owner info editors
// The panel
JPanel panel=new JPanel();
Border border=BorderFactory.createEtchedBorder();
border=BorderFactory.createTitledBorder(border,"Owner Info");
panel.setBorder(border);
// Create components
JLabel name=new JLabel("Name");
JLabel phone=new JLabel("Phone");
JTextField nameEditor=createTextField("Jane Doe",10);
JTextField phoneEditor=createTextField("555-3245",10);
GridRows rows=new GridRows();
// First row
rows.newRow().add(name,nameEditor);
// First row
rows.newRow().add(phone,phoneEditor);
CellGrid grid=rows.createCellGrid();
// Baseline alignment
grid.alignBaseline(name,nameEditor);
grid.alignBaseline(phone,phoneEditor);
// Create and return the panel cell
return new PanelCell(panel,grid);
}
public static String [] breedCategories= {
"Best of Breed", "Prettiest Female", "Handsomest Male",
"Best Dressed", "Fluffiest Ears", "Most Colors",
"Best Performer", "Loudest Bark", "Best Behaved",
"Prettiest Eyes", "Most Hair", "Longest Tail",
"Cutest Trick"};
public static String [] breeds=
{"Collie", "Pitbull", "Poodle", "Scottie"};
public static void createGUI()
{
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container container=frame.getContentPane();
// Create Components
JComboBox combo=new JComboBox(breeds);
JLabel dogName=new JLabel("Dog's Name");
JTextField dogNameEditor=createTextField("Fifi",20);
JLabel breed=new JLabel("Breed");
JLabel photo=new JLabel("Photo");
JLabel categories=new JLabel("Categories");
JButton browse=new JButton("Browse..");
JButton delete=new JButton("Delete");
JButton enter=new JButton("Enter");
JPanel imagePanel=new JPanel();
imagePanel.setBackground(new Color(200,200,255));
imagePanel.setBorder(BorderFactory.createLoweredBevelBorder());
JList list=new JList(breedCategories);
list.setBorder(BorderFactory.createLoweredBevelBorder());
list.setBackground(Color.white);
list.setVisibleRowCount(100);
GridRows rows=new GridRows();
// Row 1 starting with "Dog's Name"
rows.newRow().add(dogName,dogNameEditor).span(2);
// Row 2 starting with "Breed"
rows.newRow().add(breed,combo).add(CENTER,CENTER,categories);
// Row 3 starting with "Photo"
// imagePanel spans three rows and list spans four rows.
rows.newRow().add(photo,imagePanel,list);
// Row 4 starting with "Browse" button.
// Note the call to 'spanVertical' for
// the imagePanel and the list.
rows.newRow().add(RIGHT,Cell.NO_ALIGNMENT,browse);
rows.getCurrentRow().spanVertical().spanVertical();
// Rows 5 starting with "Delete" button.
// Note the call to 'spanVertical' for the
// imagePanel and the list.
rows.newRow().add(RIGHT,NO_ALIGNMENT,delete);
rows.getCurrentRow().spanVertical().spanVertical();
// Row 6 starting with "Owner Info" panel
// Note the call to 'spanVertical' for the list.
rows.newRow().add(ownerInfoPanelCell()).span(2).spanVertical();
// Row 7 terminating with "Enter" button
rows.newRow().add(RIGHT,CENTER,enter).span(3);
// Create the main grid
CellGrid grid=rows.createCellGrid();
// Size constraints
grid.linkWidth(categories,new Component[]{list},2);
grid.linkWidth(browse,new Component[]{delete},1);
// Make the list expandible in both directions.
grid.setFixedWidth(new Component[]{list},false);
grid.setFixedHeight(new Component[]{list},false);
// Fix the combo box height
grid.setFixedHeight(new Component[]{combo},true);
// Baseline alignments
grid.alignBaseline(dogName,dogNameEditor);
grid.alignBaseline(breed,combo,categories);
// Create the layout
grid.createLayout(container);
frame.pack();
frame.setSize(frame.getPreferredSize());
frame.setVisible(true);
}
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
breedCategories
public static java.lang.String[] breedCategories
breeds
public static java.lang.String[] breeds
Example9
public Example9()
createTextField
public static javax.swing.JTextField createTextField(java.lang.String text,
int len)
ownerInfoPanelCell
public static PanelCell ownerInfoPanelCell()
createGUI
public static void createGUI()
main
public static void main(java.lang.String[] args)
run
public static void run()