Most components have a few common methods. |
| cmp.requestFocus(); |
Puts focus (eg, blinking cursor) in the field, selection of a button, etc. |
| tf.setFont(f); |
Sets font. |
lbl = | new JLabel(t) |
Creates JLabel with text on it. Typically created in call to add() and not assigned. |
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 |
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. |
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) |
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. |
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. |