Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Cheat Sheet for Swing GUI Components, Cheat Sheet of Programming Languages

A cheat sheet for Swing GUI components, specifically focusing on the parts of the GUI, components, and containers. It includes information on the properties of JComponents, as well as methods for creating and manipulating JLabels, JButtons, JTextAreas, JDialogs, and more. The document also covers useful container methods, such as adding and removing components, setting layouts, and refreshing the layout. This cheat sheet is likely useful for students studying GUI programming in a computer science or software engineering program.

Typology: Cheat Sheet

2021/2022

Uploaded on 05/11/2023

lumidee
lumidee 🇺🇸

4.4

(47)

115 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Cheat Sheet for Swing GUI Components and more Cheat Sheet Programming Languages in PDF only on Docsity! CSE 331 Section 6 cheat sheet Swing Parts of the GUI Window A first-class citizen of the graphical desktop  Also called a top-level container  In both AWT and Swing: a class that extends Window JFrame  new JFrame(String title) make a new frame with optional title  setVisible(true) make a frame appear on the screen  add(Component comp) place the given component or container inside the frame  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) make it so that the program exits when the frame is closed  setSize(int width, int height) gives the frame a fixed size in pixels  pack() resize the frame to fit the components inside it snugly JDialog  new JDialog(Frame parent, String title, boolean modal) make a new JDialog with given parent and title. If modal is set, the parent will be locked until the dialog is closed  JOptionPane.showMessageDialog(parent, message) static method to pop up a dialog with just a message and OK button  JOptionPane.showConfirmDialog(parent, message) static method to pop up a dialog with a message and Yes and No buttons  JOptionPane.showInputDialog(parent, message) static method to pop a dialog with a message and a text field for entering information and many more… Component A GUI widget that resides in a window  Also called a control in many other languages and graphical toolkits  In Swing: a class that extends JComponent (which extends Component and Container from AWT) JComponent Properties: name type description background Color background color behind component foreground Color foreground color of component border Border border line around component enabled boolean whether it can be interacted with focusable boolean whether key text can be typed on it font Font font used for text in component height, width int component’s current size in pixels visible boolean whether component can be seen tooltip text String text show when hovering mouse size, minimum / maximum / preferred size Dimension various sizes, size limits, or desired sizes that the component may take Each property has a get (or is) accessor method, and a set modifier method. JLabel  new JLabel(String text) creates a new label with the given text  getText() returns the text showing on the label  setText() sets label’s text JButton  new JButton(String text) creates a new button with text  getText() returns the text showing on the button  setText(String text) sets button’s text  setMnemonic(char) assigns the given character as a hotkey for this button, activated when keyboard focus is on the appropriate component and Alt+char is pressed JTextArea  new JTextArea(int lines, int columns) create a new text area with preferred size for the given number of lines and columns JTextField (Also useful: JFormattedTextField, which can enforce formatting of entered text)  new JTextField(int columns) create a new field, the given number of columns wide and many more… Container A logical grouping for storing components  In Swing: containers are just another type of component  Used to create a hierarchy of components inside components inside components... all inside a top-level window  JFrame can also be used as a container (thought it doesn’t extend JComponent) Useful container methods:  add(Component comp) places the given component inside this container  add(Component comp, Object info) places the given component inside this container  remove(Component comp) removes the given component from this container  setLayout(LayoutManager mgr) use a particular layout to position components  validate() refreshes the layout if changes have been made after the container is onscreen JPanel A general container JScrollPane Used to add scroll bars to a component when it becomes too large  new JScrollPane(Component comp) and many more…
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved