How to get started with Swing Chapter 18 Murach's Java Programming

¡Supera tus tareas y exámenes ahora con Quizwiz!

JComponent

A base class for Swing components such as JPanel, JButton, JLabel, and JTextField.

QUESTION_MESSAGE

A question icon

message

A string representing the message to be displayed in the dialog box.

title

A string representing the title of the dialog box

WARNING_MESSAGE

A warning icon

Container

An abstract AWT class that defines any component that can contain other components.

Component

An abstract AWT class that defines any object that can be displayed. For instance, frames, panels, buttons, labels, and text fields are derived from this class.

ERROR_MESSAGE

An error icon

INFORMATION_MESSAGE

An information icon

messageType

An int that indicates the type of icon that will be used for the dialog box. You can use the fields of the JOptionPane class for this argument.

parent

An object representing the component that's the parent of the dialog box. If you specify null, the dialog box will appear in the center of the screen.

Event Dispatch Thread (EDT)

Any code that runs in response to a Swing event runs on a special thread.

Dimension class

Dimension(width, height) Specifies the width and height of a component in pixels setMinimumSize(Dimension) Specifies the minimum width and height of a component setPreferredSize(Dimension) Specifies the preferred width and height of a component

Validation

GUI applications should validate all data entered by the user before processing the data When an entry is invalid, the application can display an error message and give the user another chance to enter valid data To test whether a value has been entered into a text field, you can use the getText() method of the text field to get a string that contains the text the user entered. Then, you can check whether the string is empty by using its isEmpty() method. To test whether a text field contains valid numeric data, you can code the statement that converts the data in a try block and use a catch block to catch a NUmberFormatException.

BorderLayout

Lays out component by providing five areas that can each hold one component. The five areas are north, south, east, west, and center add(component, area) Adds the specified components to the specified area. To set the area, you can specify the NORTH, SOUTH, EAST, WEST, or CENTER constant of the BorderLayout class.

FlowLayout

Lays out components by flowing them from left to right, or right to left, similar to words on a page. FlowLayout(alignment) Sets the horizontal alignment of this manager. To set this alignment, you can specify the LEFT, RIGHT, or CENTER constant of the FlowLayout class. If the components don't fit on one line, the FlowLayout Manager wraps the components to a new line. The FlowLayout manager can align components to the left, right, or center. The default is center.

BoxLayout

Lays out components in a horizontial or vertical row of cells. This layout is rarely used, but it can be useful for producing rows of buttons.

GridLayout

Lays out components in a rectangular grid of cell is the same size.

GridBagLayout

Lays out components in a rectangular grid of cells. Can be used to create more complex and flexible laysout than some of the other layout managers

CardLayout

Lays out components on a card where only card is visible at a time. This layout is rarely used, but can be useful for creating wizard dialogs that guide the user through a step-by-step procedure.

PLAIN_MESSAGE

No icon

Frame

The AWT class that defines a window with a title bar and border.

Window

The AWT class that defines a window without a title bar or border.

JButton Class

The Swing class that defines a button. JButton(String) Creates a button and sets the text it displays. setText(String) Sets the text displayed by the button. setEnabled(boolean) Determines whether the button is enabled or disabled and grayed out. setToolTipText(String) Sets the text for the tooltip. If the user hovers the mouse over the button for about one second, the tooltip is displayed. To create a button, you can create a JBUtton object. Then, you can add it to a frame or a panel.

JLabel Class

The Swing class that defines a label JLabel() Creates a JLabel object. JLabel(String) Creates a JLabel object and sets the text displayed by the label to the specified string. setText(String) Sets the text displayed by the label to the specified string. A JLabel component defines a label, which is a non-editable component that typically displays text that identifies other components such as text fields

JPanel Class

The Swing class that defines a panel, which is used to hold other components To create a panel you can create a JPanel object. Then, you can add it to the frame. By default, a JPanel uses the FlowLayout manager

JTextField Class

The Swing class that defines a text field A JTextField component defines a text field that displays text to the user and allows the user to enter text. This type of component is also known as a text box. JTextField() Creates a text field. JTextField(int) Creates a text field with the specified number of columns (characters) getText(String) Gets the text contained by the text field. setText(String) Sets the text displayed by the text field. setColumns(int) Sets the width of the text field to the specified number of columns (characters). Many layout managers ignore this setting. setEditable(boolean) Determines whether the user can edit the text or not setEnabled(boolean) Determines whether the text field is enabled. If set to false, the text field is disabled and grayed out so the user can't change the text in it.

JFrame

The Swing class that defines a window with a title bar and border.

action listener

To make a button do something when clicked, you add an action listener by calling its addActionListener() method. The addActionListener() method accepts an ActionListener object that includes an actionPerformed() method that accepts and ActionEvent object. Within the actionPerformed() method, you write the code that's executed when the button is clicked

inner class

a class that is defined inside another class

Loosely coupled

allows this class to work with different types of controls. refers to the strings that the user enters into the control

lambda expression

an anonymous method that provides the parameters and code for the method, but no name. To code a lambda expression, you code the variable name for the ActionEvent object, followed by the lambda operator (->) and the code for the method.

layout manager

determines how your components are placed in the container and how they behave if the container is resized or if the font size changes.

GridBagConstraints

gridx, gridy Sets the x and y coordinates of the component in the grid where 0, 0 is the top left cell of the grid. anchor Sets where the component is displayed if the component is smaller than the cell that is its display area. The most commonly used are LINE_START and LINE_END. In older versions of Java, these were called WEST and EAST. gridwidth, gridheight Sets the number of horizontal and vertical cells that the component occupies. insets Uses an Insets object to specify how much external padding should be applied to the components within the grid. ipadx, ipady Sets how much internal padding to add to the width and height of the component. The value applies to both side Insets(top, left, bottom, right) Specifies the top, left, bottom, and right padding in pixels. You can reuse the same GridBagConstraints object for multiple components. However, you must reset any values you don't want to use in the next component

anonymous class

is an inner class that doesn't have a name. You can code the braces for an anonymous class immediately after the parentheses that create an object from the interface. Within the braces, you can supply the code that implements the interface.

panel

is an invisible container that's used to group other components . To create a panel you can create a JPanel object. Then, you can add it to the frame.

Swing

is an object-oriented API, and the swing classes exist in a hierarchy. Swing builds on the older AWT framework. By default, Swing components use the Metal look and feel, which causes them to look and act the same on any operating system. The exception is the title bar for the frame, whose look and feel depends on the operating system. To Set the look and feel to the default for the operating system, you call the setLookAndFeel() method of the UIManager class inside the try block of a try/catch statement

Swing Library

is built upon the AWT library. Swing classes often inherit AWT classes. Most swing classes begin with the letter J (for Java)

A common method of most container components

setLayout(layout) Sets the layout to the specified layout manager.

JFrame Class

setTitle(String) Sets the title of the frame to the specified string. setSize(width, height) Sets the size of the frame to the specified width and height pack() Sizes the frame so it's just large enough to hold its components. setLocationByPlatform(boolean) If Set to true, the operating system sets the location of the frame. setDefaultCloseOperation(operation) Set to the EXIT_ON_CLOSE field of the JFrame class to exit the application when the user clicks the close button of the frame. setVisible(boolean) Determines whether the frame is visible. add(Component) Adds the specified component to the frame or panel. By default, a JFrame uses the BorderLayout manager

SwingValidator Class

validate multiple entries


Conjuntos de estudio relacionados

ACCT 3021 Chapter 19 - Income Taxes

View Set

anthrbio 167 unit 3 cumulative quiz

View Set

GS MKT 306 CH 6 Product Development

View Set

ΒΙΟΛΟΓΙΑ -2ο Κεφάλαιο β ( φωτοσύνθεση) ( πέψη στους μονοκύτταρους οργανισμούς) ( πέψη στους ζωικούς)

View Set

Long and Short U Sounds Spelling Words

View Set