Swing Method & Constructors

Table JFrame Constructors & Methods
Constructor Description
JFrame() Create a new frame with no title
JFrame(String title) Creates a new friend with a specific title
void setLocation (int x, int y) Sets the position of the frame on screen at x and y coordinate The top left corner of the screen is 0.0
void setLocationRelativeTo(Component c) Centers the frame on screen if the parameter is null
void setResizeable(boolean value) Sets whether or not the size of the frame can be change by the user. Default setting is true.
void setSize(int width, int height) Sets the size of the frame to the specified width and height
void setJMenuBar(JMenuBar menu) Sets the menu for the frame
Method Descritpion
void pack() Adjust the size of the frame to fit the components we've added
void setDefaultCloseOperation Sets the action when the user closes the Frame
void setLayout(LayoutManager layout) Sets the layout manager used to control how components are arranged when the frame is displayed
void add(Component c) Adds the specified component to the frame
void setIconImage(Icon image) Sets the icon displayed when the frame is minimized
void remove(Component c) Removes the specified component from the frame
JMenuBar getJMenuBar() Gets the menu for the frame

 

Table JButton Constructors and Methods
Constructor Description
JButton() Create a new button
JButton(String text) Creates a new button with a specified text
void setText(String text) Sets the text displayed by the button
void setToolTipText(String text) Sets the tooltip text that displayed if the user lets the mouse rest over the button
void setContentAreaFilled(boolean value) The default setting is true. It determined whether or not the button's background should be filled or left empty
void setEnabled(boolean value) Enable or disable the button.
void setRolloverEnabled(boolean value) It enables or disables the rollover effect, hence causes the border to get thicker when the mouse moves over the button.
void setVisible(boolean value) Shows or hides the button
Method Description
String getText() Returns the text that is displayed by a push of a button
doClick() Triggers an action event for a button if the user clicks it
void setBorderPainted(boolean value) Shows or hides the button's border

 

Table JPanel Constructors and Methods
Constructor Description
JPanel() Create a new panel
JPanel(LayoutManager layout) Creates a new panel with a specified layout manager
JPanel(boolean isDoubleBuffered) Creates a new panel. If the parameter is true, the panel uses the double-buffering technique, which makes the graphic applications to display better
Method Description
void setLocation(int x int y) Sets the x & y coordinate of the frame
void add(Component c) Adds the specified component to the panel
void remove(Component c) Removes the specified component from the panel
void setSize(int width int height) Sets the size of the frame with a specified with and height
void setLayout(LayoutManager layout) Sets the layout manager used to control how components are arranged when the panel is displayed
void setToolTipText(String text) Sets the tooltip text that's displayed if the user rests the mouse over an empty part of the panel

 

Table JLabel Constructors and Methods
Constructor Description
JLabel() Create a new label with no text
JLabel(String text) Creates a new label with a specified text
Method Description
String getText() Returns the displayed text
void setVisible(boolean value) Shows or hides the label
void setToolTipText(String text) Sets the tooltip text that displayed when the user rests the mouse over the label
void setText(String text) Sets the displayed text by the label


 

Table Used Event Classes  
Event Class Listener Interface Descritpion
ActionEvent ActionListener Performs an action when the user clicks a button or other components
ItemEvent ItemListener When the list of an item changes, such as combo box or list box
DocumentEvent DocumentListener It will be create when a text component is change, such as text field
WindowEvent windowListener It will be create when the status of the window frame changes
KeyEvent KeyListener It will be create when the user presses a key on the keyboard
MouseEvent MouseListener It will be create when the user does something with the mouse
FocusEvent FocusListener Create when a component receive or loses focus

 

Table Event Listener Interfaces  
Listener Interface Method Description
ActionListener(ActionEvent e) void actionPerformed Calls the method when an action even occurs, such as a push of a button
WindowListener void windowClosed(WindowEvent e) Calls the method when a window has been closed
  void windowActivated(WindowEvent e) Calls the method when the window is activated
  void windowClosing(WindowEvent e) Calls the method when the user attempts to close the window
  void windowIconified(WindowEvent e) Calls the method when the window is changed
  void windowDeactivated(WindowEvent e) Calls the method when the window is deactivated
  void windowOpened(WindowEvent e) Calls the method when the window is minimized to an icon
DocumentListener void removeUpdate(DocumentEvent e) Calls the method when text is deleted
  void changeUpdate(DocumentEvent e) Calls the method when text is changed
  void insertUpdate(DocumentEvent e) Calls the method when text is inserted
