Swing: CS-328 Dick Steflik John Margulies
Swing: CS-328 Dick Steflik John Margulies
Swing vs AWT
AWT is Javas original set of classes for building GUIs
Uses peer components of the OS; heavyweight Not truly portable: looks different and lays out inconsistently on different OSs
Due to OSs underlying display management system
JFrame
Frames are the basis of any Java GUI Frame is the actual window that encompasses your GUI objects; a GUI can have multiple frames The J prefix is at the beginning of any Swing components name (to distinguish them from AWT components) JFrame is a wrapper around AWTs Frame
JFrame - Code
Frame/Pane
Panes/JPanels
The terms pane and panel are used interchangeably in Java If a frame is a window, a pane is the glass Panes hold a windows GUI components Every frame has at least one pane, the default Content Pane
Panes
Useful for layout
If you want to group certain GUI components together, put them inside a pane, then add that pane to the frame
Content Pane
When a frame is created, the content pane is created with it To add a component to the content pane (and thus to the frame), use:
frameName.getContentPane().add(comp onent name);
Text Areas
Specified by Javas JTextarea class Multiple constructors allow you to create a new text area with a specified size and/or specified text A text area is just a white space of variable size that can hold text If text goes out of the areas bounds, it will exist but some of it will not be seen
Wrap the text area in a scrollable pane
Text Areas
JTextarea Methods
textarea.setText(String); textarea.getText(String); textarea.append(String); textarea.setEditable(boolean);
JScrollPane
Similar to a regular pane, only, when necessary, a scrollbar appears to allow scrolling through the panes contents Particularly useful for embedding tables and text areas, as these tend to contain more content than they can show at one time
JScrollPane
Default constructor (JScrollPane()) creates a scrollable pane that you can add components to Alternatively, you can initialize a pane to wrap itself around a component
JScrollPane newPane = new JScrollPane(JTextArea area);
JScrollPane
JTextField
A Java text field is essentially the same as a text area, only limited to one line Very similar set of methods JPasswordField is the same as JTextField, only the contents are hidden Different constructors allow you to predefine the number of columns and/or the default text
JButton
Java class that allows you to define a button Multiple constructors allow you to initialize a button with a predefined label and/or a predefined icon Although the buttons action can be defined in the constructor, defining a buttons action can take many lines of code and should be done separately
Defining a JButton
JButton button = new JButton(Press Me!); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { /* insert action here */ } });
/* setting an action requires that you import java.awt.event.* */
Model-View-Controller
Design pattern often used in Swing objects Breaks a GUI object down into three parts
Model manages the data used by the object View manages the graphical/textual output of the object Controller interprets user input, commanding the model and view to change as necessary
Model-View-Controller
Swing components that use the MVC pattern, such as JList and JTable, generally have one class that controls both the view and the controller and a separate class that controls the model
Model-View-Controller
Programmer instantiates a model (e.g., the DefaultTableModel class), then loads that model with the data to be displayed in the GUI The view/controller class (e.g., the JTable class) is then instantiated from the model
If the programmer instantiates the GUI object without a model, the view/controller class creates an empty model to work from
JTable table = new JTable(DefaultTableModel model);
JList
A simple GUI object design to hold lists of objects and allow users to make selections from the list Can be created from a ListModel, a Vector, or an array (all essentially lists themselves)
JTable
Usually created from a DefaultTableModel
Can also be created from an array of arrays or a Vector of Vectors, or can have no initial data
Layout Managers
Every pane has a layout manager Layout managers tell Java where to put components when you add them to a pane The default layout manager is FlowLayout, which lays out components from left to right until there is no room left on a line, then starts the next line Lays out components in the order they are added Layouts can be nested, one inside of another making them quite versatile
GridLayout
Programmer defines matrix dimensions; objects are then put in the matrix in the order they are added, left to right, top to bottom
BoxLayout
BoxLayout is a simple way to come close to absolute positioning (which
isnt recommended)
Panes can be laid out either top to bottom or left to right Panes laid out with BoxLayout can be put in other BoxLayout panes, creating a grid of completely variable size and a very controlled layout
BoxLayout
BoxLayout
Pane.setLayout(new BoxLayout(Pane, BoxLayout.Y_AXIS)); where Pane is the name of the pane you are laying out
For the entire Java API specification, including all the Swing APIs, go to
http://java.sun.com/j2se/1.4/docs/api/