Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Summary - Basic GUI Components (Widgets)

JavaFAQ Home » Swing, AWT Go to all tips in Swing, AWT


Bookmark and Share
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.
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 ' '.
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.
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 Fred Swartz

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy