Please enable JavaScript.
Coggle requires JavaScript to display documents.
Java Swing Components and Layout Managers - Coggle Diagram
Java Swing Components and Layout Managers
JFrame
the object that represents a window on the screen
JFrame frame = new JFrame();
Creating a button on the JFrame and setting text to its name:
JButton btn1 = new JButton();
btn1.setText("Button 1");
Adding the widget to the frame:
frame.getContentPane().addbutton();
Setting the window size:
frame.setSize(300, 300);
frame.setVisible(true);
Adding 2D graphic objects to the JFrame
create GrawPnel class, extend it with JPanel class, and override the paintComponent() method
Two ways of creating a frame:
Association
by creating the object of Frame class
by writing all the codes of creating JFrame, JButton and method call inside the java constructor
Inheritance
by extending Frame class
Event Handling
used to handle different events
Listener Interface
bridges the listener (you) and the event source (the button)
Events
ActionEvent
listener interface: ActionListener
method: actionPerformed
MouseEvent
listener interface: MouseListener
methods:
mouseClick
mousePressed
mouseReleased
KeyEvent
each has a matching listener interface
found in Java.awt package
can have different methods to perform various actions for the same event
Java Swing
provides platform-independent and lightweight components
package: javax.swing
Java Foundation Classes (JFC)
a set of GUI components which simplify the development of desktop applications
Hierarchy of Java Swing API
Object
Component
JComponent
JLabel
Constructors
JLabel()
JLabel(String s)
JLabel(Icon i)
JLabel(String s, Icon i, int horizontalAlignment)
Methods
String getText()
void setText(String text)
void setHorizontalAlignment(int alignment)
Icon getIcon()
int getHorizontalAlignment()
JList
Constructors
JList()
Creates a JList with an empty, read-only, model
JList(ary[] listData)
displays the elements in the specified array
JList(ListModel<ary> dataModel)
displays elements from the specified, non-null, model
Methods
Void addListSelectionListener(ListSelectionListener listener)
int getSelectedIndex()
ListModel getModel()
ListModel getModel()
JTable
Constructors
JTable()
table with empty cells
JTable(Object[][] rows, Object[] columns)
table with the specified data
JComboBox
Constructors
ListModel getModel()
JComboBox()
JComboBox(Object[] items)
Methods
void addItem(Object anObject)
void removeItem(Object anObject)
void removeAllItems()
void removeAllItems()
void addActionListener(ActionListener a)
void addItemListener(ItemListener i)
JSlider
Constructors
JSlider()
creates a slider with the initial value of 50 and range of 0 to 100
JSlider(int orientation)
creates a slider with the specified orientation set by either JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to 100 and initial value 50
JSlider(int min, int max)
creates a horizontal slider using the given min and max
JSlider(int min, int max, int value)
creates a horizontal slider using the given min, max and value
JSlider(int orientation, int min, int max, int value)
creates a slider using the given orientation, min, max and value
Methods
public void setMinorTickSpacing(int n)
sets the minor tick spacing to the slider
public void setMajorTickSpacing(int n)
sets the major tick spacing to the slider
public void setPaintTicks(boolean b)
determines whether tick marks are painted
public void setPaintLabels(boolean b)
determine whether labels are painted
public void setPaintTracks(boolean b)
determine whether track is painted
JMenu
Class declaration: public class JMenu extends JMenuItem implements MenuElement, Accessible
AbstractButton
Methods
void setText(String s)
String getText()
void setEnabled(boolean b)
void setIcon(Icon b)
Icon getIcon()
void setMnemonic(int a)
void addActionListener(ActionListener a)
JButton
Constructors
JButton()
no text and icon
JButton(String s)
specified text
JButton(Icon i)
specified icon object
Container
Window
Frame
Dialog
Panel
Applet
Common methods:
public void add(Component c)
add a component on another component
public void setSize(int width,int height)
sets size of the component
public void setLayout(LayoutManager m)
sets the layout manager for the component
public void setVisible(boolean b)
sets the visibility of the component
it is by default false
Java LayoutManagers
arranges components in a particular manner
BorderLayout
arranges the components in five regions: north, south, east, west, and center
Constructors
BorderLayout()
BorderLayout(int hgap, int vgap)
every content pane is initialized to use a BorderLayout
the content pane is the main container in all frames, applets, and dialogs
BoxLayout
puts components in a single row or column
CardLayout
implements an area that contains different components at different times
FlowLayout
default layout manager for every JPanel
lays out components in a single row, starting a new row if its container is not sufficiently wide
GridBagLayout
aligns components by placing them within a grid of cells, allowing components to span more than one cell
rows in the grid can have different heights, and grid columns can have different widths
GridLayout
makes a bunch of components equal in size and displays them in the requested number of rows and column
GroupLayout
works with the horizontal and vertical layouts separately
layout is defined for each dimension independently
each component needs to be defined twice in the layout
SpringLayout
specifies precise relationships between the edges of components under its control