FocusListener void focusGained(FocusEvent e) Calls the method when the component gain focus
FocusListener void focusLost(FocusEvent e) Calls the method when the component looses focus
KeyListener void keyPressed(KeyEvent e) Calls the method when the user presses a key
  void keyReleased(KeyEvent e) Calls the method when the user releases a key
  void keyTyped(KeyEvent e) Calls the method when the user types a key
MouseListener void mouseClicked(MouseEvent e) Calls the method when the user clicks the mouse button
  void mouseEntered(MouseEvent e) Calls the method when the mouse moves over a component
  void mousePressed(MouseEvent e) Calls the method when the use presses the mouse button
  void mouseExited(MouseEvent e) Calls the method when the mouse leaves a component
  void mouseReleased(MouseEvent e) Calls the method when the user releases the mouse button

 

Table JTextField Constructors and Methods
Constructor Description
JTextField() Creates a new text field
JTextField(String text, int clos) Creates a new text field with a specified initial text value and the width
JTextfield(int cols) Creates a new text field with a specified width
Method Description
String getText() Gets the text value that is entered into the field
void setText() Sets the text value for the field
void setColumns(int cols) Sets the size of the text field
void setEditable(boolean value) If the value is set to false, text field can not be edit
void requestFocus() Asks for the focus to be moved to the text field
void setToolTipText(String text) If the user rests the mouse over the text field, the tooltip text will be displayed

 

Table JTextArea Constructors and Methods
Constructor Description
JTextArea() Creates a new text area
JTextArea(String text, int rows, int cols) Creates a new text area with a specified initial text value, that can display the specified number of rows and columns
JTextArea(int rows, int cols) Creates a new text area that can display the specified number of rows and columns
Method Description
String getText() Gets the text value that is entered into the field
int getLineCount() Gets the number of lines currently in the text value
void append(String text) Adds the specified text to the end of the text area's field
void requestFocus() Asks for the focus to be moved to the text field
void replaceRange(String text, int start, int end) Replaces text indicated by the start and end with a specified text
void insert(String text, int post) Inserts a specified text at the specified position
void setEditable(boolean value) If it's false, the text field can not be edit
void setColumns(int cols) Sets the width of the text area
void setText(String text) Sets the field's text value
void setLineWrap(boolean value) Lines wrap if the text doesn't fit on one line, only if value is true
void setWrapStyleWord() if true, text wraps at word boundaries
void setToolTipText(String text) Sets the tooltip text if the user rests the mouse pointer over the text field

 

Table JCheckBox Constructor and Methods
Constructor Description
JCheckBox() Creates a new check box, initially unchecked
JCheckBox(String text) Creates a new check box with a specified text display
JCheckBox(String text, boolean selected) Creates a new check box with a specified text, if it's true the check box will be checked
Method Description
String getText() Gets the displayed text by the check box
Boolean isSelected() Returns true if box is checked otherwise returns false
void addActionListener(ActionListener listener) Adds the action listener to listen for the event
void addItemListener(ItemListener listener) Adds the item listener to listen for the event
void setSelected(boolean value) The box will be checked if value true otherwise it's unchecked
void setToolTipText(String text) Sets the tooltip text if mouse is rest over the check box
void setText(String text) set the text for the check box

 

Table JRadioButton Constructor and Methods
Constructor Description
JRadioButton() Creates a new radio button with no text
JRadioButton(String txt) Creates a new radio button with a specified text
Method Description
void setText(String txt) Sets the text for the radio button
String getText() Gets the text displayed by the radio button
void addActionListener(ActionListener listener) Adds an action listener to listen for the events
void addItemListener(ItemListener listener) Adds an item listener to listen for the vents
void setSelected(boolean value) If value is true, radio button will be selected
Boolean isSelected() Returns true if radio button is selected otherwise returns false
void setToolTipText(String txt) Sets the tooltip text displayed by resting the mouse over the radio button

 

Table JScrollPane Constructors and Fields
Constructor Description
JScrollPane(Component c) Creates a scroll pane for the specified component
JScrollPane(Component, int vert, int hor) Creates a scroll pane for the specified component with vertical and horizontal bar
Field Description
VERTICAL_SCROLLBAR_ALWAYS Always adds a vertical scroll bar
VERTICAL_SCROLLBAR_AS_NEVER Never adds a vertical scroll bar
VERTICAL_SCROLLBAR_AS_NEEDED Adds a vertical bar whenever is needed
HORIZONTAL_SCROLLBAR_ALWAYS Always add a horizontal scroll bar
HORIZONTAL_SCROLLBAR_NEVER Never adds a horizontal scroll bar
HORIZONTAL_SCROLLBAR_AS_NEEDED Adds a horizontal scroll bar whenever is needed

 

Table JSlider Constructors and Methods
Constructor Description
JSlider() Create a new slider
JSlider(int min, int max) Creates a new slider with a specified minimum and maximum values.
JSlider(int min, int max, int val) Creates a new slider with a specified minimum, maximum, and initial values
JSllider(int orientation, int max, int val) Creates a new slider with a specified minimum, maximum, and initial values. The orientation can either be JSlider.HORIZONTAL or JSlider.VERTICAL
Method Description
int getValue() Gets the value indicated by the currently position
void setFont() Sets the font of the text with the slider
void addChangeListener(ChangeListener listener) Ass a change listener to listen for change events
void setMajorTickSpacing(int val) Sets the interval for major tick marks
void setInvert(boolean val) If value is true, inverts the slider's direction so the max value will be on the left and the min value will be on the right
void setMinimum(int val) Sets the minimum value
void setMaximum(int val) Sets the maximum value
setOrientation(int orientation) Sets the orientation, values are JSlider.HORIZONTAL and JSlider.VERTICAL
void setPaintLabels(boolean val) If the value is true, the tick labels are shown
void setSnapToTicks(boolean value) If the value is true, the value returned by the getValue method is rounded to the nearest tick mark
void setToolTipText Sets the tooltip text that is displayed if the user rests the mouse pointer over the slider
void setMinorTickSpacing(int val) Sets the interval for minor tick marks

 

Table JList Constructors and Methods
Constructors Description
JList() Creates an empty list
JList(Vector[] val) Creates a list and fills it with the values that is in the vector
JList(ListModel list) Creates a list that uses a specified list model
JList(Ojbect[] val) Creates a list and fills it with the values in the array
void clearSelection() Clears all the selections
Method Description
int getSelectedIndex() Returns the index of the first selected item, if no items are selected -1 will be returned
int[] getSelectedIndexes() Returns an array with the index of each selected item, if the array is empty no items are selected
boolean isSelectionEmpty() It returns true if no items are selected
boolean isSelectedIndex(int index) It will return true if the item at the specified index is selected
Object getSelectedValue() It returns the first selected item
Object[] getSelectedValues() Returns an array with all the selected items
void setFixedCellHeight(int height) Sets the height of each row
void setFixedCellWidth(int wid) Sets the width of each row
void setVisibleRowCount(int c) Sets the number of rows that is displayed by the list
void setSelectionMode(int m) It sets the selection mode. Values that are allowed are:

ListSelectionModel.SINGLE_SELECTION,  ListSelectionModel.SINGLE_INTERVAL_SELECTION, and ListSelectionModel.MULTIPLE_INTERVAL_SELECTION

void setSelectedIndices(int[] in) Selects the items at the indices specified in the array
void setSelectedIndex(int index) Selects the item at a specified index

 

Table JComboBox Constructors and Methods
Constructor Description
JComboBox() Creates an empty combo box
JComboBox(Vector[] val) Creates a combo box and fills it with the values in the vector
JComboBox(Object[] val) Creates a combo box and fills it with the values in the array
Method Description
Object getItemAt(int index) Returns the item at a specified index
int getSelectedIndex() Returns the index of the selected item
int getItemCount() Returns the number of item in the combo box
Object getSelectedItem() Returns the selected item
Boolean isEditable() Indicates whether the combo box's text is editable or not
void addActionListener(ActionListener listener) Adds an action listener to the combo box to listen for events
void addItem(Object item) Adds the item to the combo box
void addItemListener(ItemListener listener) Adds an item listener to the combo box
void removeItem(Object item) Removes a specified item
void removeAllItems() Removes all the items from the combo box
void removeItemAt(int index) Removes an item at a specified index
void setEditable(boolean val) If val is true, combo box's text field is editable
void setSelectedIndex(int index) Selects an item at a specified index
void setSelectedItem(Object item) Selects a specified item
void setMaximumRowCount(int c) Sets the number of rows displayed when the combo box list drops down
void insertItemAt(Object item, int index) Inserts an item at a specified index

 

 <Java 24><Home><Swing><Next>