Home Java Java-tips Summaries Summary - Basic GUI Components

Ask Questions?

View Latest Questions

Advertisement


 
 

Summary - Basic GUI Components
Posted on: July 27, 2006 at 12:00 AM
Most components have a few common methods.

Java: Summary - Basic GUI Components

Swing API provides lots components for crating the GUI for Java Swing applications.

In this section we are studying the Basic GUI components of Swing framework.

We will study the following GUI components:

a) JLabel

b) JTextField

c) JButton

d) JTextArea

e) JCheckBox

f) JList

Let's see all these components in detail.

Most components have a few common methods.
cmp.requestFocus(); Puts focus (eg, blinking cursor) in field, select button, etc.
tf.setFont(f); Sets font.
JLabel - For fixed text.
lbl = new JLabel(t) Creates JLabel with text on it. Typically created in call to add() and not assigned.
JTextField - Box containing one line of text.
tf = new JTextField(n); Creates textfield about n columns wide.
s = tf.getText(); Returns string in textfield.
tf.setText(s); Sets text to s.
tf.addActionListener(lst); Action listener lst will be called if enter typed.
tf.setEditable(bool); Don't allow user to edit text field used for output.
tf.setHorizontalAlignment(align); JTextField.LEFT (default), JTextField.CENTER, or JTextField.RIGHT
JButton - Standard clickable button.
btn = new JButton(t); Creates button with text t.
btn = new JButton(img); Creates button with icon img.
btn = new JButton(t, img); Creates button with both text and icon.
btn.addActionListener(lst); Action listener lst called when button clicked.
btn.setEnabled(bool); Used to enable/disable button.
JTextArea - Box containing multiple lines of text separated by '\n'.
ta = new JTextField(rows, cols); Creates textarea with specifed number of rows and columns.
s = ta.getText(); Returns string in textfield.
ta.setText(s); Sets text to s.
ta.append(s); Adds s to end of existing text.
ta.insert(s, pos); Inserts s at position pos.
ta.setEditable(bool); Don't allow user to edit textarea if used for output.
ta.setLineWrap(bool); Allow/disallow long lines to wrap.
ta.setWrapStyleWord(bool); Call setLineWrap(true) first. true wraps at word boundaries, false (default) at characters.
ta.setBorder(brdr); Add space between text and edge. Eg, to add 2 pixels use brdr BorderFactory.createEmptyBorder(2,2,2,2)
JCheckBox - Check box followed by text. Use with listener or to get/set state.
cb = new JCheckBox(text); Creates check box initially unchecked.
cb = new JCheckBox(text, bool); Creates check box with checked state tf.
b = cb.isSelected(); Returns bool if box is checked/unchecked.
cb.setSelected(bool); Sets checked state to tf.
ta.addItemListener(itemListen); Adds an item listener.
JList - List of string values. Put in JScrollPane if scrolling needed.
lst = new JList(); Creates empty JList.
lst = new JList(sarray); Creates JList intialized to String array sarray. Can also use array of Objects.
lst.setListData(sarray); Sets list "model" to String array sarray.
s = (String)lst.getSelectedValue(); Returns value as Object value. Typically cast to String. null if no selection.
i = lst.getSelectedIndex(); Returns index of selection or -1 if no selection.

Copyleft 2005 Fred Swartz
Advertisement


DMCA.com